#!/usr/bin/env bash init-dotenv-sops() { local project_name project_name=$(basename -s .git "$(git remote get-url origin 2>/dev/null)" 2>/dev/null) project_name=${project_name:-${PWD##*/}} local key_name key_name=$(echo "${1:-$project_name}" | tr '[:upper:]' '[:lower:]') local age_dir="$HOME/.config/sops/age" local age_key_file="$age_dir/keys.txt" mkdir -p "$age_dir" local public_key="" if [[ -f ".sops.yaml" ]]; then public_key=$(grep -E '^[[:space:]]*age:' ".sops.yaml" | head -1 | awk '{print $2}' | tr -d '"' | tr -d "'") echo ".sops.yaml already exists with public key: $public_key" fi if [[ -z "$public_key" ]]; then local keygen_output keygen_output=$(age-keygen 2>/dev/null) public_key=$(echo "$keygen_output" | grep '^# public key:' | cut -d: -f2 | tr -d ' ') { echo "# name: $key_name" echo "$keygen_output" } >> "$age_key_file" echo "Generated age key '$key_name' and appended to $age_key_file" fi if [[ -f "$age_key_file" ]] && ! grep -q "$public_key" "$age_key_file"; then echo "Warning: private key for $public_key not found in $age_key_file" fi if [[ ! -f ".sops.yaml" ]]; then echo "Create .sops.yaml" printf "creation_rules:\n - path_regex: '.*secrets\\.env$'\n age: '%s'\n" "$public_key" > ".sops.yaml" fi echo "Age key file: $age_key_file" echo "Age key name: $key_name" echo "Age public key: $public_key" echo "sops will use the key from $age_key_file automatically." } ids() { init-dotenv-sops "$@" }