Code refactoring

This commit is contained in:
James Barnett 2017-02-14 08:10:25 -06:00
parent 51cee6cf6c
commit f7bf18e85c
No known key found for this signature in database
GPG key ID: E4B5E45AA3B8C5C3

View file

@ -20,96 +20,42 @@
# Main script. # Main script.
function error_exit declare -r SNAPPER_CONFIG_FILE=/etc/conf.d/snapper
{ declare -r DESC_LIMIT=48
exit 1 declare PACMAN_ABORT_ON_FAIL="no"
}
function kill_exit out() { printf "$1 $2\n" "${@:3}"; }
{ error() { out "\033[00;31m==> ERROR:\033[00m" "$@"; } >&2
printf "\nExited due to user intervention.\n" warning() { out "\033[00;33m==> WARNING:\033[00m" "$@"; } >&2
exit 1 die() {
error "$@"
[[ $PACMAN_ABORT_ON_FAIL == "yes" ]] && exit 1
exit 0
} }
error_exit() { die "Encountered unknown error."; }
kill_exit() { die "\nExited due to user intervention."; }
trap error_exit ERR trap error_exit ERR
trap kill_exit SIGTERM SIGINT trap kill_exit SIGTERM SIGINT
SNAPPER_CONFIG_FILE=/etc/conf.d/snapper [[ ! -f "$SNAPPER_CONFIG_FILE" ]] && die "%s does not exist!" "$SNAPPER_CONFIG_FILE"
DESC_LIMIT=48 source "$SNAPPER_CONFIG_FILE"
ERRORMSG="\033[00;31mERROR:\033[00m" [[ $EUID -ne 0 ]] && die "Script must be run as root."
WARNINGMSG="\033[00;33mWARNING:\033[00m" [[ ! -d /var/run/dbus ]] && die "Unable to use snapper without dbus. Are you in a chroot environment?"
[[ $# -ne 1 ]] && die "Only one argument should be passed to this script."
[[ $1 != "pre" ]] && [[ $1 != "post" ]] && [[ $1 != "rem" ]] && die "First argument should either be 'pre', 'post', or 'rem'."
[[ -z "$SNAPPER_CONFIGS" ]] && die "No snapper configurations found, so not taking any snapshots!"
PACMAN_ABORT_ON_FAIL="no" # Only use checkmark if UTF8 is available and we are not in TTY
declare checkmark="done"
# Virtual console fonts don't have the unicode checkmark [[ $(awk '/UTF-8|utf8/' <(echo $LANG)) && "$TERM" != "linux" ]] && checkmark="✓"
if [[ "$TERM" == "linux" ]]; then
checkmark="done"
# Ensure user is using UTF locale
elif [[ $(awk '/UTF-8|utf8/' <(echo $LANG)) ]]; then
checkmark="✓"
else
checkmark="done"
fi
if [[ -f "$SNAPPER_CONFIG_FILE" ]]; then
source "$SNAPPER_CONFIG_FILE"
else
printf "%b %s does not exist!\n" "$ERRORMSG" "$SNAPPER_CONFIG_FILE"
exit 1
fi
if [[ $EUID -ne 0 ]]; then
printf "%b Script must be run as root.\n" "$ERRORMSG"
if [[ $PACMAN_ABORT_ON_FAIL == "yes" ]]; then
exit 1
else
exit 0
fi
fi
if [[ ! -d /var/run/dbus ]]; then
printf "%b Unable to use snapper without dbus. Are you in a chroot environment?\n" "$ERRORMSG"
if [[ $PACMAN_ABORT_ON_FAIL == "yes" ]]; then
exit 1
else
exit 0
fi
fi
if [[ $# -ne 1 ]]; then
printf "%b Only one argument should be passed to this script.\n" "$ERRORMSG"
if [[ $PACMAN_ABORT_ON_FAIL == "yes" ]]; then
exit 1
else
exit 0
fi
fi
if [[ $1 != "pre" ]] && [[ $1 != "post" ]] && [[ $1 != "rem" ]]; then
printf "%b First argument should either be 'pre', 'post', or 'rem'.\n" "$ERRORMSG"
if [[ $PACMAN_ABORT_ON_FAIL == "yes" ]]; then
exit 1
else
exit 0
fi
fi
if [[ -z "$SNAPPER_CONFIGS" ]]; then
printf "%b No snapper configurations found, so not taking any snapshots!\n" "$ERRORMSG"
if [[ $PACMAN_ABORT_ON_FAIL == "yes" ]]; then
exit 1
else
exit 0
fi
fi
declare -r pre_or_post=$1 declare -r pre_or_post=$1
declare pacman_cmd declare pacman_cmd="$(sed 's./usr/bin/pacman.pacman.g' <(ps -C pacman -o args=))"
pacman_cmd="$(sed 's./usr/bin/pacman.pacman.g' <(ps -C pacman -o args=))"
if [[ -z "$pacman_cmd" ]]; then if [[ -z "$pacman_cmd" ]]; then
printf "%b Didn't find pacman running.\n" "$WARNINGMSG" warning "Didn't find pacman running."
pacman_cmd="pacman" pacman_cmd="pacman"
fi fi
@ -118,9 +64,7 @@ for CONFIG in $SNAPPER_CONFIGS; do
# Set defaults # Set defaults
PACMAN_PRE_POST="no" PACMAN_PRE_POST="no"
if [[ $CONFIG == "root" ]]; then [[ $CONFIG == "root" ]] && PACMAN_PRE_POST="yes"
PACMAN_PRE_POST="yes"
fi
PACMAN_PRE_DESCRIPTION="$pacman_cmd" PACMAN_PRE_DESCRIPTION="$pacman_cmd"
PACMAN_POST_DESCRIPTION="$pacman_cmd" PACMAN_POST_DESCRIPTION="$pacman_cmd"
PACMAN_CLEANUP_ALGORITHM="number" PACMAN_CLEANUP_ALGORITHM="number"
@ -128,63 +72,38 @@ for CONFIG in $SNAPPER_CONFIGS; do
# Source snapper configuration to override defaults # Source snapper configuration to override defaults
source /etc/snapper/configs/"$CONFIG" source /etc/snapper/configs/"$CONFIG"
if [[ $PACMAN_PRE_POST == "yes" ]]; then [[ $PACMAN_PRE_POST == "no" ]] && continue
prefile="/tmp/snap-pac-pre_$CONFIG" prefile="/tmp/snap-pac-pre_$CONFIG"
snapper_cmd="snapper --config $CONFIG create --type $pre_or_post --cleanup-algorithm $PACMAN_CLEANUP_ALGORITHM" snapper_cmd="snapper --config $CONFIG create --type $pre_or_post --cleanup-algorithm $PACMAN_CLEANUP_ALGORITHM"
x=$((x+1)) x=$((x+1))
if [[ "$pre_or_post" == "pre" ]]; then
case "$pre_or_post" in
pre)
printf " %s " "$CONFIG" printf " %s " "$CONFIG"
[[ "${#PACMAN_PRE_DESCRIPTION}" -gt $DESC_LIMIT ]] && PACMAN_PRE_DESCRIPTION="$(echo $PACMAN_PRE_DESCRIPTION | cut -c 1-$DESC_LIMIT)..."
if [[ "${#PACMAN_PRE_DESCRIPTION}" -gt $DESC_LIMIT ]]; then
PACMAN_PRE_DESCRIPTION="$(echo $PACMAN_PRE_DESCRIPTION | cut -c 1-$DESC_LIMIT)..."
fi
$snapper_cmd --description "$PACMAN_PRE_DESCRIPTION" --print-number > "$prefile" $snapper_cmd --description "$PACMAN_PRE_DESCRIPTION" --print-number > "$prefile"
printf "%s %s\n" "$(< "$prefile")" "$checkmark" printf "%s %s\n" "$(< "$prefile")" "$checkmark"
;;
elif [[ "$pre_or_post" == "post" ]]; then post)
if [[ -f $prefile ]]; then if [[ -f $prefile ]]; then
printf " %s " "$CONFIG" printf " %s " "$CONFIG"
[[ "${#PACMAN_POST_DESCRIPTION}" -gt $DESC_LIMIT ]] && PACMAN_POST_DESCRIPTION="$(echo $PACMAN_POST_DESCRIPTION | cut -c 1-$DESC_LIMIT)..."
if [[ "${#PACMAN_POST_DESCRIPTION}" -gt $DESC_LIMIT ]]; then
PACMAN_POST_DESCRIPTION="$(echo $PACMAN_POST_DESCRIPTION | cut -c 1-$DESC_LIMIT)..."
fi
postnum=$($snapper_cmd --description "$PACMAN_POST_DESCRIPTION" --print-number --pre-number "$(< "$prefile")") postnum=$($snapper_cmd --description "$PACMAN_POST_DESCRIPTION" --print-number --pre-number "$(< "$prefile")")
printf "%s %s\n" "$postnum" "$checkmark" printf "%s %s\n" "$postnum" "$checkmark"
rm "$prefile" rm "$prefile"
else else
warning "%s does not exist, so no post snapshot for %s will be taken. If you are initially installing snap-pac, this is normal." "$prefile" "$CONFIG"
printf "%b %s does not exist, so no post snapshot for %s will be taken. If you are initially installing snap-pac, this is normal.\n" "$WARNINGMSG" "$prefile" "$CONFIG"
fi fi
;;
elif [[ "$pre_or_post" == "rem" ]]; then rem)
rm -f "$prefile"
if [[ -f $prefile ]]; then ;;
rm "$prefile" esac
fi
fi
fi
done done
if [[ $x -eq 0 ]]; then [[ $x -eq 0 ]] && die "No snapper configurations are set up for snapshots to be taken!"
printf "%b No snapper configurations are set up for snapshots to be taken!\n" "$ERRORMSG"
if [[ $PACMAN_ABORT_ON_FAIL == "yes" ]]; then
exit 1
else
exit 0
fi
fi
exit 0 exit 0