generated from nhcarrigan/template
feat(bash): add env-to-1pass script for importing .env files to 1Password
CI / dependency-pin-check-python (push) Successful in 4s
CI / dependency-pin-check-typescript (push) Successful in 4s
CI / typescript (push) Failing after 4m46s
CI / python (push) Successful in 9m33s
Security Scan and Upload / Security & DefectDojo Upload (push) Successful in 2m45s
CI / dependency-pin-check-python (push) Successful in 4s
CI / dependency-pin-check-typescript (push) Successful in 4s
CI / typescript (push) Failing after 4m46s
CI / python (push) Successful in 9m33s
Security Scan and Upload / Security & DefectDojo Upload (push) Successful in 2m45s
This commit is contained in:
Executable
+46
@@ -0,0 +1,46 @@
|
||||
#!/usr/bin/env bash
|
||||
# env-to-1pass.sh: Create a 1Password item from a .env file
|
||||
# Each KEY=value line becomes a password-type field labelled KEY
|
||||
#
|
||||
# Usage: bash bash/1password/env-to-1pass.sh
|
||||
# Requires: 1Password CLI (op) — https://developer.1password.com/docs/cli
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
# Prompt for item name
|
||||
read -rp "Item name: " item_name
|
||||
[[ -z "$item_name" ]] && { echo "Item name cannot be empty."; exit 1; }
|
||||
|
||||
# Prompt for .env file path
|
||||
read -rp ".env file path: " env_file
|
||||
env_file="${env_file/#\~/$HOME}" # expand ~ if present
|
||||
[[ ! -f "$env_file" ]] && { echo "File not found: $env_file"; exit 1; }
|
||||
|
||||
# Build field arguments from the .env file
|
||||
fields=()
|
||||
while IFS= read -r line || [[ -n "$line" ]]; do
|
||||
# Skip blank lines and comments
|
||||
[[ -z "$line" || "$line" =~ ^[[:space:]]*# ]] && continue
|
||||
# Skip lines without =
|
||||
[[ "$line" != *"="* ]] && continue
|
||||
|
||||
key="${line%%=*}"
|
||||
value="${line#*=}"
|
||||
|
||||
# Strip surrounding quotes from value if present
|
||||
value="${value#\"}" ; value="${value%\"}"
|
||||
value="${value#\'}" ; value="${value%\'}"
|
||||
|
||||
fields+=("${key}[password]=${value}")
|
||||
done < "$env_file"
|
||||
|
||||
[[ ${#fields[@]} -eq 0 ]] && { echo "No KEY=value pairs found in $env_file"; exit 1; }
|
||||
|
||||
echo "Creating \"$item_name\" with ${#fields[@]} field(s)..."
|
||||
|
||||
op item create \
|
||||
--category "Secure Note" \
|
||||
--title "$item_name" \
|
||||
"${fields[@]}"
|
||||
|
||||
echo "Done! ✓"
|
||||
Reference in New Issue
Block a user