#!/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