zsh/install.bash

108 lines
2.3 KiB
Bash
Raw Normal View History

2022-07-30 20:11:35 -05:00
#!/usr/bin/env bash
2022-07-30 21:13:25 -05:00
BOLD=$(tput bold)
2022-07-31 21:44:44 -05:00
M_COLOR=$(tput setaf 5)
2022-07-30 21:13:25 -05:00
CYAN=$(tput setaf 6)
YELLOW=$(tput setaf 3)
2022-07-30 21:19:56 -05:00
RED=$(tput setaf 1)
2022-07-30 21:13:25 -05:00
RESET=$(tput sgr0)
2022-07-30 22:24:52 -05:00
RESET_BOLD="${RESET}${BOLD}"
2022-07-30 21:13:25 -05:00
log() {
# Print a message and send it to stdout or stderr depending upon log level, also configurable with debug etc.
#
# Arguments:
# level <type: string> <position: 1> <required: true>
# - The log level, defined within a case check in this function
# message <type: string> <position: 2> <required: true>
# - The info message
#
# Usage:
# log "info" "Could not find that directory"
#
# POSIX Compliant:
# Yes
#
# Convert the level to uppercase
local level
2022-07-30 22:24:52 -05:00
level=$(printf "%s" "${1}" | tr '[:lower:]' '[:upper:]')
2022-07-30 21:13:25 -05:00
local message
2022-07-30 22:24:52 -05:00
message="${2}"
2022-07-30 21:13:25 -05:00
2022-07-31 21:44:44 -05:00
local prefix="${M_COLOR}Installer${RESET} - "
2022-07-30 21:13:25 -05:00
case "${level}" in
INFO)
2022-07-31 21:44:44 -05:00
printf "%s%sINFO:%s %s%s%s\n" \
"${prefix}" \
2022-07-30 21:13:25 -05:00
"${CYAN}" \
"${RESET}" \
"${BOLD}" \
"${message}" \
"${RESET}" >&2
return 0
;;
WARN*)
2022-07-31 21:44:44 -05:00
printf "%s%sWARN:%s %s%s%s\n" \
"${prefix}" \
2022-07-30 21:13:25 -05:00
"${YELLOW}" \
"${RESET}" \
"${BOLD}" \
"${message}" \
"${RESET}" >&2
return 0
;;
ERROR)
2022-07-31 21:44:44 -05:00
printf "%s%sERROR:%s %s%s%s\n" \
"${prefix}" \
2022-07-30 21:13:25 -05:00
"${RED}" \
"${RESET}" \
"${BOLD}" \
"${message}" \
"${RESET}" >&2
return 0
;;
2022-07-30 22:24:52 -05:00
# Further log levels can be added by extending this switch statement with more comparisons
2022-07-30 21:13:25 -05:00
esac
}
print-break() {
2022-07-31 21:44:44 -05:00
printf "${M_COLOR}%.s─${RESET}" $(seq 1 "$(tput cols)")
2022-07-30 22:24:52 -05:00
}
2022-07-30 20:11:35 -05:00
main() {
2022-07-31 21:44:44 -05:00
local installers_directory
installers_directory="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)/installers"
local installers=()
for installer in "${installers_directory}/"*; do
log "info" "Found installer: ${M_COLOR}${installer}${RESET_BOLD}"
installers+=("${installer}")
2022-07-30 21:13:25 -05:00
done
2022-07-30 22:24:52 -05:00
2022-07-31 21:44:44 -05:00
if ! ((${#installers[@]})); then
log "error" "No installers found"
2022-07-31 00:36:04 -05:00
exit 1
fi
# shellcheck disable=2155
export SOURCE_DIR="$(
# shellcheck disable=2046,2164
cd $(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)/dots
pwd
)"
2022-07-31 21:44:44 -05:00
for installer in "${installers[@]}"; do
log "info" "Running installer: ${M_COLOR}${installer}${RESET_BOLD}"
if ! bash "${installer}"; then
log "error" "Installer: ${M_COLOR}${installer}${RESET_BOLD} failed!"
exit 2
else
log "info" "Finished running installer ${M_COLOR}${installer}${RESET_BOLD}"
2022-07-30 22:24:52 -05:00
fi
2022-07-31 21:44:44 -05:00
done
2022-07-30 20:11:35 -05:00
}
2022-07-30 21:16:11 -05:00
2022-07-31 21:44:44 -05:00
main