Skip to content

Instantly share code, notes, and snippets.

@alejandrobernardis
Last active June 8, 2020 03:09
Show Gist options
  • Save alejandrobernardis/94cfab53c0b6eb4c7ae848019ed95839 to your computer and use it in GitHub Desktop.
Save alejandrobernardis/94cfab53c0b6eb4c7ae848019ed95839 to your computer and use it in GitHub Desktop.
Generic installer
#!/bin/zsh
# Author: Alejandro M. BERNARDIS
# Email alejandro.bernardis at gmail dot com
# Created: 2020-06-07
### debug ######################################################################
typeset -gr LOG_LEVEL_OFF=0
typeset -gr LOG_LEVEL_FATAL=1
typeset -gr LOG_LEVEL_CRITICAL=2
typeset -gr LOG_LEVEL_ERROR=3
typeset -gr LOG_LEVEL_WARN=4
typeset -gr LOG_LEVEL_NOTICE=5
typeset -gr LOG_LEVEL_INFO=6
typeset -gr LOG_LEVEL_DEBUG=7
typeset -gr LOG_LEVEL_ALL=100
typeset -gr LOG_LEVEL_COLOR_FATAL="%{%}"
typeset -gr LOG_LEVEL_COLOR_CRITICAL="%{%}"
typeset -gr LOG_LEVEL_COLOR_ERROR="%{%}"
typeset -gr LOG_LEVEL_COLOR_WARN="%{%}"
typeset -gr LOG_LEVEL_COLOR_NOTICE="%{%}"
typeset -gr LOG_LEVEL_COLOR_INFO="%{%}"
typeset -gr LOG_LEVEL_COLOR_DEBUG="%{%}"
typeset -gr LOG_LEVEL_COLOR_HL="%{%}"
typeset -g LOG_LEVEL=$LOG_LEVEL_NOTICE
typeset -gr RC="%{%}"
banner() {
print -P "%{%}"
print "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░"
print "░░░░░░██╗░░░██████╗░░█████╗░████████╗███████╗██╗██╗░░░░░███████╗░██████╗░░"
print "░░░░░██╔╝░░░██╔══██╗██╔══██╗╚══██╔══╝██╔════╝██║██║░░░░░██╔════╝██╔════╝░░"
print "░░░░██╔╝░░░░██║░░██║██║░░██║░░░██║░░░█████╗░░██║██║░░░░░█████╗░░╚█████╗░░░"
print "░░░██╔╝░░░░░██║░░██║██║░░██║░░░██║░░░██╔══╝░░██║██║░░░░░██╔══╝░░░╚═══██╗░░"
print "░░██╔╝░░░██╗██████╔╝╚█████╔╝░░░██║░░░██║░░░░░██║███████╗███████╗██████╔╝░░"
print "░░╚═╝░░░░╚═╝╚═════╝░░╚════╝░░░░╚═╝░░░╚═╝░░░░░╚═╝╚══════╝╚══════╝╚═════╝░░░"
print "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░"
print -P "${RC}"
}
log() {
local ts="${RC}%{%}≋ $(date "+%Y-%m-%d %H:%M:%S") ${RC}"
local action=$1 && shift
{ [[ $1 == "-m" ]] && shift } || \
[[ -n $ts && $action -le ${LOG_LEVEL:-$LOG_LEVEL_WARN} ]] \
|| return 1
local l lc
case $action in
$LOG_LEVEL_FATAL) : "${LOG_LEVEL_COLOR_FATAL}FATAL" ;;
$LOG_LEVEL_CRITICAL) : "${LOG_LEVEL_COLOR_CRITICAL}CRITICAL" ;;
$LOG_LEVEL_ERROR) : "${LOG_LEVEL_COLOR_ERROR}ERROR" ;;
$LOG_LEVEL_WARN) : "${LOG_LEVEL_COLOR_WARN}WARNING" ;;
$LOG_LEVEL_NOTICE) : "${LOG_LEVEL_COLOR_NOTICE}NOTICE" ;;
$LOG_LEVEL_INFO) : "${LOG_LEVEL_COLOR_INFO}INFORMATION" ;;
$LOG_LEVEL_DEBUG) : "${LOG_LEVEL_COLOR_DEBUG}DEBUG" ;;
*) return 1;;
esac
print -P " ${_}:${RC} $@ ${ts} " 1>&2
}
fatal() { log $LOG_LEVEL_FATAL $@; }
critical() { log $LOG_LEVEL_CRITICAL $@; }
error() { log $LOG_LEVEL_ERROR $@; }
warn() { log $LOG_LEVEL_WARN $@; }
notice() { log $LOG_LEVEL_NOTICE $@; }
info() { log $LOG_LEVEL_INFO $@; }
debug() { log $LOG_LEVEL_DEBUG $@; }
exit_error() { error -m ${@:-"fuck!"}; echo ""; exit 1 }
### installer ###################################################################
function main {
clear
setopt LOCAL_OPTIONS EXTENDED_GLOB
local -U args=($@)
local item reload ignore clean output=~/.error
for item ($args); do
case $item in
--(fatal|critical|error|warn|notice|info|debug))
LOG_LEVEL=${(P)$(echo "LOG_LEVEL_${${item:s/--/}:u}")}
;;
-(r|-reload))
reload=1
;;
-(y|-yes))
ignore=1
;;
-(c|-clean))
clean=1
;;
*) exit_error "Argument '${LOG_LEVEL_COLOR_HL}${item}${RC}' is not supported";;
esac
done
banner
notice "Installation started."
debug "Started - $(date +%T)."
umask 022 2&>$output || exit_error "Can't apply umask."
debug "Prevent the cloned repository from having insecure permissions."
info 'Searching essentials commands...'
for item (git); do
command -v "$item" > /dev/null 2>&1 || exit_error "${(C)item} command not found."
debug "${LOG_LEVEL_COLOR_HL}${(C)item}${RC} command was found."
done
info "Looking for an existing ${LOG_LEVEL_COLOR_HL}Github${RC} configuration..."
for item ($HOME/.github .github); do
if [[ -r $item ]]; then
source $item;
debug "Loaded Github configuration ${LOG_LEVEL_COLOR_HL}'$item'${RC}."
break
fi
done
[[ -n $GH_USERNAME && -n $GH_TOKEN ]] || exit_error "Github configuration not found."
debug "Github username ${LOG_LEVEL_COLOR_HL}'$GH_USERNAME'${RC}."
local url="https://${GH_USERNAME}:${GH_TOKEN}@github.com/${GH_USERNAME}/.dotfiles.git"
debug "Repository ${LOG_LEVEL_COLOR_HL}'$(echo $url | sed 's|[^:]*@|******@|g')'${RC}."
local dst="$HOME/.dotfiles"
debug "Destination ${LOG_LEVEL_COLOR_HL}'$dst'${RC}."
[[ ! -d $dst ]] || {
if [[ -z $ignore ]]; then
warn -m "You already have ${LOG_LEVEL_COLOR_HL}~/.dotfile${RC} installed."
warn -m "Press ${LOG_LEVEL_COLOR_NOTICE}[Y]${RC} to reinstall..."
read -sq "answer?"
[[ $answer =~ [yY] ]] || { warn "Aborted."; exit 2 }
fi
warn "Removing old configuration ${LOG_LEVEL_COLOR_HL}'$dst'${RC}..."
rm -fr $dst
}
notice "Checkpoints passed."
info "Cloning repository ${LOG_LEVEL_COLOR_HL}'${url:t:r}'${RC}..."
git clone --quiet --recursive --single-branch \
--config=core.eol=lf \
--config=core.autocrlf=false \
--config=fsck.zeroPaddedFilemode=ignore \
--config=fetch.fsck.zeroPaddedFilemode=ignore \
--config=receive.fsck.zeroPaddedFilemode=ignore \
--branch=master \
--depth=1 \
$url $dst
notice "Repository downloaded."
[[ $? -eq 0 ]] || exit_error "Can't clone the repository."
info "Starting setup..."
/bin/zsh $dst/.0x83 2&>$output
notice "Setup finished."
[[ $? -eq 0 && ! -s $output ]] || exit_error "We found some errors (${LOG_LEVEL_COLOR_HL}'$output'${RC}) in the installation process."
rm -f $output
debug "Removed installation output."
debug "Finished - $(date +%T)."
[[ -z $reload ]] || { info "Restarting shell..."; echo "" ; [[ $clean -eq 1 ]] && clear; exec /bin/zsh --login }
notice "Done."
print ""
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment