rabbitmq-server/selenium/bin/gen-env-file

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

57 lines
1.4 KiB
Plaintext
Raw Normal View History

2023-03-21 19:39:28 +08:00
#!/usr/bin/env bash
SCRIPT="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
if [[ ! -z "${DEBUG}" ]]; then
set -x
fi
2024-01-03 16:28:36 +08:00
2023-03-21 19:39:28 +08:00
ENV_FILE="/tmp/rabbitmq/.env"
PROFILES=$1
FIND_PATH=$2
ENV_FILE=$3
2023-03-21 19:39:28 +08:00
FIND_PARENT_PATH="$(dirname "$FIND_PATH")"
generate_env_file() {
parentdir="$(dirname "$ENV_FILE")"
mkdir -p $parentdir
2024-01-03 16:28:36 +08:00
echo "#!/usr/bin/env bash" > $ENV_FILE
echo "set -u" >> $ENV_FILE
echo "export SELENIUM=${SCRIPT}/.." >> $ENV_FILE
echo "export TEST_CONFIG_PATH=${FIND_PATH}" >> $ENV_FILE
2024-01-03 16:28:36 +08:00
declare -a FILE_ARRAY
for f in $($SCRIPT/find-template-files "${PROFILES}" $FIND_PATH "env")
2024-01-03 16:28:36 +08:00
do
FILE_ARRAY+=($f)
done
TMP_ENV_FILE="/tmp/env-tmp"
FILE_ARRAY_LENGTH=${#FILE_ARRAY[@]}
2023-03-21 19:39:28 +08:00
2024-01-03 16:28:36 +08:00
## Append each .env file one by one while all variables can be resolved
## if one variable cannot be resolve the temporary .env file fails
## and we add the last env file to end of the list and carry one with the next one
while [ $FILE_ARRAY_LENGTH -gt 0 ]
2023-03-21 19:39:28 +08:00
do
2024-01-03 16:28:36 +08:00
f="${FILE_ARRAY[0]}"
cp $ENV_FILE $TMP_ENV_FILE
cat $f >> $TMP_ENV_FILE
chmod u+x $TMP_ENV_FILE
$TMP_ENV_FILE 2> /dev/null
if [ $? -eq 0 ]
then
cat $f >> $ENV_FILE
else
FILE_ARRAY+=($f) # insert it to the end
fi
FILE_ARRAY=("${FILE_ARRAY[@]:1}") # remove the first element
FILE_ARRAY_LENGTH=${#FILE_ARRAY[@]}
2023-03-21 19:39:28 +08:00
done
2024-01-03 16:28:36 +08:00
rm -r $TMP_ENV_FILE
tail +3 $ENV_FILE > "$ENV_FILE.tmp" && mv "$ENV_FILE.tmp" $ENV_FILE
2023-03-21 19:39:28 +08:00
}
generate_env_file