Last active
April 4, 2024 23:33
-
-
Save AhmedAbouelkher/e59cc74c59fd257848aab8d4dc272b51 to your computer and use it in GitHub Desktop.
Create an `env.dart` files passed on the Environment Values passed and prefixed with `flutter_`
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/sh | |
# list all system env variables starting with flutter_ and put them in an array variable | |
values=$(env | grep ^flutter_) | |
# check if values is empty | |
if [ -z "$values" ]; then | |
echo "No environment variables found starting with flutter_" | |
exit 0 | |
fi | |
strippedValues="" | |
# loop through all values and strip the flutter_ prefix | |
for value in $values | |
do | |
# strip the prefix | |
key=$(echo $value | sed 's/^flutter_//') | |
strippedValues="$strippedValues $key" | |
done | |
envString="" | |
# loop through all values and print them | |
for envVariable in $strippedValues | |
do | |
# split key and value by = (for only the first = in the string) | |
key=$(echo $envVariable | cut -d'=' -f1) | |
value=$(echo $envVariable | cut -d'=' -f2-) | |
# check if value is empty | |
if [ -z "$value" ] | |
then | |
echo "No value found for $key" | |
exit 0 | |
fi | |
# if key starts with _secret, then it is a secret and should be decoded from base64 | |
# example _secret_kAPIKey | |
if echo "$key" | grep -q '^secret_'; then | |
value=$(echo $value | base64 --decode) | |
# remove the _secret_ from the key | |
key=$(echo $key | sed 's/secret_//') | |
fi | |
# add the value to the envString | |
line="const String $key = '$value';\n" | |
envString="$envString$line" | |
done | |
# write the envString to the env.dart file | |
echo "$envString" > $(pwd)/lib/env.dart |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment