pashage

Yet Another Opinionated Re-engineering of the Unix Password Store
git clone https://git.instinctive.eu/pashage.git
Log | Files | Refs | README | LICENSE

mock-age (1478B)


      1 #!/bin/sh
      2 
      3 set -Cue
      4 
      5 die() {
      6 	printf '%s\n' "$*" >&2
      7 	exit 1
      8 }
      9 
     10 case "$1" in
     11     -e)
     12 	shift
     13 	MOCK_AGE_OUT="$(@mktemp -- "$(@dirname "$2")/mock-age-encrypt.XXXXXXX")"
     14 	DEST='-'
     15 	while [ $# -gt 0 ]; do
     16 		case "$1" in
     17 		    -R|-i)
     18 			@sed -- 's/^/ageRecipient:/' "$2" >>"${MOCK_AGE_OUT}"
     19 			shift 2 ;;
     20 		    -r)
     21 			printf 'ageRecipient:%s\n' "$2" >>"${MOCK_AGE_OUT}"
     22 			shift 2 ;;
     23 		    -o)
     24 			DEST="$2"
     25 			shift 2
     26 			break ;;
     27 		    *)
     28 			die 'Unexpected age -e [...] %s\n' "$*"
     29 			;;
     30 		esac
     31 	done
     32 
     33 	if [ $# -gt 1 ]; then
     34 		die "Unexpected arguments to age -e [...] $*\n"
     35 	elif [ $# -eq 1 ]; then
     36 		@sed -- 's/^/age:/' "$1" >>"${MOCK_AGE_OUT}"
     37 	else
     38 		@sed 's/^/age:/' >>"${MOCK_AGE_OUT}"
     39 	fi
     40 
     41 	if [ "${DEST}" = '-' ]; then
     42 		@cat -- "${MOCK_AGE_OUT}"
     43 		@rm -f -- "${MOCK_AGE_OUT}"
     44 	else
     45 		@mv -f -- "${MOCK_AGE_OUT}" "${DEST}"
     46 	fi
     47 	;;
     48     -d)
     49 	shift
     50 	DEST='-'
     51 	if [ "$1" = '-o' ]; then
     52 		DEST="$2"
     53 		shift 2
     54 	fi
     55 	[ "$1" = '-i' ] || die "Unexpected age -d arg: \"$1\""
     56 	MOCK_AGE_ID_SRC="$2"
     57 	MOCK_AGE_ID="$(@cat -- "$2")"
     58 	shift 2
     59 	[ "$1" = '--' ] && shift
     60 	[ $# -eq 1 ] || die "Too many age -d arguments ($#): $*"
     61 	@grep -v -- '^age' "$1" >&2 && die "Bad encrypted file \"$1\""
     62 	if ! @grep -qFx -- "ageRecipient:${MOCK_AGE_ID}" "$1"; then
     63 		die "Bad identity \"${MOCK_AGE_ID_SRC}\": ${MOCK_AGE_ID}"
     64 		exit 1
     65 	fi
     66 	if [ "${DEST}" = '-' ]; then
     67 		@sed -n -- 's/^age://p' "$1"
     68 	else
     69 		@sed -n -- 's/^age://p' "$1" >|"${DEST}"
     70 	fi
     71 	;;
     72     *)
     73 	die "Unexpected age \$1: \"$1\""
     74 	;;
     75 esac