File: gen-env-file

package info (click to toggle)
rabbitmq-server 4.0.5-6
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 37,948 kB
  • sloc: erlang: 257,835; javascript: 22,466; sh: 2,796; makefile: 2,517; python: 1,966; xml: 646; cs: 335; java: 244; ruby: 212; php: 100; perl: 63; awk: 13
file content (50 lines) | stat: -rwxr-xr-x 1,297 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env bash
SCRIPT="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

#set -x

ENV_FILE="/tmp/rabbitmq/.env"
FIND_PATH=$1
ENV_FILE=$2
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

  declare -a FILE_ARRAY
  for f in $($SCRIPT/find-template-files $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