Skip to content

Instantly share code, notes, and snippets.

@justsml
Created May 7, 2025 23:43
Show Gist options
  • Select an option

  • Save justsml/fdc439aa48d3f0176125df965a7b5193 to your computer and use it in GitHub Desktop.

Select an option

Save justsml/fdc439aa48d3f0176125df965a7b5193 to your computer and use it in GitHub Desktop.

Revisions

  1. justsml created this gist May 7, 2025.
    34 changes: 34 additions & 0 deletions .env.load.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    #!/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