From dd7cc6202754ccbf2ed70effeb135766b41569be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Fuhry?= Date: Fri, 31 Mar 2023 01:10:49 +0200 Subject: [PATCH] Code cleanup Improved Code by using extraction and I reduced the amount of redundant code --- update | 52 ++++++++++++++++++++++++++-------------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/update b/update index 3dc6ec1..2ca99e8 100644 --- a/update +++ b/update @@ -1,7 +1,7 @@ #!/bin/bash # updates using a pacman-wrapper and flatpak-update with flags -# version 1.7 +# version 1.8 source "$HOME"/.config/update.conf @@ -24,14 +24,28 @@ Help() { echo "-g shutdowns the computer afterwards (needs to be the last or only option to work properly)" echo "-r reboots the computer afterwards (needs to be the last or only option to work properly)" echo "--help displays this message" - echo "-P, --preview shows a preview of which pkg's will be updates" + echo "-P, --preview shows a preview of which pkg's can be updated" +} + +check_for_dblck() { + while [ -f /var/lib/pacman/db.lck ]; do { + echo -e "${IYELLOW}->${NC} /var/lib/pacman/db.lck exists" + echo -e "${IYELLOW}->${NC} there might be another instance of pacman running. exiting..." + exit 1 + } + done +} + +delete_oldest_backup() { + if [[ $1 > $BACKUP_AMOUNT ]]; then + OLDEST_FILE="$(ls -t "$BACKUP_LOCATION" | grep $2 | tail -1)" + rm "$BACKUP_LOCATION"/"$OLDEST_FILE" + fi } before_backup() { - if [[ $PRE_BACKUP_AMOUNT > $BACKUP_AMOUNT ]]; then - OLDEST_FILE="$(ls -t "$BACKUP_LOCATION" | grep before-backup | tail -1)" - rm "$BACKUP_LOCATION"/"$OLDEST_FILE" - fi + check_for_dblck + delete_oldest_backup $PRE_BACKUP_AMOUNT before-backup rm -r "$TMP" &> /dev/null mkdir "$TMP" && mkdir "$TMP"/before-backup_"$DATE" && mkdir "$TMP"/after-backup_"$DATE" pacman -Q > "$TMP"/before-backup_"$DATE"/pacman-pre.txt @@ -44,10 +58,7 @@ before_backup() { } after_backup() { - if [[ $POST_BACKUP_AMOUNT > $BACKUP_AMOUNT ]]; then - OLDEST_FILE="$(ls -t "$BACKUP_LOCATION" | grep after-backup | tail -1)" - rm "$BACKUP_LOCATION"/"$OLDEST_FILE" - fi + delete_oldest_backup $POST_BACKUP_AMOUNT after-backup tar -cJf "$TMP"/after-backup_"$DATE".tar.xz.new "$TMP"/after-backup_"$DATE" &> /dev/null cp "$TMP"/after-backup_"$DATE".tar.xz.new "$BACKUP_LOCATION" rename after-backup_"$DATE".tar.xz.new after-backup_"$DATE".tar.xz "$BACKUP_LOCATION"/after-backup_"$DATE".tar.xz.new @@ -55,8 +66,10 @@ after_backup() { } update_with_pacman_wrapper() { + before_backup && echo -e "${IGREEN}pre-backup complete${NC}" "$PACMAN_WRAPPER" pacman -Q > "$TMP"/after-backup_"$DATE"/pacman-after.txt + after_backup && echo -e "${IGREEN}after-backup complete${NC}" } update_with_flatpak() { @@ -67,15 +80,6 @@ update_with_flatpak() { [[ $1 = --help ]] && Help && exit 0 [[ $1 = --preview || $1 = -P ]] && sudo pacman -Sy &> /dev/null && sudo pacman -Qu && exit 0 -while [ -f /var/lib/pacman/db.lck ]; do { - echo -e "${IYELLOW}->${NC} /var/lib/pacman/db.lck exists" - echo -e "${IYELLOW}->${NC} there might be another instance of pacman running. exiting..." - exit 1 -} -done - -before_backup && echo -e "${IGREEN}pre-backup complete${NC}" - [[ -z $1 ]] && update_with_pacman_wrapper; while getopts 'fpagr' OPTIONS; do case "$OPTIONS" in @@ -84,19 +88,15 @@ while getopts 'fpagr' OPTIONS; do p) update_with_pacman_wrapper ;; a) - update_with_pacman_wrapper; update_with_flatpak ;; + update_with_flatpak; update_with_pacman_wrapper ;; g) [[ $1 = -g ]] && update_with_pacman_wrapper - after_backup && echo -e "${IGREEN}after-backup complete${NC}" && sleep 3s && shutdown now - exit 0;; + sleep 3s && shutdown now && exit 0;; r) [[ $1 = -r ]] && update_with_pacman_wrapper - after_backup && echo -e "${IGREEN}after-backup complete${NC}" && sleep 3s && reboot - exit 0;; + sleep 3s && reboot && exit 0;; ?) Help exit 1;; esac done - -after_backup && echo -e "${IGREEN}after-backup complete${NC}"