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

57 lines
1.4 KiB
Bash
Executable File

#!/usr/bin/env bash
SCRIPT="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
if [[ ! -z "${DEBUG}" ]]; then
set -x
fi
ENV_FILE="/tmp/rabbitmq/.env"
PROFILES=$1
FIND_PATH=$2
ENV_FILE=$3
FIND_PARENT_PATH="$(dirname "$FIND_PATH")"
generate_env_file() {
parentdir="$(dirname "$ENV_FILE")"
mkdir -p $parentdir
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
declare -a FILE_ARRAY
for f in $($SCRIPT/find-template-files "${PROFILES}" $FIND_PATH "env")
do
FILE_ARRAY+=($f)
done
TMP_ENV_FILE="/tmp/env-tmp"
FILE_ARRAY_LENGTH=${#FILE_ARRAY[@]}
## 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 ]
do
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[@]}
done
rm -r $TMP_ENV_FILE
tail +3 $ENV_FILE > "$ENV_FILE.tmp" && mv "$ENV_FILE.tmp" $ENV_FILE
}
generate_env_file