From 44bfffd53bcc2076aad5e1bb8fafa684a5e6fdb2 Mon Sep 17 00:00:00 2001 From: Christian Heller Date: Fri, 4 Apr 2025 13:16:58 +0200 Subject: [PATCH] Refactor. --- testing/home/desktop/.local/bin/borgplom | 55 +++++--------------- testing/home/desktop/.local/bin/vol | 15 +++--- testing/home/desktop/.plomlib.sh/print_usage | 40 ++++++++++++++ 3 files changed, 59 insertions(+), 51 deletions(-) create mode 100644 testing/home/desktop/.plomlib.sh/print_usage diff --git a/testing/home/desktop/.local/bin/borgplom b/testing/home/desktop/.local/bin/borgplom index 8305282..8f71fbe 100755 --- a/testing/home/desktop/.local/bin/borgplom +++ b/testing/home/desktop/.local/bin/borgplom @@ -5,9 +5,11 @@ cd $(dirname "$0") . lib/constants_borg # PATH_BORG_CONF . lib/get_passphrase . lib/path_tmp_timestamped +. lib/print_usage cd - > /dev/null -BIN_NAME="$(basename $0)" +USAGE_DESCRIPTION='Wrapper around certain borgbackup usages.' +USAGE_LINES='COMMAND\nCOMMAND PATH' PATH_BORG_CONF_SECURITY="${PATH_BORG_CONF}/security" PATH_BORG_CONF_KEYS="${PATH_BORG_CONF}/keys" @@ -15,45 +17,12 @@ servername_to_location() { printf 'ssh://plom@%s/./borg' "$1"; } location_to_servername() { echo "$1" | cut -d'/' -f3 | cut -d'@' -f2; } path_repo_location() { printf '%s' "${PATH_BORG_CONF_SECURITY}/${1}/location"; } -_print_usage() { - echo "Usage: ${BIN_NAME} COMMAND" - echo "Usage: ${BIN_NAME} COMMAND TARGET" - echo "Wrapper around certain borgbackup usages.\n" - echo "Available commands:\n" - - _var_of() { eval printf '"%s"' '"${'"${1}${2}"'}"'; } - _build_index() { - _ARGS="$(_var_of 'HELP_ARGS_' ${_CMD})" - if [ ! -z "${_ARGS}" ]; then - _ARGS=" ${_ARGS}" - fi - _INDEX=" ${_CMD}${_ARGS}" - _LEN_INDEX=$(echo -n "${_INDEX}" | wc -c) - } - _MAX_LEN_INDEX=0 - for _CMD in ${COMMANDS}; do - _build_index - if [ "${_LEN_INDEX}" -gt "${_MAX_LEN_INDEX}" ]; then - _MAX_LEN_INDEX="${_LEN_INDEX}" - fi - done - for _CMD in ${COMMANDS}; do - _build_index - printf '%s' "${_INDEX}" - _LEN_GAP=$(calc "${_MAX_LEN_INDEX} - ${_LEN_INDEX} + 2") - for _ in $(seq 1 ${_LEN_GAP}); do - printf ' ' - done - printf '%s\n' "$(_var_of 'HELP_DESC_' ${_CMD})" - done -} - # exits error_exit() { abort "Aborting due to $1"; } error_exit_with_usage() { error_exit "${1}\n\n$(_print_usage)"; } # commands -COMMANDS='backup_keys claim help keys orgpull' +USAGE_INDICES='backup_keys claim help keys orgpull' _check_args_n() { _N_MAX_ARGS="$1" @@ -70,8 +39,8 @@ _check_args_n() { _id_from_file() { head -1 "$1" | cut -d' ' -f2; } -HELP_ARGS_backup_keys='PATH' -HELP_DESC_backup_keys="copy known keys to PATH/, with their repos' server names as filenames" +USAGE_ARGS_backup_keys='PATH' +USAGE_DESC_backup_keys="copy known keys to PATH/, with their repos' server names as filenames" CMDFNC_backup_keys() { _check_args_n 1 "$@" _PATH_TARGET_DIR=$(realpath "$1") @@ -95,8 +64,8 @@ CMDFNC_backup_keys() { cd - > /dev/null } -HELP_ARGS_claim='PATH' -HELP_DESC_claim="register file of PATH as key to repo at \"$(servername_to_location SERVERNAME)\", with SERVERNAME the filename portion of PATH" +USAGE_ARGS_claim='PATH' +USAGE_DESC_claim="register file of PATH as key to repo at \"$(servername_to_location SERVERNAME)\", with SERVERNAME the filename portion of PATH" CMDFNC_claim() { _check_args_n 1 "$@" _PATH_SOURCE="$1" @@ -126,13 +95,13 @@ CMDFNC_claim() { chmod a-rwx,u+rw "${_PATH_TARGET_KEY}" "${_PATH_TARGET_LOCATION}" } -HELP_DESC_help='print this help and exit' +USAGE_DESC_help='print this help and exit' CMDFNC_help() { _check_args_n 0 "$@" _print_usage } -HELP_DESC_keys='list known repos in ID, key filename, and alleged location' +USAGE_DESC_keys='list known repos in ID, key filename, and alleged location' CMDFNC_keys() { _check_args_n 0 "$@" _exit_ok() { @@ -160,7 +129,7 @@ CMDFNC_keys() { done } -HELP_DESC_orgpull='pull most recent org directory available in repos' +USAGE_DESC_orgpull='pull most recent org directory available in repos' CMDFNC_orgpull() { _check_args_n 0 "$@" _NAME_ARCHIVE=orgdir @@ -215,7 +184,7 @@ CMDFNC_orgpull() { if [ "$#" -lt 1 ]; then error_exit_with_usage 'missing command.' fi -for CMD in ${COMMANDS}; do +for CMD in ${USAGE_INDICES}; do if [ "$1" = "${CMD}" ]; then break fi diff --git a/testing/home/desktop/.local/bin/vol b/testing/home/desktop/.local/bin/vol index 7081e12..cbb9ea7 100755 --- a/testing/home/desktop/.local/bin/vol +++ b/testing/home/desktop/.local/bin/vol @@ -3,20 +3,19 @@ set -e cd $(dirname "$0") . lib/abort . lib/audio_dev_is_mute +. lib/print_usage MAX_LOUDNESS=150 -BIN_NAME="$(basename $0)" FLAG_HELP_SHORT='-h' FLAG_HELP_LONG='--help' -print_usage() { - echo "Usage: ${BIN_NAME} [${FLAG_HELP_SHORT}] [${FLAG_HELP_LONG}] [LOUDNESS]" - echo "Set audio volume, or (if called without any arguments) toggle audio device muteness.\n" - echo "Available arguments (of which at most one may be used):\n" - echo " LOUDNESS\taudio volume to set (as percentage, must be integer)" - echo " ${FLAG_HELP_SHORT}, ${FLAG_HELP_LONG}\tdisplay this help and exit" -} +USAGE_DESCRIPTION='Set audio volume, or (if called without any arguments) toggle audio device muteness.' +USAGE_LINES='[COMMAND]\nLOUDNESS' +USAGE_INDICES='LOUDNESS _help' +USAGE_DESC_LOUDNESS='audio volume to set (as percentage, must be integer)' +USAGE_DESC__help='display this help and exit' +USAGE_NAME__help="${FLAG_HELP_SHORT}, ${FLAG_HELP_LONG}" error_exit() { abort "Aborting due to $1\n\n$(print_usage)"; } diff --git a/testing/home/desktop/.plomlib.sh/print_usage b/testing/home/desktop/.plomlib.sh/print_usage new file mode 100644 index 0000000..1c0958d --- /dev/null +++ b/testing/home/desktop/.plomlib.sh/print_usage @@ -0,0 +1,40 @@ +USAGE_BIN_NAME="$(basename $0)" + +print_usage() { + echo 'Usage:' + echo "${USAGE_LINES}" | while read _LINE; do + echo " ${USAGE_BIN_NAME} ${_LINE}" + done + echo "\n${USAGE_DESCRIPTION}\n" + echo "Available arguments:\n" + _var_of() { eval printf '"%s"' '"${'"${1}${2}"'}"'; } + _build_index() { + _ARGS="$(_var_of 'USAGE_ARGS_' ${_IDX})" + if [ ! -z "${_ARGS}" ]; then + _ARGS=" ${_ARGS}" + fi + _NAME="${_IDX}" + _ALT_NAME="$(_var_of 'USAGE_NAME_' ${_IDX})" + if [ ! -z "${_ALT_NAME}" ]; then + _NAME="${_ALT_NAME}" + fi + _KEY=" ${_NAME}${_ARGS}" + _LEN_KEY=$(echo -n "${_KEY}" | wc -c) + } + _MAX_LEN_KEY=0 + for _IDX in ${USAGE_INDICES}; do + _build_index + if [ "${_LEN_KEY}" -gt "${_MAX_LEN_KEY}" ]; then + _MAX_LEN_KEY="${_LEN_KEY}" + fi + done + for _IDX in ${USAGE_INDICES}; do + _build_index + printf '%s' "${_KEY}" + _LEN_GAP=$(calc "${_MAX_LEN_KEY} - ${_LEN_KEY} + 4") + for _ in $(seq 1 ${_LEN_GAP}); do + printf ' ' + done + printf '%s\n' "$(_var_of 'USAGE_DESC_' ${_IDX})" + done +} -- 2.30.2