Jafner.net/.sops/decrypt-filter.sh

33 lines
772 B
Bash
Raw Permalink Normal View History

2024-08-23 00:12:20 -07:00
#!/bin/bash
# Takes file path as first positional argument
# Takes encrypted file contents from /dev/stdin
# Outputs to stdout
if [[ -f $HOME/.age/key ]]; then
export SOPS_AGE_KEY_FILE=$HOME/.age/key
else
echo "SOPS_AGE_KEY_FILE not found at $HOME/.age/key"
echo "Cannot decrypt secrets."
exit 1
2024-08-23 00:12:20 -07:00
fi
REPO_ROOT="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )/.."
2024-08-23 00:12:20 -07:00
# Set input/output type
FILE_EXT="${1##*.}"
case $FILE_EXT in
"env")
FILE_TYPE=dotenv ;;
"json")
FILE_TYPE=json ;;
"yaml")
FILE_TYPE=yaml ;;
"ini")
FILE_TYPE=ini ;;
esac
if [[ -z ${FILE_TYPE+x} ]]; then
sops --decrypt /dev/stdin
else
sops --decrypt --input-type $FILE_TYPE --output-type $FILE_TYPE /dev/stdin
fi