This repository has been archived on 2022-03-07. You can view files and clone it, but cannot push or open issues or pull requests.
hotpocket/scripts/polmake.sh
2022-02-20 14:43:11 +00:00

59 lines
1.2 KiB
Bash
Executable File

#!/usr/bin/env bash
set -u
if [ -z "$EDITOR" ]; then echo "polmake: No EDITOR set in environment. Please set one :)" 1>&2; exit 1; fi
ext=".yaml"
newfile () {
local prev_mask="$(umask)"
umask 0077
local file="$(mktemp --suffix "$ext")"
umask "$prev_mask"
echo "$file"
}
extswap () {
local text="$(cat < /dev/stdin)"
if [ "$ext" == ".yaml" ]; then
echo "$text" | yq .
elif [ "$ext" == ".json" ]; then
echo "$text"
else
echo "polmake: Unsure how to process extension typeof \"$ext\"." 1>&2
exit 1
fi
}
file="$(newfile)"
while :; do
set +u
$EDITOR "$file"
set -u
json="$(cat "$file")"
code="$?"
if [ "$code" != 0 ]; then exit 0; fi
if [ -z "$json" ]; then exit 0; fi
json="$(echo "$json" | extswap)"
echo "$json
Type EDIT to open the editor
Type SIGN to sign this policy
Type QUIT to cancel"
read -p "> " cmd
if [ "$cmd" == "EDIT" ] || [ "$cmd" == "edit" ] || [ "$cmd" == "e" ]; then
continue
elif [ "$cmd" == "SIGN" ] || [ "$cmd" == "sign" ] || [ "$cmd" == "s" ]; then
rm "$file"
read -p "gpgid> " gpgid
./polsign.sh "$gpgid" <<< "$json"
break
else
rm "$file"
break
fi
done