Last active
October 31, 2023 08:38
-
-
Save mrantivirus/24d5966ba5c6b161e5ecd8cd45c9f9c3 to your computer and use it in GitHub Desktop.
Bash script to import dotenv files into 1Password.
This file contains 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 | |
# Function to display help information | |
function display_help() { | |
echo "Usage: $0 [dotenv_path] [item_name] [-v vault_name] [-s section_name]" | |
echo "" | |
echo "dotenv_path: Path to the dotenv file." | |
echo "item_name: Name of the 1Password item." | |
echo "Optional Parameters:" | |
echo " -v vault_name: Name of the vault in 1Password." | |
echo " -s section_name: Section name for the dotenv key-values." | |
exit 1 | |
} | |
# Check if help flag is set | |
if [[ "$1" == "-h" ]] || [[ "$1" == "--help" ]]; then | |
display_help | |
fi | |
# Check arguments | |
if [ "$#" -lt 2 ]; then | |
echo "Error: Missing required arguments." | |
display_help | |
fi | |
DOTENV_PATH=$1 | |
ITEM_NAME=$2 | |
shift 2 | |
VAULT="" | |
SECTION="" | |
while getopts ":v:s:" opt; do | |
case $opt in | |
v) | |
VAULT="$OPTARG" | |
;; | |
s) | |
SECTION="$OPTARG." | |
;; | |
\?) | |
echo "Invalid option: -$OPTARG" >&2 | |
display_help | |
;; | |
:) | |
echo "Option -$OPTARG requires an argument." >&2 | |
display_help | |
;; | |
esac | |
done | |
# Check if item exists in the vault | |
if [[ ! -z "$VAULT" ]]; then | |
op item list --categories API\ Credential --vault "$VAULT" | grep -q "\"name\":\"$ITEM_NAME\"" | |
ITEM_EXISTS=$? | |
else | |
op item list --categories API\ Credential | grep -q "\"name\":\"$ITEM_NAME\"" | |
ITEM_EXISTS=$? | |
fi | |
# If the item does not exist, create it | |
if [ "$ITEM_EXISTS" -eq 1 ]; then | |
op item create --category="API Credential" --title="$ITEM_NAME" ${VAULT:+--vault=$VAULT} | |
fi | |
# Read dotenv file and update 1Password item | |
while IFS='=' read -r key value || [[ -n "$key" ]]; do | |
# Ignore comments | |
if [[ ! $key =~ ^# ]]; then | |
# Edit the existing item | |
op item edit "$ITEM_NAME" "${SECTION}${key}[text]=$value" | |
fi | |
done < "$DOTENV_PATH" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
1Password dotenvImporter
This script, 1password_import_dotenv.sh, imports key-value pairs from a dotenv file and populates a 1Password item with those values. It can optionally specify a vault and a section within the item.
Prerequisites
Usage
bash ./1password_import_dotenv.sh [dotenv_path] [item_name] [-v vault_name] [-s section_name]
Parameters:
Examples:
bash ./1password_import_dotenv.sh ./local.env DatabaseCreds
2. To import key-value pairs from ./local.env to an item named "DatabaseCreds" in a vault named "WorkVault", within a section named "Database":
bash ./1password_import_dotenv.sh ./local.env DatabaseCreds -v WorkVault -s Database
Notes