@@ -14,6 +14,7 @@ function show_help () {
1414 # shellcheck disable=SC2016
1515 echo ' -e, --edit FILE edits FILE using $EDITOR'
1616 echo ' -r, --rekey re-encrypts all secrets with specified recipients'
17+ echo ' -d, --decrypt FILE decrypts FILE to STDOUT'
1718 echo ' -i, --identity identity to use when decrypting'
1819 echo ' -v, --verbose verbose output'
1920 echo ' '
@@ -45,6 +46,7 @@ function err() {
4546test $# -eq 0 && (show_help && exit 1)
4647
4748REKEY=0
49+ DECRYPT_ONLY=0
4850DEFAULT_DECRYPT=(--decrypt)
4951
5052while test $# -gt 0; do
@@ -77,6 +79,17 @@ while test $# -gt 0; do
7779 shift
7880 REKEY=1
7981 ;;
82+ -d|--decrypt)
83+ shift
84+ DECRYPT_ONLY=1
85+ if test $# -gt 0; then
86+ export FILE=$1
87+ else
88+ echo " no FILE specified"
89+ exit 1
90+ fi
91+ shift
92+ ;;
8093 -v|--verbose)
8194 shift
8295 set -x
@@ -89,7 +102,6 @@ while test $# -gt 0; do
89102done
90103
91104RULES=${RULES:- ./ secrets.nix}
92-
93105function cleanup {
94106 if [ -n " ${CLEARTEXT_DIR+x} " ]
95107 then
@@ -102,10 +114,13 @@ function cleanup {
102114}
103115trap " cleanup" 0 2 3 15
104116
105- function edit {
106- FILE= $ 1
107- KEYS= $( (@nixInstantiate@ --eval -E " (let rules = import $RULES ; in builtins.concatStringsSep \" \n \" rules. \" $FILE \" .publicKeys) " | @sedBin@ ' s/"//g ' | @sedBin@ ' s/\\n/\n/g ' ) | @sedBin@ ' /^$/d ' || exit 1 )
117+ function keys {
118+ (@nixInstantiate@ --eval -E " (let rules = import $RULES ; in builtins.concatStringsSep \" \n \" rules. \" $1 \" .publicKeys) " | @sedBin@ ' s/"//g ' | @sedBin@ ' s/\\n/\n/g ' ) | @sedBin@ ' /^$/d ' || exit 1
119+ }
108120
121+ function decrypt {
122+ FILE=$1
123+ KEYS=$2
109124 if [ -z " $KEYS " ]
110125 then
111126 err " There is no rule for $FILE in $RULES ."
@@ -132,6 +147,12 @@ function edit {
132147 @ageBin@ " ${DECRYPT[@]} " || exit 1
133148 cp " $CLEARTEXT_FILE " " $CLEARTEXT_FILE .before"
134149 fi
150+ }
151+
152+ function edit {
153+ FILE=$1
154+ KEYS=$( keys " $FILE " ) || exit 1
155+ decrypt " $FILE " " $KEYS " || exit 1
135156
136157 [ -t 0 ] || EDITOR=' cp /dev/stdin'
137158
@@ -160,6 +181,14 @@ function edit {
160181 mv -f " $REENCRYPTED_FILE " " $1 "
161182}
162183
184+ function decrypt_only {
185+ FILE=$1
186+ KEYS=$( keys " $FILE " ) || exit 1
187+ decrypt " $FILE " " $KEYS "
188+ printf " %s" " $( < " ${CLEARTEXT_FILE} " ) "
189+ cleanup
190+ }
191+
163192function rekey {
164193 FILES=$( (@nixInstantiate@ --eval -E " (let rules = import $RULES ; in builtins.concatStringsSep \" \n\" (builtins.attrNames rules))" | @sedBin@ ' s/"//g' | @sedBin@ ' s/\\n/\n/g' ) || exit 1)
165194
@@ -172,4 +201,5 @@ function rekey {
172201}
173202
174203[ $REKEY -eq 1 ] && rekey && exit 0
204+ [ $DECRYPT_ONLY -eq 1 ] && decrypt_only " ${FILE} " && exit 0
175205edit " $FILE " && cleanup && exit 0
0 commit comments