diff options
Diffstat (limited to 'secrets/main.sh')
| -rwxr-xr-x | secrets/main.sh | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/secrets/main.sh b/secrets/main.sh new file mode 100755 index 0000000..89bbea3 --- /dev/null +++ b/secrets/main.sh @@ -0,0 +1,69 @@ +#!/bin/bash + +set -euo pipefail + +this_dir="$(cd "$(dirname "$(realpath "$0")")" && pwd)" + +# list all commands, name only +cmds() { + find "${this_dir}" -maxdepth 1 -type f -name '*.sh' -print0 \ + | xargs -0 -n 1 basename \ + | sed 's/\.sh$//' \ + | grep -Ev '^(main|common)$' \ + | sort +} + +# list all commands, name and description +cmd_descriptions() { + for cmd in $(cmds); do + local cmd_sh="${this_dir}/${cmd}.sh" + if [[ -x "${cmd_sh}" ]]; then + local description + description="$(bash "${cmd_sh}" -h 2>/dev/null | head -1 | sed 's/^[^:]*: *//')" + if [[ -n "${description}" ]]; then + printf "%-15s %s\n" "${cmd}" "${description}" + else + printf "%-15s\n" "${cmd}" + fi + fi + done +} + +if [[ "$#" -gt 0 && ("$1" == "-h" || "$1" == "--help") ]]; then + cat <<EOF +szecret: Secret management using age encryption + +Usage: + szecret -h|--help + szecret <command> -h|--help + szecret <command> [args...] + +Available commands: + +$(cmd_descriptions | sed 's/^/ /') +EOF + exit 0 +fi + +# check number of arguments > 1, error and exit if not +# extract first argument, call script with same name using remaining arguments +if [ $# -lt 1 ]; then + >&2 cat <<EOF +ERROR: no command specified. Available: + +$(cmd_descriptions | sed 's/^/ /') +EOF + exit 1 +fi + +cmd="$1" +cmd_sh="${this_dir}/${cmd}.sh" +shift + +if [[ ! -e "${cmd_sh}" ]]; then + >&2 echo "ERROR: no command named ${cmd}. Available: $(cmds | paste -sd ',' -)" + exit 1 +fi + +exec bash "${cmd_sh}" "$@" + |
