mock-gpg (2035B)
1 #!/bin/sh 2 3 set -Cue 4 5 die() { 6 printf '%s\n' "$*" >&2 7 exit 1 8 } 9 10 check_eq() { 11 [ "$1" = "$2" ] && return 0 12 shift 2 13 die "$@" 14 } 15 16 case "$1" in 17 -e) 18 shift 19 MOCK_AGE_OUT="$(@mktemp -- "$(@dirname "$2")/mock-gpg-encrypt.XXXXXXX")" 20 DEST='-' 21 while [ $# -gt 0 ]; do 22 case "$1" in 23 -r) 24 printf 'gpgRecipient:%s\n' "$2" >>"${MOCK_AGE_OUT}" 25 shift 2 ;; 26 -o) 27 DEST="$2" 28 shift 2 29 break ;; 30 *) 31 die "Unexpected arguments to gpg -e [...] $*\n" 32 ;; 33 esac 34 done 35 if [ $# -lt 4 ] || [ $# -gt 5 ]; then 36 die "Unexpected arguments to gpg -e [...] $*\n" 37 fi 38 check_eq "$1" '--quiet' "Unexpected gpg -e \$1: \"$1\"" 39 check_eq "$2" '--yes' "Unexpected gpg -e \$2: \"$2\"" 40 check_eq "$3" '--compress-algo=none' "Unexpected gpg -e \$3: \"$3\"" 41 check_eq "$4" '--no-encrypt-to' "Unexpected gpg -e \$4: \"$4\"" 42 if [ $# -eq 5 ]; then 43 @sed -- 's/^/gpg:/' "$5" >>"${MOCK_AGE_OUT}" 44 else 45 @sed 's/^/gpg:/' >>"${MOCK_AGE_OUT}" 46 fi 47 if [ "${DEST}" = '-' ]; then 48 @cat -- "${MOCK_AGE_OUT}" 49 @rm -f -- "${MOCK_AGE_OUT}" 50 else 51 @mv -f -- "${MOCK_AGE_OUT}" "${DEST}" 52 fi 53 ;; 54 -d) 55 shift 56 OUTPUT='-' 57 if [ "$1" = '-o' ]; then 58 OUTPUT="$2" 59 shift 2 60 fi 61 check_eq "$1" '--quiet' "Unexpected gpg -d \$1: \"$1\"" 62 check_eq "$2" '--yes' "Unexpected gpg -d \$2: \"$2\"" 63 check_eq "$3" '--compress-algo=none' "Unexpected gpg -d \$3: \"$3\"" 64 check_eq "$4" '--no-encrypt-to' "Unexpected gpg -d \$4: \"$4\"" 65 shift 4 66 [ "${1:-}" = '--' ] && shift 67 @grep -v '^gpg' -- "$1" >&2 && die "Bad encrypted file \"$1\"" 68 if [ "${OUTPUT}" = '-' ]; then 69 @sed -n -- 's/^gpg://p' "$1" 70 else 71 @sed -n -- 's/^gpg://p' "$1" >|"${OUTPUT}" 72 fi 73 ;; 74 --list-config) 75 [ $# -eq 2 ] || die "Unexpected arguments to gpg $*\n" 76 check_eq "$2" '--with-colons' "Unexpected gpg --list-config \$2: \"$2\"" 77 ;; 78 --list-keys) 79 check_eq "$2" '--with-colons' "Unexpected gpg --list-keys \$2: \"$2\"" 80 printf 'sub::::%s:::::::e:\n' "$@" 81 ;; 82 *) 83 die "Unexpected gpg \$1: \"$1\"" 84 ;; 85 esac