|
6 | 6 | set -e
|
7 | 7 |
|
8 | 8 | follow_link() {
|
9 |
| - FILE="${1}" |
10 |
| - while [ -h "${FILE}" ]; do |
11 |
| - # On macOS, readlink -f doesn't work. |
12 |
| - FILE="$(readlink "${FILE}")" |
| 9 | + local file="$1" |
| 10 | + while [[ -h "${file}" ]]; do |
| 11 | + file="$(readlink "${file}")" |
| 12 | + if [[ ! -e "${file}" ]]; then |
| 13 | + echo "Error: Broken symbolic link: $1" >&2 |
| 14 | + exit 1 |
| 15 | + fi |
13 | 16 | done
|
14 |
| - echo "${FILE}" |
| 17 | + printf '%s\n' "${file}" |
15 | 18 | }
|
16 | 19 |
|
17 |
| -# This function displays the help message. |
18 | 20 | show_help() {
|
19 |
| - local exit_after=${1:-"exit"} |
20 |
| - script_name=$(basename "$0") |
21 |
| - |
22 |
| - printf "\033[0;32m Usage\033[0m: %s [options] [config_path]\n\n" "$script_name" |
23 |
| - printf "\033[0;34m Options:\033[0m\n" |
24 |
| - printf " \033[0;34m --help, -h, -H\033[0m Display this help message.\n\n" |
25 |
| - printf "\033[0;34m config_path:\033[0m\n" |
26 |
| - printf " \033[0m Path to save the generated configuration files. (Optional).\n\n" |
27 |
| - |
28 |
| - # Handle user input or exit. |
29 |
| - if [[ -z "$input" ]]; then |
30 |
| - [ "$exit_after" = "exit" ] && exit 0 |
31 |
| - else |
32 |
| - press_enter_continue |
| 21 | + local exit_after="${1:-exit}" |
| 22 | + local script_name="$(basename "$0")" |
| 23 | + |
| 24 | + printf 'Usage: %s [options] [config_path]\n\n' "${script_name}" |
| 25 | + printf 'Options:\n' |
| 26 | + printf ' --help, -h, -H Display this help message.\n\n' |
| 27 | + printf 'config_path:\n' |
| 28 | + printf ' Path to save the generated configuration files. (Optional).\n\n' |
| 29 | + |
| 30 | + if [[ -z "${input}" ]]; then |
| 31 | + if [[ "${exit_after}" == "exit" ]]; then |
| 32 | + exit 0 |
| 33 | + fi |
33 | 34 | fi
|
| 35 | + read -p "Press Enter to continue..." |
34 | 36 | }
|
35 | 37 |
|
36 |
| -# Define the script and default config paths |
37 |
| -follow_link_result=$(follow_link "$0") |
38 |
| -if ! SCRIPT_PATH=$(realpath "$(dirname "${follow_link_result}")"); then |
39 |
| - echo "Error: follow_link or realpath failed" |
| 38 | +script_path="$(dirname "$(follow_link "$0")")" |
| 39 | + |
| 40 | +config_path="${1:-${script_path}/config}" |
| 41 | + |
| 42 | +if [[ "$1" == "--help" || "$1" == "-h" || "$1" == "-H" ]]; then |
| 43 | + show_help |
| 44 | + exit 0 |
| 45 | +fi |
| 46 | + |
| 47 | +if ! mkdir -p "${config_path}"; then |
| 48 | + echo "Error: Failed to create directory: ${config_path}" >&2 |
40 | 49 | exit 1
|
41 | 50 | fi
|
42 | 51 |
|
43 |
| -# Set config_path if it's not already set |
44 |
| -if [ -z "$CONFIG_PATH" ]; then |
45 |
| - CONFIG_PATH=$( |
46 |
| - cd "${SCRIPT_PATH}/config" || exit |
47 |
| - pwd |
48 |
| - ) |
| 52 | +backup_dir="" |
| 53 | +backup_taken=0 |
| 54 | + |
| 55 | +if [[ -d "${config_path}" && "$(find "${config_path}" -maxdepth 1 -type f -name "*.hcl" -print -quit 2>/dev/null)" ]]; then |
| 56 | + echo "> Backing up existing configurations..." |
| 57 | + backup_time=$(date +%Y%m%d-%H%M%S) |
| 58 | + backup_dir="${config_path}/backup.${backup_time}" |
| 59 | + if ! mkdir -p "${backup_dir}"; then |
| 60 | + echo "Error: Failed to create backup directory: ${backup_dir}" >&2 |
| 61 | + exit 1 |
| 62 | + fi |
| 63 | + find "${config_path}" -maxdepth 1 -type f -name "*.hcl" -print0 | |
| 64 | + xargs -0 -I {} bash -c 'if mv "$1" "${2}/$(basename "$1")"; then :; else echo "Error moving $1"; exit 1; fi' -- {} "${backup_dir}" |
| 65 | + if [[ $? -ne 0 ]]; then |
| 66 | + echo "Error: Some configuration files failed to move to backup" >&2 |
| 67 | + exit 1 |
| 68 | + fi |
| 69 | + if ! rm -f "${config_path}"/*.hcl; then |
| 70 | + echo "Error: Failed to remove original configuration files" >&2 |
| 71 | + exit 1 |
| 72 | + fi |
| 73 | + backup_taken=1 |
| 74 | + echo "> Backup created: ${backup_dir}" |
49 | 75 | fi
|
50 | 76 |
|
51 |
| -# Script options. |
52 |
| -while (("$#")); do |
53 |
| - case "$1" in |
54 |
| - --help | -h | -H) |
55 |
| - run_show_help=true |
56 |
| - show_help |
57 |
| - shift |
58 |
| - ;; |
59 |
| - *) |
60 |
| - CONFIG_PATH=$(realpath "$1") |
61 |
| - shift |
62 |
| - ;; |
63 |
| - esac |
64 |
| -done |
| 77 | +cp -av "${script_path}"/builds/*.pkrvars.hcl.example "${config_path}" 2>&1 >/dev/null |
65 | 78 |
|
66 |
| -mkdir -p "${CONFIG_PATH}" |
67 |
| -### Copy the example input variables. |
68 |
| -echo |
69 |
| -echo "> Copying the example input variables..." |
70 |
| -cp -av "${SCRIPT_PATH}"/builds/*.pkrvars.hcl.example "${CONFIG_PATH}" |
71 |
| -find "${SCRIPT_PATH}"/builds/*/ -type f -name "*.pkrvars.hcl.example" | while IFS= read -r srcfile; do |
| 79 | +find "${script_path}"/builds/*/ -type f -name "*.pkrvars.hcl.example" -print0 | while IFS= read -r -d $'\0' srcfile; do |
72 | 80 | srcdir=$(dirname "${srcfile}" | tr -s /)
|
73 |
| - dstfile=$(echo "${srcdir#"${SCRIPT_PATH}"/builds/}" | tr '/' '-') |
74 |
| - cp -av "${srcfile}" "${CONFIG_PATH}/${dstfile}.pkrvars.hcl.example" |
| 81 | + dstfile=$(echo "${srcdir#"${script_path}"/builds/}" | tr '/' '-') |
| 82 | + cp -av "${srcfile}" "${config_path}/${dstfile}.pkrvars.hcl.example" 2>&1 >/dev/null |
| 83 | + if [[ $? -ne 0 ]]; then |
| 84 | + echo "Error: Failed to copy ${srcfile} to ${config_path}/${dstfile}.pkrvars.hcl.example" >&2 |
| 85 | + fi |
75 | 86 | done
|
76 | 87 |
|
77 |
| -### Rename the example input variables. |
78 |
| -echo |
79 |
| -echo "> Renaming the example input variables..." |
80 |
| -for file in "${CONFIG_PATH}"/*.pkrvars.hcl.example; do |
81 |
| - mv -i -- "${file}" "${file%.example}" |
| 88 | +if [[ $? -ne 0 ]]; then |
| 89 | + echo "Error: One or more copy operations failed." >&2 |
| 90 | + exit 1 |
| 91 | +fi |
| 92 | + |
| 93 | +for file in "${config_path}"/*.pkrvars.hcl.example; do |
| 94 | + if ! mv -- "${file}" "${file%.example}"; then |
| 95 | + echo "Error: Failed to rename ${file}" >&2 |
| 96 | + exit 1 |
| 97 | + fi |
82 | 98 | done
|
83 | 99 |
|
84 |
| -echo |
85 |
| -echo "> Done." |
| 100 | +echo "> Configuration setup complete: ${config_path}" |
0 commit comments