Add color output & error handling to bootstrap.sh

This commit is contained in:
Rezart Qelibari
2022-03-01 11:25:34 +01:00
parent 45711e79c5
commit 7619c10845

View File

@@ -2,16 +2,15 @@
# vi: set expandtab ft=zsh tw=80 ts=2 # vi: set expandtab ft=zsh tw=80 ts=2
function ensureDocopts() { function ensureDocopts() {
which docopts > /dev/null which docopts > /dev/null && return
[ $? -eq 0 ] && return curl --output ./docopts -fsSL https://github.com/astzweig/docopts/releases/download/v.0.7.0/docopts_darwin_amd64 >&! /dev/null || return
curl --output ./docopts -fsSL https://github.com/astzweig/docopts/releases/download/v.0.7.0/docopts_darwin_amd64
chmod u+x ./docopts chmod u+x ./docopts
PATH="${PATH}:`pwd`" PATH="${PATH}:`pwd`"
} }
function cloneMacOSSystemRepo() { function cloneMacOSSystemRepo() {
local repoUrl="${MACOS_SYSTEM_REPO_URL:-https://github.com/astzweig/macos-system.git}" local repoUrl="${MACOS_SYSTEM_REPO_URL:-https://github.com/astzweig/macos-system.git}"
git clone -q "${repoUrl}" . git clone -q "${repoUrl}" . 2> /dev/null || return 10
[ -n "${MACOS_SYSTEM_REPO_BRANCH}" ] && git checkout ${MACOS_SYSTEM_REPO_BRANCH} 2> /dev/null || true [ -n "${MACOS_SYSTEM_REPO_BRANCH}" ] && git checkout ${MACOS_SYSTEM_REPO_BRANCH} 2> /dev/null || true
} }
@@ -20,30 +19,60 @@ function cloneZSHLibRepo() {
git config --file=.gitmodules submodule.zshlib.url "${zshlibRepoUrl}" git config --file=.gitmodules submodule.zshlib.url "${zshlibRepoUrl}"
git submodule -q sync git submodule -q sync
[ -n "${ZSHLIB_REPO_BRANCH}" ] && git submodule set-branch -b ${ZSHLIB_REPO_BRANCH} `git config --file=.gitmodules submodule.zshlib.path` 2> /dev/null || true [ -n "${ZSHLIB_REPO_BRANCH}" ] && git submodule set-branch -b ${ZSHLIB_REPO_BRANCH} `git config --file=.gitmodules submodule.zshlib.path` 2> /dev/null || true
git submodule -q update --init --recursive --remote git submodule -q update --init --recursive --remote 2> /dev/null || return 10
} }
function isDebug() { function isDebug() {
test "${DEBUG}" = true -o "${DEBUG}" = 1 test "${DEBUG}" = true -o "${DEBUG}" = 1
} }
function printSuccess() {
print "${colors[green]}${*}${colors[reset]}"
}
function printError() {
print "${errColors[red]}${*}${errColors[reset]}" >&2
}
function printFailedWithError() {
print "${colors[red]}failed.${colors[reset]}"
print "$*" >&2
}
function defineColors() {
local -A colorCodes=(red "`tput setaf 9`" green "`tput setaf 10`" reset "`tput sgr0`")
[ -t 1 ] && colors=("${(kv)colorCodes[@]}")
[ -t 2 ] && errColors=("${(kv)colorCodes[@]}")
}
function ensureRepo() {
local repoName="$1" cmdName="${2}"
print -n "Installing ${1}..."
$cmdName || { printFailedWithError "This script requires $repoName but was not able to clone it. Please ensure access to the $repoName repository."; return 10}
printSuccess 'done'
}
function ensureBinary() {
local binaryName="$1" cmdName="${2}"
print -n "Ensure ${1} is installed..."
$cmdName || { printFailedWithError "This script requires $binaryName but was neither able to locate and install it. Please install $binaryName and add it to one of the PATH directories."; return 10}
printSuccess 'done'
}
function main() { function main() {
local colors=() local -A colors=() errColors=()
[ -t 2 ] && colors=(red "`tput setaf 1`" reset "`tput sgr0`") defineColors
id -Gn | grep admin >&! /dev/null || { echo "${colors[red]}"'This script requires root access. Please run as an admin user.'"${colors[reset]}" >&2; return 10 } id -Gn | grep admin >&! /dev/null || { printError 'This script requires root access. Please run as an admin user.'; return 10 }
local tmpdir="`mktemp -d -t 'macos-system'`" local tmpdir="`mktemp -d -t 'macos-system'`"
isDebug || trap "rm -fr -- '${tmpdir}'; return" INT TERM EXIT isDebug || trap "rm -fr -- '${tmpdir}'; return" INT TERM EXIT
pushd -q "${tmpdir}" pushd -q "${tmpdir}"
printf 'Installing macos-system...' print -l "Working directory is: ${tmpdir}"
cloneMacOSSystemRepo
printf 'done\n' ensureRepo 'macos-system' cloneMacOSSystemRepo || return
printf 'Installing zshlib...' ensureRepo 'zshlib' cloneZSHLibRepo || return
cloneZSHLibRepo ensureBinary 'docopts' ensureDocopts || return
printf 'done\n'
printf 'Ensure docopts is installed...' print 'Will now run the installer.'
ensureDocopts
printf 'done\n'
printf 'Will now run the installer\n.'
sudo "${tmpdir}/install.sh" "$@" sudo "${tmpdir}/install.sh" "$@"
popd -q popd -q
} }