From 71c41ecbde4648d4dc4e4b51762a07e16b0cf001 Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Wed, 27 Apr 2022 13:00:09 +0200 Subject: [PATCH] bootstrap: Update package list script Takes simple options -h (show help), -v (verbose), -n (dry-run) when started. By default does not spam output of *all* recognized packages anymore but simply prints a diff at the end. Old output can be enabled with `-v` verbose option. Diff is shown even if dry-run mode is enabled so that using dry-run by default will show you a preview of what changed. --- bootstrap/update_package_list.sh | 40 ++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/bootstrap/update_package_list.sh b/bootstrap/update_package_list.sh index 8e46d62..8d35bf0 100755 --- a/bootstrap/update_package_list.sh +++ b/bootstrap/update_package_list.sh @@ -7,6 +7,24 @@ pkg_all=$(pacman -Qqett | grep -v "$(pacman -Qqg base-devel)") pkg_repo=$(pacman -Qqn) pkg_aur=$(pacman -Qqm) +while getopts "nvh" opt; do + case "$opt" in + n) DRYRUN=true ;; + v) VERBOSE=true ;; + h | *) + { + printf "\nUpdate the list of installed packages.\n\nWill compare packages committed to the dotfile repository\nand those currently installed (on an Arch system, using pacman).\nUpdates the list of committed packages in repository\nand prints out the differences as a diff.\n\nOptions:\n\n\t-h\tDisplay this help.\n\t-v\tShow verbose information.\n\t-n\tPrint out changes without changing anything (dry-run).\n" + exit 1 + } + ;; + esac +done + +print_msg() { + # shellcheck disable=2059 + [ -n "$VERBOSE" ] && printf "$@" +} + # tsv file: # packagename, description, source, target # toot a toot manager A D @@ -20,9 +38,9 @@ touch "${OUTPUT}_TEMP" for pkg in $pkg_all; do source="" - if $(echo "$pkg_repo" | grep -F -q -x "$pkg"); then + if echo "$pkg_repo" | grep -F -q -x "$pkg"; then source="R" - elif $(echo "$pkg_aur" | grep -F -q -x "$pkg"); then + elif echo "$pkg_aur" | grep -F -q -x "$pkg"; then source="A" else echo "ERROR: The package $pkg could not be found in repositories or AUR." @@ -39,9 +57,9 @@ for pkg in $pkg_all; do fi if [ -n "$found_line" ]; then target=$(echo "$found_line" | cut -f4) - printf "Updating pkg: %s:%s from: %s, for: %s\n" "$pkg" "$desc" "$source" "$target" + print_msg "Updating pkg: %s:%s from: %s, for: %s\n" "$pkg" "$desc" "$source" "$target" else - printf "Adding pkg: %s:%s from: %s, for: %s\n" "$pkg" "$desc" "$source" "$target" + print_msg "Adding pkg: %s:%s from: %s, for: %s\n" "$pkg" "$desc" "$source" "$target" fi printf "%s\t%s\t%s\t%s\n" "$pkg" "$desc" "$source" "$target" >>"${OUTPUT}_TEMP" @@ -51,11 +69,19 @@ done if [ -f "$OUTPUT" ]; then while read -r line; do if ! echo "$line" | cut -f1 | xargs -I _ grep -F -q -x _ <(echo "$pkg_all"); then - printf "REMOVED: %s" "$line" + printf "REMOVED: %s\n" "$line" fi - done <<<$(tail +2 "$OUTPUT") + done <<<"$(tail +2 $OUTPUT)" +fi + +# show file changes +if [ -f "$OUTPUT" ] && [ -f "$OUTPUT"_TEMP ]; then + changes=$(diff --color=always -y --suppress-common-lines "$OUTPUT" "$OUTPUT"_TEMP | tail -n+2) + printf "FILE CHANGES:\n=============\n%s" "$changes" fi # actually write to file -cat <(printf "Name\tDescription\tSource\tTarget\n") "${OUTPUT}_TEMP" >"$OUTPUT" +if [ -z "$DRYRUN" ]; then + cat <(printf "Name\tDescription\tSource\tTarget\n") "${OUTPUT}_TEMP" >"$OUTPUT" +fi rm "${OUTPUT}_TEMP"