Skip to content

Instantly share code, notes, and snippets.

@mkroman
Last active June 2, 2022 02:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mkroman/1b6c71829eff1335d0fe2e4f71acf1aa to your computer and use it in GitHub Desktop.
Save mkroman/1b6c71829eff1335d0fe2e4f71acf1aa to your computer and use it in GitHub Desktop.
Prepare a shell environment with help from a 1Password vault
# Prepare a shell environment with help from a 1Password vault
#
# Prepares a shell environment by reading the `script` field from an item with
# the given NAME inside the `Environments` vault and then evaluating it in the
# current shell after injecting 1Password references.
#
# Usage:
#
# op-env <NAME>
#
# Example:
#
# op-env npm
#
# The `script` field for the `hello-world` item in the `Environments` vault:
#
# export GITHUB_ACTOR="{{ op://${vault}/GitHub/username }}"
# export GITHUB_TOKEN="{{ op://${vault}/GitHub/Personal Access Tokens/read_packages }}"
op-env() {
local name="${1}"
local vault="Private"
# Read the template from the Environments vault
local tpl=
tpl="$(op read -n "op://Environments/${name}/script")"
[ $? -ne 0 ] && return
# Inject variables and evaluate 1Password references
local script=
script="$(echo -n "${tpl}" | vault="${vault}" env_name="${name}" op inject --cache)"
[ $? -ne 0 ] && return
# Print and evaluate the final script
echo "${script}"
eval "${script}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment