From 002982c4ac955fef477becfe1a0ef7ac867e6795 Mon Sep 17 00:00:00 2001 From: LeeW Date: Mon, 3 Feb 2025 10:34:17 +0100 Subject: [PATCH] refactor: args --- bin/update_sops.sh | 28 +++++++++++++++++++++++++--- example/cmd_sops.sh | 3 ++- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/bin/update_sops.sh b/bin/update_sops.sh index 473e765..f218dfe 100755 --- a/bin/update_sops.sh +++ b/bin/update_sops.sh @@ -2,11 +2,33 @@ # Purpose: manage .sops.yaml based on gpg keys in the same dir _and_ verify correct configuration set -euo pipefail -keyfiles_dir="$(realpath "${1?"E: specify dir containing keyfiles"}")"; shift; -sops_config_dir="$(realpath "${1?"E: specify dir containing .sops.yaml"}")"; shift; -sops_config="${sops_config_dir}/.sops.yaml" +# OPTIONS AND ARGPARSING +# assume location of script as running directly from repo with keys (instead of as a standalone packaged tool) +keyfiles_dir="$(realpath $(dirname "${BASH_SOURCE[0]}")/..)" +# assume location of secrets config file in pwd +sops_config_dir="${PWD}" + +while (( $# >= 1 ));do + cur="${1}"; + case $cur in + -k|--key|--keyfiles) keyfiles_dir="${2}"; shift ;; + -c|--config_dir) sops_config_dir="${2}"; shift ;; + esac + shift; +done secrets_file="${1:-0}" +keyfiles_dir="$(realpath "${keyfiles_dir}")" +test -d "${keyfiles_dir}" || (echo "E: specify dir containing keyfiles; invalid dir: '${keyfiles_dir}'" && exit 1) +sops_config_dir="$(realpath "${sops_config_dir}")" +test -d "${sops_config_dir}" || (echo "E: specify dir containing .sops.yaml, invalid dir: '${sops_config_dir}'" && exit 1) +sops_config="${sops_config_dir}/.sops.yaml" +test -e "${sops_config}" || (echo "E: could not locate .sops.yaml, tried ${sops_config}" && exit 1) +if [[ "${secrets_file}" != "0" ]]; then + test -e "${secrets_file}" || (echo "E: could not locate .sops.yaml, tried ${secrets_file}" && exit 1) +fi +# /OPTIONS AND ARGPARSING + function fn_extract_fpr(){ gpgkeyfile=$1;shift; # fingerprint diff --git a/example/cmd_sops.sh b/example/cmd_sops.sh index 660fd2d..41e401b 100755 --- a/example/cmd_sops.sh +++ b/example/cmd_sops.sh @@ -10,4 +10,5 @@ secrets_file="mock_secrets.yaml" test -e "${secrets_file}" || (yq -n '.demo.credentials.secret = "hunter2"' > "${secrets_file}" && sops -e -i "${secrets_file}" ) set -x -../bin/update_sops.sh "${keys_dir}" "${sops_cfg_dir}" "${secrets_file}" +# ../bin/update_sops.sh -k "${keys_dir}" -c "${sops_cfg_dir}" "${secrets_file}" +../bin/update_sops.sh "${secrets_file}"