blob: bc462376549f918935efff568ffd86720c558b9f (
plain) (
blame)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
#!/bin/sh
set -eu
usage() {
cat <<-'EOF'
Usage:
grun [-r RECIPIENT] FILENAME -- COMMAND...
grun -h
EOF
}
help() {
cat <<-'EOF'
Options:
-r RECIPIENT the recipient to encrypt to. Can be provided
multiple times for multiple recipients.
-h, --help show this message
COMMAND A command to be executed, that accepts input
in STDIN and emits result in STDOUT, and emits
errors as non-zero return codes.
FILENAME The GPG-encrypted file to be processed. If it
doesn't exist yet, it will be created.
Edit the encripted FILENAME in a pipeline, using COMMAND to
modify the content. If COMMAND emits a non-zero return code,
the file is left unmodified.
Examples:
Edit "secrets.txt.gpg" using `vipe` and the default recipient:
$ grun secrets.txt.gpg -- vipe
Delete lines containing "TODO" in todos.gpg for specific keys:
$ grun -r ABC123DEF321 todos.gpg -- sed '/TODO/d'
EOF
}
for flag in "$@"; do
case "$flag" in
(--)
break
;;
(--help)
usage
help
exit
;;
(*)
;;
esac
done
while getopts 'rh' flag; do
case "$flag" in
(r)
RECIPIENTS_FLAG="${RECIPIENTS_FLAG:-} -r $OPTARG"
;;
(h)
usage
help
exit
;;
(*)
usage >&2
exit 2
;;
esac
done
shift $((OPTIND - 1))
FILENAME="${1:-}"
eval "$(assert-arg -- "$FILENAME" 'FILENAME')"
shift
if [ "${1:-}" != '--' ]; then
printf 'Missing "--" separator\n\n' >&2
usage >&2
exit 2
fi
shift
eval "$(assert-arg -- "${1:-}" 'COMMAND')"
if [ ! -e "$FILENAME" ]; then
OUT="$(printf '' | "$@")"
else
OUT="$(gpg -dq "$FILENAME" | "$@")"
fi
# GPG recipients can't contain spaces:
# shellcheck disable=2086
printf '%s\n' "$OUT" | gpg -e ${RECIPIENTS_FLAG:--r eu@euandre.org} | sponge "$FILENAME"
|