Created
August 28, 2021 15:10
-
-
Save sokolovvs/1d39090b4c5a83503993a238ca81e4b6 to your computer and use it in GitHub Desktop.
Update dot env
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# read dot env file to hashmap $tmpHash | |
# param $1 - path to dot env file | |
function readDotEnvIntoTmpHash() { | |
local pathToDotEnv=$1 | |
for line in $(egrep -v '^#' $pathToDotEnv | xargs); do | |
IFS="=" read -ra STR_ARRAY <<<"$line" | |
local k=${STR_ARRAY[0]} | |
local v=${STR_ARRAY[1]} | |
tmpHash["$k"]="$v" | |
done | |
} | |
# cp env.example .env with saving defined values from .env | |
# param $1 - path to dot env file | |
function updateDotEnv() { | |
declare -A tmpHash | |
pathToEnv=$1 | |
readDotEnvIntoTmpHash "$pathToEnv.example" | |
readDotEnvIntoTmpHash "$pathToEnv" | |
writeDotEnv "$pathToEnv" | |
unset tmpHash | |
} | |
# write hashmap $tmpHash to dot env file | |
# param $1 - path to dot env file | |
function writeDotEnv() { | |
pathToEnv=$1 | |
cat /dev/null >"$pathToEnv" | |
for k in "${!tmpHash[@]}"; do | |
echo "$k=${tmpHash[$k]}" >>"$pathToEnv" | |
done | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment