|
| 1 | +# shellcheck shell=bash |
| 2 | + |
| 3 | +bootstrap() { |
| 4 | + set -eEo pipefail |
| 5 | + |
| 6 | + trap 'bootstrap.int' INT |
| 7 | + bootstrap.int() { |
| 8 | + die 'Received SIGINT' |
| 9 | + } |
| 10 | + |
| 11 | + local _original_wd="$PWD" |
| 12 | + trap 'bootstrap.exit' EXIT |
| 13 | + bootstrap.exit() { |
| 14 | + # shellcheck disable=SC2164 |
| 15 | + cd "$_original_wd" |
| 16 | + } |
| 17 | + |
| 18 | + # source files in 'common' |
| 19 | + local dir="common" |
| 20 | + |
| 21 | + shopt -q nullglob |
| 22 | + local shoptExitStatus="$?" |
| 23 | + shopt -s nullglob |
| 24 | + |
| 25 | + local -a filesToSource=() |
| 26 | + |
| 27 | + # Add file in 'util' to filesToSource, |
| 28 | + # ensuring priority of 'override' scripts |
| 29 | + local file possibleFileBasename |
| 30 | + for file in "$GLUE_WD/.glue/$dir"/*?.sh; do |
| 31 | + filesToSource+=("$file") |
| 32 | + done |
| 33 | + |
| 34 | + # Add an 'auto' file if it doesn not have a name of 'bootstrap.sh', |
| 35 | + # or if the name does not already exist in the filesToSource array |
| 36 | + for possibleFile in "$GLUE_WD/.glue/$dir/auto"/*?.sh; do |
| 37 | + possibleFileBasename="${possibleFile##*/}" |
| 38 | + |
| 39 | + if [[ $possibleFileBasename == 'bootstrap.sh' ]]; then |
| 40 | + continue |
| 41 | + fi |
| 42 | + |
| 43 | + # loop over exiting files that we're going to source |
| 44 | + # and ensure 'possibleFile' is not already there |
| 45 | + local alreadyThere=no |
| 46 | + for file in "${filesToSource[@]}"; do |
| 47 | + fileBasename="${file##*/}" |
| 48 | + |
| 49 | + # if the file is not included (which means it's not |
| 50 | + # already covered by 'override'), add it |
| 51 | + if [[ $fileBasename == "$possibleFileBasename" ]]; then |
| 52 | + alreadyThere=yes |
| 53 | + fi |
| 54 | + done |
| 55 | + |
| 56 | + if [[ $alreadyThere == no ]]; then |
| 57 | + filesToSource+=("$possibleFile") |
| 58 | + fi |
| 59 | + done |
| 60 | + |
| 61 | + (( shoptExitStatus != 0 )) && shopt -u nullglob |
| 62 | + |
| 63 | + for file in "${filesToSource[@]}"; do |
| 64 | + source "$file" |
| 65 | + done |
| 66 | + |
| 67 | + local dir="${BASH_SOURCE[1]}" |
| 68 | + dir="${dir%/*}" |
| 69 | + if [ "$GLUE_IS_AUTO" ]; then |
| 70 | + dir="${dir%/*}" |
| 71 | + fi |
| 72 | + dir="${dir##*/}" |
| 73 | + |
| 74 | + # Print |
| 75 | + case "$dir" in |
| 76 | + actions) |
| 77 | + action.log |
| 78 | + ;; |
| 79 | + commands) |
| 80 | + command.log |
| 81 | + ;; |
| 82 | + *) |
| 83 | + die "boostrap: Directory '$dir' not supported" |
| 84 | + esac |
| 85 | +} |
| 86 | + |
| 87 | +unbootstrap() { |
| 88 | + for option in $_util_shopt_data; do |
| 89 | + optionValue="${option%.*}" |
| 90 | + optionName="${option#*.}" |
| 91 | + |
| 92 | + local newOptionValue |
| 93 | + case "$optionValue" in |
| 94 | + -s) newOptionValue="-u" ;; |
| 95 | + -u) newOptionValue="-s" ;; |
| 96 | + esac |
| 97 | + |
| 98 | + shopt "$newOptionValue" "$optionName" |
| 99 | + done |
| 100 | + |
| 101 | + _util_shopt_data= |
| 102 | + |
| 103 | + local dir="${BASH_SOURCE[1]}" |
| 104 | + dir="${dir%/*}" |
| 105 | + if [ "$GLUE_IS_AUTO" ]; then |
| 106 | + dir="${dir%/*}" |
| 107 | + fi |
| 108 | + dir="${dir##*/}" |
| 109 | + |
| 110 | + # Print |
| 111 | + case "$dir" in |
| 112 | + actions) |
| 113 | + echo ":: :: END ACTION" |
| 114 | + ;; |
| 115 | + commands) |
| 116 | + echo ":: END COMMAND" |
| 117 | + ;; |
| 118 | + *) |
| 119 | + die "boostrap: Directory '$dir' not supported" |
| 120 | + esac |
| 121 | +} |
0 commit comments