Created
May 7, 2025 23:43
-
-
Save justsml/fdc439aa48d3f0176125df965a7b5193 to your computer and use it in GitHub Desktop.
✨ 1Password secret loader script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -eo pipefail | |
# check for `op` | |
if ! command -v op &> /dev/null; then | |
printf "\nError: op could not be found\n\n" >&2 | |
printf "Please install 1Password CLI: https://developer.1password.com/docs/cli/get-started\n\n" >&2 | |
printf "MacOS+brew: brew install 1password-cli\n\n" >&2 | |
exit 1 | |
fi | |
key_name="${1}" | |
if [ -z "$key_name" ]; then | |
printf "\nError: key_name is required\n\n" >&2 | |
printf "# Env Var Loader (1Password Notes)\n\n" | |
printf "Usage:\n env \$(%s <key_name>) <command>\n\n" "$0" | |
printf "Example:\n env \$(%s \"dev-keys\") sh -c 'echo \$OPENAI_API_KEY'\n\n" "$0" | |
exit 1 | |
fi | |
set +e | |
# Get NOTES item from 1Password | |
op item get "$key_name" --format json --fields notesPlain | \ | |
jq -r .value | \ | |
grep -E -v '^#|^$|^\s' | |
set -e | |
if [ $? -ne 0 ]; then | |
printf "\nError: op item get failed\n\n" >&2 | |
printf "Please check your 1Password CLI setup and run 'op item list' to verify the key_name exists (and is properly quoted in the command.)\n\n" >&2 | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment