From 7666086dbaa60b9ec5c598e5c7c0ba18441a86dd Mon Sep 17 00:00:00 2001 From: Julian Prein Date: Thu, 22 Sep 2022 14:29:35 +0200 Subject: [PATCH] *: 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. --- .local/bin/filterHistory | 12 +++++------- .local/bin/helpers.sh | 13 +++++++++++++ meta/git-remote | 12 +++++------- meta/git/hooks/commit-msg | 12 +++++------- meta/git/hooks/pre-commit | 9 +++------ 5 files changed, 31 insertions(+), 27 deletions(-) create mode 100755 .local/bin/helpers.sh diff --git a/.local/bin/filterHistory b/.local/bin/filterHistory index 80e8359..cdad175 100755 --- a/.local/bin/filterHistory +++ b/.local/bin/filterHistory @@ -8,14 +8,12 @@ ## format of zshs extended history. ## An automatic backup is created before deleting that can be used for recovery. -die() { - printf "$1" >&2 - exit ${2:-1} -} +# Source die() +. "$HOME"/.local/bin/helpers.sh -[[ $# -eq 1 ]] || die "Specify a history file.\n" -[[ -e "$1" ]] || die "File does not exist.\n" -[[ "$(stat -c '%a' "$1")" = "600" ]] || die "File permissions are off.\n" +[[ $# -eq 1 ]] || die "Specify a history file." +[[ -e "$1" ]] || die "File does not exist." +[[ "$(stat -c '%a' "$1")" = "600" ]] || die "File permissions are off." # Take a history file per stdin and sort the commands per number of occurrences hist-sort() { diff --git a/.local/bin/helpers.sh b/.local/bin/helpers.sh new file mode 100755 index 0000000..318b53f --- /dev/null +++ b/.local/bin/helpers.sh @@ -0,0 +1,13 @@ +#!/bin/sh +# +# A collection of useful shell functions. This file should be sourced, rather +# than executed. + + +# Print error message, prepended by the programs name, and then exit +# +# Usage: die [] [] +die() { + [ -z "$1" ] || >&2 printf "%s: %s\n" "$0" "$1" + exit ${2:-1} +} diff --git a/meta/git-remote b/meta/git-remote index 1a944e0..8c509f6 100755 --- a/meta/git-remote +++ b/meta/git-remote @@ -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" $? diff --git a/meta/git/hooks/commit-msg b/meta/git/hooks/commit-msg index 117e3d7..ede0a11 100755 --- a/meta/git/hooks/commit-msg +++ b/meta/git/hooks/commit-msg @@ -9,10 +9,8 @@ # # To enable this hook, save this file in ".git/hooks/commit-msg". -die() { - printf "$1" >&2 - exit ${2:-1} -} +# Source die() +. "$HOME"/.local/bin/helpers.sh subject="$(head -1 "$1")" # Ignore "fixup! " and "squash! " prefix' added by `git-commit` @@ -39,7 +37,7 @@ body="$( /^[^${git_comment_char/auto/\#}]/p" )" -[[ ${#subject} -le 50 ]] || die "Subject too long. (<= 50)\n" +[[ ${#subject} -le 50 ]] || die "Subject too long. (<= 50)" # The subject line has to match "${pats[@]}", but to be more verbose different # error messages are printed for the different 'levels' of the pattern. @@ -54,7 +52,7 @@ msg=( "Start subject with a capital letter.\n" "Remove punctuation mark from end.\n" ) -[[ ${#msg[@]} -ge ${#pats[@]} ]] || die "Something went wrong internally.\n" +[[ ${#msg[@]} -ge ${#pats[@]} ]] || die "Something went wrong internally." for ((i = 0; i < ${#pats[@]}; i++)); do if ! grep -qE "$(printf "%s" "${pats[@]:0:$i+1}")" <<<"$subject"; then die "${msg[$i]}" @@ -65,6 +63,6 @@ BKP_IFS="$IFS" IFS=' ' for line in $body; do - [[ ${#line} -le 72 ]] || die "Body lines too long. (<= 72)\n" + [[ ${#line} -le 72 ]] || die "Body lines too long. (<= 72)" done IFS="$BKP_IFS" diff --git a/meta/git/hooks/pre-commit b/meta/git/hooks/pre-commit index e15b269..9c9decc 100755 --- a/meta/git/hooks/pre-commit +++ b/meta/git/hooks/pre-commit @@ -6,10 +6,8 @@ # # To enable this hook, save this file in ".git/hooks/pre-commit". -die() { - printf "$1" >&2 - exit ${2:-1} -} +# Source die() +. "$HOME"/.local/bin/helpers.sh if git rev-parse --verify HEAD >/dev/null 2>&1; then against=HEAD @@ -35,8 +33,7 @@ if [ "$allownonascii" != "true" ]; then | wc -c ) if [ $num_nonascii != 0 ]; then - printf "Rename files to only include ASCII characters.\n" >&2 - die "Or set hooks.allownonascii to true.\n" + die "Rename files with ASCII characters only, or enable hooks.allownonascii" fi fi