*: Move die() into .local/bin/helpers.sh

The function `die` was redundantly implemented in various files.

Move the function into .local/bin/helpers.sh and source that where
previously implemented.

Also prepend the program's name to the message and always terminate the
message with a newline. The newline was previously needed for a small
but unnecessary hack that prevented the need of the `[ -z "$1" ]` test.
This commit is contained in:
2022-09-22 14:29:35 +02:00
parent d7c9bda8a7
commit 7666086dba
5 changed files with 31 additions and 27 deletions

View File

@@ -1,18 +1,16 @@
#!/bin/sh
die() {
printf "$1" >&2
exit ${2:-1}
}
# Source die()
. "$HOME"/.local/bin/helpers.sh
remote="$(git remote -v | grep -m1 "druckdev/dotfiles" | cut -f1)"
: "${remote:=origin}"
url="$(git remote get-url "$remote")"
! printf "$url" | grep -q '^git@' || die "Using ssh already.\n" 0
! printf "$url" | grep -q '^git@' || die "Using ssh already." 0
host="$(printf "$url" | cut -d/ -f3)"
grep -q "$host" "$HOME"/.ssh/known_hosts || die "No ssh key for $host found.\n"
grep -q "$host" "$HOME"/.ssh/known_hosts || die "No ssh key for $host found."
ssh_url="$(printf "$url" | sed 's_^https\?://_git@_;s_/_:_')"
git remote set-url "$remote" "$ssh_url"
die "Set $remote remote url to $ssh_url\n" $?
die "Set $remote remote url to $ssh_url" $?