Skip to content

Commit f386250

Browse files
committed
exit of sub commands fail; don't re-encrypt if there is no diff; apply some shellcheck suggestions
1 parent b381af0 commit f386250

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

pkgs/agenix.nix

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{writeShellScriptBin, runtimeShell, age} :
22
writeShellScriptBin "agenix" ''
3-
set -euo pipefail
3+
set -Eeuo pipefail
44
55
PACKAGE="agenix"
66
@@ -84,7 +84,7 @@ trap "cleanup" 0 2 3 15
8484
8585
function edit {
8686
FILE=$1
87-
KEYS=$(nix-instantiate --eval -E "(let rules = import $RULES; in builtins.concatStringsSep \"\n\" rules.\"$FILE\".public_keys)" | sed 's/"//g' | sed 's/\\n/\n/g')
87+
KEYS=$((nix-instantiate --eval -E "(let rules = import $RULES; in builtins.concatStringsSep \"\n\" rules.\"$FILE\".public_keys)" | sed 's/"//g' | sed 's/\\n/\n/g') || exit 1)
8888
8989
if [ -z "$KEYS" ]
9090
then
@@ -101,13 +101,21 @@ function edit {
101101
while IFS= read -r key
102102
do
103103
DECRYPT+=(--identity "$key")
104-
done <<<$(find ~/.ssh -maxdepth 1 -type f -not -name "*pub" -not -name "config" -not -name "authorized_keys" -not -name "known_hosts")
104+
done <<<"$((find ~/.ssh -maxdepth 1 -type f -not -name "*pub" -not -name "config" -not -name "authorized_keys" -not -name "known_hosts") || exit 1)"
105105
DECRYPT+=(-o "$CLEARTEXT_FILE" "$FILE")
106-
${age}/bin/age "''${DECRYPT[@]}"
106+
${age}/bin/age "''${DECRYPT[@]}" || exit 1
107+
cp "$CLEARTEXT_FILE" "$CLEARTEXT_FILE.before"
107108
fi
108109
109110
$EDITOR "$CLEARTEXT_FILE"
110111
112+
if [ ! -f "$CLEARTEXT_FILE" ]
113+
then
114+
echo "$FILE wasn't created."
115+
return
116+
fi
117+
[ -f "$FILE" ] && [ "$EDITOR" != ":" ] && diff "$CLEARTEXT_FILE.before" "$CLEARTEXT_FILE" 1>/dev/null && echo "$FILE wasn't changed, skipping re-encryption." && return
118+
111119
ENCRYPT=()
112120
while IFS= read -r key
113121
do
@@ -119,21 +127,22 @@ function edit {
119127
120128
ENCRYPT+=(-o "$REENCRYPTED_FILE")
121129
122-
cat "$CLEARTEXT_FILE" | ${age}/bin/age "''${ENCRYPT[@]}"
130+
${age}/bin/age "''${ENCRYPT[@]}" <"$CLEARTEXT_FILE" || exit 1
123131
124132
mv -f "$REENCRYPTED_FILE" "$1"
125133
}
126134
127135
function rekey {
128-
echo "rekeying..."
129-
FILES=$(nix-instantiate --eval -E "(let rules = import $RULES; in builtins.concatStringsSep \"\n\" (builtins.attrNames rules))" | sed 's/"//g' | sed 's/\\n/\n/g')
136+
FILES=$((nix-instantiate --eval -E "(let rules = import $RULES; in builtins.concatStringsSep \"\n\" (builtins.attrNames rules))" | sed 's/"//g' | sed 's/\\n/\n/g') || exit 1)
130137
131138
for FILE in $FILES
132139
do
133-
EDITOR=: edit $FILE
140+
echo "rekeying $FILE..."
141+
EDITOR=: edit "$FILE"
142+
cleanup
134143
done
135144
}
136145
137146
[ $REKEY -eq 1 ] && rekey && exit 0
138-
edit $FILE && exit 0
147+
edit "$FILE" && cleanup && exit 0
139148
''

0 commit comments

Comments
 (0)