#!/bin/zsh

set -e

trap 'echo {} ; exit 0' ERR

add() {
        gopass insert -f docker/$serverurl <<END
$secret
login: $login
END
}

case $1 in
    get)
        read serverurl || true
        [[ -n $serverurl ]]
        pass="$(gopass show --password docker/$serverurl)"
        login="$(gopass show --password docker/$serverurl login)"
        jo ServerURL=$serverurl Username=$login Secret=$pass
        exit ;;
    store)
        jq '.Username+"|"+.Secret+"|"+.ServerURL' | IFS='|' read login secret serverurl
        add
        exit ;;
    erase)
        read serverurl
        [[ -z $serverurl ]] && exit 1
        gopass delete -f docker/$serverurl
        exit ;;
    list)
        gopass ls --flat -s docker | jq -Rn '[inputs]'
        exit ;;
esac

echo "get|list|store|erase"
