#!/usr/bin/env bash real_args=("$@") # Array to hold arguments for the real `age`. args=() identity_file="" # Parse all arguments. while [[ $# -gt 0 ]]; do case "$1" in -i|--identity) # The next argument is the identity file. Remember and then # remove it. identity_file="$2" shift 2 ;; *) # Keep all other arguments args+=("$1") shift ;; esac done if [ -n "$identity_file" ]; then if [ -f "${identity_file}.gpg" ]; then # If an identity argument was found, assume the corresponding file # is GPG encrypted, decrypt it transparently, and pass it to `age` # via stdin (this assumes no other `age` argument requires reading # from stdin, which empirically seems to be the case). ident=$(gpg --decrypt "${identity_file}.gpg" 2> /dev/null) if [ $? -eq 0 ]; then echo "$ident" | age "${args[@]}" -i - exit $? fi fi fi exec age "${real_args[@]}"