From 8fd025653ac1e34bd188186268a08a79ebd2f28f Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Tue, 30 Mar 2021 22:21:42 +0200 Subject: [PATCH] scripts: Improve nomie credentials options Added option to input nomie api key either directly as an environment variable, or to pass a command (also through env var) to nomie which it will call to get the api key. So, if you have e.g. pass as password manager running, you can just do `NOMIE_API_KEY_CMD="pass show path/to/key/file"` and it will use the returned credentials. --- scripts/.config/sh/env.d/nomie-api-key.sh | 1 + scripts/.local/bin/nomie | 43 ++++++++++++++++++++--- 2 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 scripts/.config/sh/env.d/nomie-api-key.sh diff --git a/scripts/.config/sh/env.d/nomie-api-key.sh b/scripts/.config/sh/env.d/nomie-api-key.sh new file mode 100644 index 0000000..b681bb4 --- /dev/null +++ b/scripts/.config/sh/env.d/nomie-api-key.sh @@ -0,0 +1 @@ +export NOMIE_API_KEY_CMD="pass show websites/nomie.app/apikey" diff --git a/scripts/.local/bin/nomie b/scripts/.local/bin/nomie index 20e1b31..6eff3e5 100755 --- a/scripts/.local/bin/nomie +++ b/scripts/.local/bin/nomie @@ -2,10 +2,45 @@ # this script logs a new entry in nomie - https://nomie.app/ - an open source habit tracker and journaling app # It will very simply take the text input and pass it on. -sendtonomie() { - req="{\"note\":\"$*\",\"api_key\":\"$(pass show websites/nomie.app/apikey)\"}" - echo "$req" - curl -s -H "Content-Type: application/json" --compressed --data "$req" https://nomieapi.com/log +getapikey() { + if [ -n "$NOMIE_API_KEY_CMD" ]; then + # shellcheck disable=2005 + echo "$($NOMIE_API_KEY_CMD)" + elif [ -n "$NOMIE_API_KEY" ]; then + echo "$NOMIE_API_KEY" + else + exit 1 + fi } +sendtonomie() { + apikey=$(getapikey) + if [ "$?" -eq 1 ]; then + echo "No api key found." + exit 1 + fi + req="{\"note\":\"$*\",\"api_key\":\"$apikey\"}" + echo "$req" + curl -s -H "Content-Type: application/json" --compressed --data "$req" https://nomieapi.com/log +} + +usage() { + printf """ +simple nomie logging script. + +usage: nomie \"note content to upload to nomie with #tags and +mentions\" + +Will upload the content passed in on to nomie, using your api key. +Set you api key through the \"NOMIE_API_KEY=\" env var, +or set a function to return the api key from \"NOMIE_API_KEY_CMD=\" which will +be used preferentially. + """ +} + +# print super basic help +if [ "$1" = "-h" ] || [ "$1" = "--help" ] || [ "$#" -eq 0 ]; then + usage + exit 0 +fi + sendtonomie "$@"