Skip to content

Commit

Permalink
Merge pull request #21 from LoganBarnett/add-agenix-command-show-trace
Browse files Browse the repository at this point in the history
add --show-trace to agenix CLI
  • Loading branch information
oddlama authored May 29, 2024
2 parents 8cf7384 + 439129c commit 314a8de
Showing 1 changed file with 45 additions and 16 deletions.
61 changes: 45 additions & 16 deletions nix/package.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@ writeShellScriptBin "agenix" ''
function die() { echo "error: $*" >&2; exit 1; }
function show_help() {
echo 'Usage: agenix [COMMAND]'
echo 'Usage: agenix <OPTIONS> [COMMAND]'
echo "Edit, generate or rekey secrets for agenix."
echo "Add help or --help to a subcommand to view a command specific help."
echo ""
echo 'COMMANDS:'
echo ' rekey Re-encrypts secrets for hosts that require them.'
echo ' edit Create/edit age secret files with $EDITOR and your master identity'
echo ' generate Automatically generates secrets that have generators'
echo ""
echo 'OPTIONS:'
echo ' --show-trace Show the trace for agenix-rekey. This must be provided before the'
echo ' subcommand or it will be provided to the subcommand.'
}
USER_GIT_TOPLEVEL=$(realpath -e "$(git rev-parse --show-toplevel 2>/dev/null || pwd)") \
Expand All @@ -38,19 +42,44 @@ writeShellScriptBin "agenix" ''
exit 1
}
case "$1" in
"help"|"--help"|"-help"|"-h")
show_help
exit 1
;;
${lib.concatStringsSep "|" allApps})
APP=$1
shift
echo "Collecting information about hosts. This may take a while..."
exec nix run .#agenix-rekey.${lib.escapeShellArg stdenv.hostPlatform.system}."$APP" -- "$@"
;;
*) die "Unknown command: $1" ;;
esac
APP=""
SHOW_TRACE_ARG=""
# Various Bash versions treat empty arrays as unset, which then trigger
# unbound variable errors.
PASS_THRU_ARGS=()
while [[ $# -gt 0 ]]; do
case "$1" in
"help"|"--help"|"-help"|"-h")
show_help
exit 1
;;
"--show-trace")
# It is potentially desirable to use --show-trace in the subcommand as
# well as this command. To do so, the --show-trace argument must be
# provided before (agenix) or after (subcommand) to indicate which one
# is to be used. We account for this here.
if [[ "$APP" == "" ]]; then
SHOW_TRACE_ARG='--show-trace'
else
PASS_THRU_ARGS+=('--show-trace')
fi
shift
;;
${lib.concatStringsSep "|" allApps})
APP="$1"
shift
;;
*)
PASS_THRU_ARGS+=("$1")
shift
;;
esac
done
if [[ "$APP" == "" ]]; then
die "Error: No app provided. Exiting."
fi
echo "Collecting information about hosts. This may take a while..."
exec nix run $SHOW_TRACE_ARG \
.#agenix-rekey.${lib.escapeShellArg stdenv.hostPlatform.system}."$APP" \
-- "''${PASS_THRU_ARGS[@]}"
''

0 comments on commit 314a8de

Please sign in to comment.