Skip to content

Instantly share code, notes, and snippets.

@mcoffin
Created July 30, 2020 13:43
Show Gist options
  • Save mcoffin/3f4599c8c2d606fe29e414cded20dfa7 to your computer and use it in GitHub Desktop.
Save mcoffin/3f4599c8c2d606fe29e414cded20dfa7 to your computer and use it in GitHub Desktop.
Pacman helper for AUR linux installing
#!/usr/bin/zsh
set -e
set -o pipefail
if [ $# -lt 1 ]; then
echo 'Usage: paclinux-install.sh VERSION' >&2
exit 1
fi
function print_pkg_files() {
echo -e '\033[1;36mpackage files:\033[0m'
while [ $# -gt 0 ]; do
printf "\t%s\n" "$1"
shift
done
}
function log_color() {
if [ $# -lt 1 ]; then
echo 'Usage: log_color LEVEL' >&2
return 1
fi
case "$1" in
"warn")
echo -ne '\033[1;33m'
;;
"error")
echo -ne '\033[0;31m'
;;
"debug")
echo -ne '\033[0;34m'
;;
"info")
echo -ne '\033[1m'
;;
*)
;;
esac
}
function log_level_prefix() {
if [ $# -lt 1 ]; then
echo 'Usage: log_level_prefix LEVEL' >&2
return 1
fi
case "$1" in
"warn")
echo -n 'WARNING'
;;
"error")
echo -n 'ERROR'
;;
"info")
echo -n 'INFO'
;;
"debug")
echo -n 'DEBUG'
;;
*)
echo -n "$1"
;;
esac
}
function do_log() {
if [ $# -lt 2 ]; then
echo 'Usage: log LEVEL [MESSAGE...]' >&2
return 1
fi
local log_level="$1"
shift
if [ $# -gt 0 ]; then
echo -e "[$(log_color "$log_level")$(log_level_prefix "$log_level")\033[0m]: $@"
fi
}
function log_warn() {
do_log warn "$@"
}
function log_error() {
do_log error "$@"
}
function log_info() {
do_log info "$@"
}
function log_debug() {
local debug_enabled=${DEBUG:-0}
if [ $debug_enabled -eq 0 ]; then
return 0
fi
do_log debug "$@"
}
while getopts ":dC:" opt; do
case ${opt} in
d)
dry_run=1
;;
C)
log_info "Changing directory to: \"$OPTARG\""
cd "$OPTARG"
;;
\?)
log_error "Unknown argument: -$OPTARG" >&2
exit 1
;;
:)
log_error "Invalid argument: -$OPTARG requires an argument" >&2
exit 1
;;
esac
done
shift $((OPTIND -1))
dry_run=${dry_run:-0}
pkg_files=(*$1*.tar.zst)
print_pkg_files ${pkg_files[@]}
if [ ${#pkg_files[@]} -le 0 ]; then
log_error "pkg_files was empty!" >&2
exit 1
fi
if [ ${#pkg_files[@]} -lt 3 ]; then
log_warn "pkg_files contained less than ${#pkg_files[@]} files (expected: 3): ${pkg_files[@]}" >&2
fi
if [ $dry_run -eq 0 ]; then
if [ "$(whoami)" != "root" ]; then
sudo pacman -U ${pkg_files[@]}
else
pacman -U ${pkg_files[@]}
fi
log_debug "Installed $1 from ${pkg_files[@]}"
else
log_info "Dry run: \"pacman -U ${pkg_files[@]}\""
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment