zsh/install.bash

108 lines
2.3 KiB
Bash
Executable File

#!/usr/bin/env bash
BOLD=$(tput bold)
M_COLOR=$(tput setaf 5)
CYAN=$(tput setaf 6)
YELLOW=$(tput setaf 3)
RED=$(tput setaf 1)
RESET=$(tput sgr0)
RESET_BOLD="${RESET}${BOLD}"
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
level=$(printf "%s" "${1}" | tr '[:lower:]' '[:upper:]')
local message
message="${2}"
local prefix="${M_COLOR}Installer${RESET} - "
case "${level}" in
INFO)
printf "%s%sINFO:%s %s%s%s\n" \
"${prefix}" \
"${CYAN}" \
"${RESET}" \
"${BOLD}" \
"${message}" \
"${RESET}" >&2
return 0
;;
WARN*)
printf "%s%sWARN:%s %s%s%s\n" \
"${prefix}" \
"${YELLOW}" \
"${RESET}" \
"${BOLD}" \
"${message}" \
"${RESET}" >&2
return 0
;;
ERROR)
printf "%s%sERROR:%s %s%s%s\n" \
"${prefix}" \
"${RED}" \
"${RESET}" \
"${BOLD}" \
"${message}" \
"${RESET}" >&2
return 0
;;
# Further log levels can be added by extending this switch statement with more comparisons
esac
}
print-break() {
printf "${M_COLOR}%.s─${RESET}" $(seq 1 "$(tput cols)")
}
main() {
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}")
done
if ! ((${#installers[@]})); then
log "error" "No installers found"
exit 1
fi
# shellcheck disable=2155
export SOURCE_DIR="$(
# shellcheck disable=2046,2164
cd $(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)/dots
pwd
)"
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}"
fi
done
}
main