From 528cd9d19d43ebbca11fa5c7097de10b5ed5a357 Mon Sep 17 00:00:00 2001 From: Julien Oculi Date: Thu, 30 May 2024 17:34:23 +0200 Subject: [PATCH] feat: implement `.env` (secrets) loader --- deploy.sh | 5 +++++ load_secrets.sh | 23 +++++++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 load_secrets.sh diff --git a/deploy.sh b/deploy.sh index e28b997..6d4e331 100644 --- a/deploy.sh +++ b/deploy.sh @@ -1,3 +1,8 @@ +# Overwrite files with secrets from `.env` +echo "[server_config] > Writting secrets to source files" +source ./load_secrets.sh + +# Deploy services APPS=$@ if [[ $# -eq 1 ]]; then diff --git a/load_secrets.sh b/load_secrets.sh new file mode 100644 index 0000000..a4373e6 --- /dev/null +++ b/load_secrets.sh @@ -0,0 +1,23 @@ +# Get all config files +# FILES=$(find . -type f \ # Only files +# -wholename "./*/*" \ # Only in subdir +# -not -wholename "./.git*" \ # Not in .git/ +# -not -name "_*.sh" \ # Not _install.sh or _deploy.sh +# -not -name "README.md") # Not README.md + +FILES=$(find . -type f -wholename "./*/*" -not -wholename "./.git*" -not -name "_*.sh" -not -name "README.md") + +cat .env | grep ".=." > .env.tmp # Clean .env entries +readarray -t SECRETS < .env.tmp # Get all .env entries +rm .env.tmp # Clean tmp file + +for file in $FILES +do + for secret in "${SECRETS[@]}" + do + KEY=$(echo $secret | grep -o "\w\+") + VALUE=$(echo $secret | grep -oP '\w+\s*=\s*\K.*' | tr -d "\r") + + sed -r "s/\{\{\s*$KEY\s*\}\}/$VALUE/g" $file + done +done