|
| 1 | +#!/bin/bash |
| 2 | +# from SO: https://stackoverflow.com/a/54261882/317605 (by https://stackoverflow.com/users/8207842/dols3m) |
| 3 | +function prompt_for_multiselect { |
| 4 | + |
| 5 | + # little helpers for terminal print control and key input |
| 6 | + ESC=$( printf "\033") |
| 7 | + |
| 8 | + cursor_blink_on() { printf "$ESC[?25h"; } |
| 9 | + cursor_blink_off() { printf "$ESC[?25l"; } |
| 10 | + cursor_to() { printf "$ESC[$1;${2:-1}H"; } |
| 11 | + print_inactive() { printf "$2 $1 "; } |
| 12 | + print_active() { printf "$2 $ESC[7m $1 $ESC[27m"; } |
| 13 | + get_cursor_row() { IFS=';' read -sdR -p $'\E[6n' ROW COL; echo ${ROW#*[}; } |
| 14 | + |
| 15 | + key_input() { |
| 16 | + local key |
| 17 | + IFS= read -rsn1 key 2>/dev/null >&2 |
| 18 | + if [[ $key = "" ]]; then echo enter; fi; |
| 19 | + if [[ $key = $'\x20' ]]; then echo space; fi; |
| 20 | + if [[ $key = $'\x1b' ]]; then |
| 21 | + read -rsn2 key |
| 22 | + if [[ $key = [A ]]; then echo up; fi; |
| 23 | + if [[ $key = [B ]]; then echo down; fi; |
| 24 | + fi |
| 25 | + } |
| 26 | + |
| 27 | + toggle_option() { |
| 28 | + local arr_name=$1 |
| 29 | + eval "local arr=(\"\${${arr_name}[@]}\")" |
| 30 | + local option=$2 |
| 31 | + if [[ ${arr[option]} == true ]]; then |
| 32 | + arr[option]= |
| 33 | + else |
| 34 | + arr[option]=true |
| 35 | + fi |
| 36 | + eval $arr_name='("${arr[@]}")' |
| 37 | + } |
| 38 | + |
| 39 | + local retval=$1 |
| 40 | + local options |
| 41 | + local defaults |
| 42 | + |
| 43 | + IFS=';' read -r -a options <<< "$2" |
| 44 | + if [[ -z $3 ]]; then |
| 45 | + defaults=() |
| 46 | + else |
| 47 | + IFS=';' read -r -a defaults <<< "$3" |
| 48 | + fi |
| 49 | + local selected=() |
| 50 | + |
| 51 | + for ((i=0; i<${#options[@]}; i++)); do |
| 52 | + selected+=("${defaults[i]:-false}") |
| 53 | + printf "\n" |
| 54 | + done |
| 55 | + |
| 56 | + # determine current screen position for overwriting the options |
| 57 | + local lastrow=`get_cursor_row` |
| 58 | + local startrow=$(($lastrow - ${#options[@]})) |
| 59 | + |
| 60 | + # ensure cursor and input echoing back on upon a ctrl+c during read -s |
| 61 | + trap "cursor_blink_on; stty echo; printf '\n'; exit" 2 |
| 62 | + cursor_blink_off |
| 63 | + |
| 64 | + local active=0 |
| 65 | + while true; do |
| 66 | + # print options by overwriting the last lines |
| 67 | + local idx=0 |
| 68 | + for option in "${options[@]}"; do |
| 69 | + local prefix=" [ ]" |
| 70 | + if [[ ${selected[idx]} == true ]]; then |
| 71 | + prefix=" [x]" |
| 72 | + fi |
| 73 | + |
| 74 | + cursor_to $(($startrow + $idx)) |
| 75 | + if [ $idx -eq $active ]; then |
| 76 | + print_active "$option" "$prefix" |
| 77 | + else |
| 78 | + print_inactive "$option" "$prefix" |
| 79 | + fi |
| 80 | + ((idx++)) |
| 81 | + done |
| 82 | + |
| 83 | + # user key control |
| 84 | + case `key_input` in |
| 85 | + space) toggle_option selected $active;; |
| 86 | + enter) break;; |
| 87 | + up) ((active--)); |
| 88 | + if [ $active -lt 0 ]; then active=$((${#options[@]} - 1)); fi;; |
| 89 | + down) ((active++)); |
| 90 | + if [ $active -ge ${#options[@]} ]; then active=0; fi;; |
| 91 | + esac |
| 92 | + done |
| 93 | + |
| 94 | + # cursor position back to normal |
| 95 | + cursor_to $lastrow |
| 96 | + printf "\n" |
| 97 | + cursor_blink_on |
| 98 | + |
| 99 | + eval $retval='("${selected[@]}")' |
| 100 | +} |
| 101 | + |
| 102 | +function delete_from_array { |
| 103 | + |
| 104 | + local retval=$1 |
| 105 | + delete="$2" |
| 106 | + array=("${@:3}") |
| 107 | + |
| 108 | + for target in "${delete[@]}"; do |
| 109 | + for i in "${!array[@]}"; do |
| 110 | + if [[ ${array[i]} = $target ]]; then |
| 111 | + unset 'array[i]' |
| 112 | + fi |
| 113 | + done |
| 114 | + done |
| 115 | + |
| 116 | + eval $retval='("${array[@]}")' |
| 117 | + |
| 118 | +} |
| 119 | + |
| 120 | +function array_contains_element { |
| 121 | + local e match="$1" |
| 122 | + shift |
| 123 | + for e; do [[ "$e" == "$match" ]] && return 0; done |
| 124 | + return 1 |
| 125 | +} |
| 126 | + |
| 127 | +function check_environment { |
| 128 | + |
| 129 | + if ! command -v xmlstarlet &> /dev/null |
| 130 | + then |
| 131 | + printf "\n ERROR: Utility 'xmlstarlet' could not be found, please install from : http://xmlstar.sourceforge.net/doc/UG/index.html\n\n" |
| 132 | + exit 1 |
| 133 | + fi |
| 134 | + |
| 135 | +} |
| 136 | + |
| 137 | +##################### |
| 138 | +##################### |
| 139 | + |
| 140 | +check_environment |
| 141 | + |
| 142 | +declare -a ALL_SPRING_PROFILES=(aws azure) |
| 143 | + |
| 144 | +BASE_FOLDER="." |
| 145 | + |
| 146 | +printf "\n" |
| 147 | + |
| 148 | +##################### |
| 149 | + |
| 150 | +unset OPTIONS_STRING OPTIONS_SELECTED_STRING |
| 151 | + |
| 152 | +printf "1. Please select the Cloud required:\n\n" |
| 153 | + |
| 154 | +OPTIONS_VALUES=("azure" "aws") |
| 155 | +OPTIONS_LABELS=("Azure Cloud" "AWS Cloud") |
| 156 | +OPTIONS_SELECTED=("true" "true") |
| 157 | + |
| 158 | +for i in "${!OPTIONS_VALUES[@]}"; do |
| 159 | + OPTIONS_STRING+="${OPTIONS_VALUES[$i]} (${OPTIONS_LABELS[$i]});" |
| 160 | + OPTIONS_SELECTED_STRING+="${OPTIONS_SELECTED[$i]};" |
| 161 | +done |
| 162 | + |
| 163 | +prompt_for_multiselect SELECTED "$OPTIONS_STRING" "$OPTIONS_SELECTED_STRING" |
| 164 | + |
| 165 | +for i in "${!SELECTED[@]}"; do |
| 166 | + if [ "${SELECTED[$i]}" == "true" ]; then |
| 167 | + CHECKED+=("${OPTIONS_VALUES[$i]}") |
| 168 | + delete_from_array ALL_SPRING_PROFILES "${OPTIONS_VALUES[$i]}" "${ALL_SPRING_PROFILES[@]}" |
| 169 | + fi |
| 170 | +done |
| 171 | +# echo "${CHECKED[@]}" |
| 172 | + |
| 173 | +##################### |
| 174 | + |
| 175 | +printf "You have selected these options for your project:\n\n" |
| 176 | +for i in "${CHECKED[@]}"; |
| 177 | +do |
| 178 | + printf " * %s\n" "${i}" |
| 179 | + MAVEN_SPECIFIC_PROFILES+="${i}," |
| 180 | +done |
| 181 | + |
| 182 | +printf "\nPress ENTER to accept or CTRL-C to quit" |
| 183 | +read -r |
| 184 | + |
| 185 | +##################### |
| 186 | + |
| 187 | +cp ${BASE_FOLDER}/pom.xml ${BASE_FOLDER}/pom.template.xml |
| 188 | + |
| 189 | +printf "" |
| 190 | +#echo "DELETE THESE..." |
| 191 | +for i in "${ALL_SPRING_PROFILES[@]}"; |
| 192 | +do |
| 193 | + #echo "$i" |
| 194 | + |
| 195 | + xmlstarlet edit -N ns='http://maven.apache.org/POM/4.0.0' \ |
| 196 | + --delete ".//ns:project/ns:properties/ns:${i}.profile.name" \ |
| 197 | + --delete ".//ns:project/ns:profiles/ns:profile[ns:id=\"${i}\"]" \ |
| 198 | + ${BASE_FOLDER}/pom.template.xml > ${BASE_FOLDER}/pom.template.xml.work |
| 199 | + |
| 200 | + mv ${BASE_FOLDER}/pom.template.xml.work ${BASE_FOLDER}/pom.template.xml |
| 201 | + |
| 202 | + sed -i "" "/- \"@${i}.profile.name@\"/d" ${BASE_FOLDER}/src/main/resources/application.yml |
| 203 | + |
| 204 | + rm -f "${BASE_FOLDER}/src/main/resources/application-${i}.yml" |
| 205 | + rm -f "${BASE_FOLDER}/src/main/resources/local/application-${i}.yml" |
| 206 | + |
| 207 | +done |
| 208 | + |
| 209 | +#echo "KEEP THESE..." |
| 210 | +for i in "${CHECKED[@]}"; |
| 211 | +do |
| 212 | + #echo "$i" |
| 213 | + |
| 214 | + xmlstarlet edit -N ns='http://maven.apache.org/POM/4.0.0' \ |
| 215 | + --move ".//ns:project/ns:profiles/ns:profile[ns:id=\"${i}\"]/ns:dependencies/*" ".//ns:project/ns:dependencies" \ |
| 216 | + ${BASE_FOLDER}/pom.template.xml > ${BASE_FOLDER}/pom.template.xml.work |
| 217 | + |
| 218 | + mv ${BASE_FOLDER}/pom.template.xml.work ${BASE_FOLDER}/pom.template.xml |
| 219 | + |
| 220 | + xmlstarlet edit -N ns='http://maven.apache.org/POM/4.0.0' \ |
| 221 | + --delete ".//ns:project/ns:properties/ns:${i}.profile.name" \ |
| 222 | + --delete ".//ns:project/ns:profiles/ns:profile[ns:id=\"${i}\"]" \ |
| 223 | + ${BASE_FOLDER}/pom.template.xml > ${BASE_FOLDER}/pom.template.xml.work |
| 224 | + |
| 225 | + mv ${BASE_FOLDER}/pom.template.xml.work ${BASE_FOLDER}/pom.template.xml |
| 226 | + |
| 227 | + sed -i "" "s/- \"@${i}.profile.name@\"/- ${i}/g" ${BASE_FOLDER}/src/main/resources/application.yml |
| 228 | + |
| 229 | +done |
| 230 | + |
| 231 | +cp ${BASE_FOLDER}/pom.template.xml ${BASE_FOLDER}/pom.xml |
| 232 | +rm -f ${BASE_FOLDER}/pom.template.xml |
0 commit comments