secrets/main.sh (view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 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}" "$@"
|