Created
November 17, 2023 17:14
-
-
Save MikeGarde/58c3ab99d4898b8791a49c940f2f51a4 to your computer and use it in GitHub Desktop.
Merge ENV Files
This file contains hidden or 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 | |
# Function to load .env file and override any duplicates | |
load_env() { | |
local env_file=$1 | |
if [ -f $env_file ]; then | |
while IFS= read -r line | |
do | |
if grep -q "$(echo $line | cut -d'=' -f1)" .env; then | |
# If key exists in .env, remove it | |
sed -i "/$(echo $line | cut -d'=' -f1)/d" .env | |
fi | |
# Add the key-value pair from the .env file | |
echo "$line" >> .env | |
done < $env_file | |
fi | |
} | |
# Create a new .env file | |
> ../.env | |
# Load .env.testing.ci - this is our base .env file for testing | |
if [ -f ../.env.testing.ci ]; then | |
while IFS= read -r line | |
do | |
echo "$line" >> .env | |
done < ../.env.testing.ci | |
fi | |
# Load .env.testing.local and override any duplicates | |
load_env ../.env.testing.local | |
load_env ../.env.testing |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment