From c3d53a92b22abaf7ed9da5a22756fe9f1fdb37c9 Mon Sep 17 00:00:00 2001 From: jutty Date: Wed, 10 Jul 2024 16:39:06 -0300 Subject: [PATCH] Implement "(un)install all" resolution strategies --- docs/development/roadmap.md | 4 ++ src/configuration.sh | 6 +-- src/package/package.sh | 47 ++++++++++++------- src/package/package_resolution.sh | 78 ++++++++++++++++--------------- 4 files changed, 77 insertions(+), 58 deletions(-) create mode 100644 docs/development/roadmap.md diff --git a/docs/development/roadmap.md b/docs/development/roadmap.md new file mode 100644 index 0000000..6554d8c --- /dev/null +++ b/docs/development/roadmap.md @@ -0,0 +1,4 @@ +To do: + +- Add a `--break` flag to the `log` function to print a newline before any output + - Add this flag to the `log debug "user:\n$user_packages"` call on `scan_packages()` in `configuration.sh` diff --git a/src/configuration.sh b/src/configuration.sh index 0bab213..cbd8f6f 100644 --- a/src/configuration.sh +++ b/src/configuration.sh @@ -25,15 +25,15 @@ scan_packages() { return 1 fi - system_packages="$(get_system_packages)" + system_packages="$(package_manager get_manually_installed)" user_packages="$(get_user_packages)" if [ "$system_packages" = "$user_packages" ]; then log debug "packages match" else log debug "packages mismatch" - log debug "system: $system_packages" - log debug "user: $user_packages" + log debug "system:\n$system_packages" + log debug "user:\n$user_packages" log user "System and configuration packages differ" resolve_packages diff --git a/src/package/package.sh b/src/package/package.sh index 96f9e97..951dd80 100644 --- a/src/package/package.sh +++ b/src/package/package.sh @@ -1,27 +1,38 @@ # package management functions get_user_packages() { - cat $CONFIG_ROOT/packages | sort | uniq + cat $CONFIG_ROOT/packages | sort | uniq } package_manager() { - local command="$1" - local manager - local args__get_manually_installed - local output + local command="$1" + local output - if [ $OS = "FreeBSD" ]; then - manager="pkg" - args__get_manually_installed='query -e "%a = 0" "%n"' - fi + local manager + local authorizer="sudo" # TODO: make configurable + local args__install + local args__uninstall + local args__get_manually_installed - if [ "$command" = 'get_manually_installed' ]; then - output=$(eval $manager $args__get_manually_installed) - printf "$output" - fi -} - -get_system_packages() { - local packages=$(package_manager get_manually_installed) - printf "$packages" + set_opts + + local args__user_args="$2" + set_opts - + + if [ $OS = "FreeBSD" ]; then + manager="pkg" + args__get_manually_installed='query -e "%a = 0" "%n"' + args__install='install' + args__uninstall='delete' + fi + + # shellcheck disable=SC2086 + if [ "$command" = 'get_manually_installed' ]; then + eval $manager "$args__get_manually_installed" + elif [ "$command" = 'install' ]; then + $authorizer $manager $args__install $args__user_args + elif [ "$command" = 'uninstall' ]; then + $authorizer $manager $args__uninstall $args__user_args + else + log debug "[package_manager] Unexpected command: $command" + fi } diff --git a/src/package/package_resolution.sh b/src/package/package_resolution.sh index 4ba510f..4d0b44e 100644 --- a/src/package/package_resolution.sh +++ b/src/package/package_resolution.sh @@ -1,49 +1,53 @@ resolve_packages() { - local strategy= + local strategy= - echo "$system_packages" > "$TMP_DIR/system_packages" - echo "$user_packages" > "$TMP_DIR/user_packages" + echo "$system_packages" > "$TMP_DIR/system_packages" + echo "$user_packages" > "$TMP_DIR/user_packages" - local not_on_configuration="$(grep -v -x -f "$TMP_DIR/user_packages" "$TMP_DIR/system_packages" | xargs)" - local not_installed=$(grep -v -x -f "$TMP_DIR/system_packages" "$TMP_DIR/user_packages" | xargs) + local not_on_configuration="$(grep -v -x -f "$TMP_DIR/user_packages" "$TMP_DIR/system_packages" | xargs)" + local not_installed=$(grep -v -x -f "$TMP_DIR/system_packages" "$TMP_DIR/user_packages" | xargs) - if [ -n "$not_on_configuration" ]; then + if [ -n "$not_on_configuration" ]; then - printf "\nInstalled packages not on configuration: $not_on_configuration\n" - echo " [1] Uninstall all" - echo " [2] Enter packages to uninstall" - echo " [3] Add all to configuration" - echo " [4] Enter packages to add to configuration" - echo " [5] Decide on editor" - echo " [6] Cancel" + printf "\nInstalled packages not on configuration: %s\n" "$not_on_configuration" + echo " [1] Uninstall all" + echo " [2] Enter packages to uninstall" + echo " [3] Add all to configuration" + echo " [4] Enter packages to add to configuration" + echo " [5] Decide on editor" + echo " [6] Cancel" - read -r -p "Choose an option [1-6]: " strategy - log debug "Input: strategy = $strategy" + read -r -p "Choose an option [1-6]: " strategy + log debug "Input: strategy = $strategy" - if [ "$strategy" = 1 ]; then - : # TODO - elif [ $strategy -eq 6 ]; then - return 0 - fi - fi + if [ -z "$strategy" ] || [ "$strategy" -eq 6 ]; then + log debug "[resolve_packages] User choice: Cancel or empty" + elif [ "$strategy" = 1 ]; then + package_manager uninstall "$not_on_configuration" + else + log debug "[resolve_packages] Unexpected input: $strategy" + fi + fi - if [ -n "$not_installed" ]; then + if [ -n "$not_installed" ]; then - printf "\nPackages on configuration but not installed: $not_installed\n" - echo " [1] Install all" - echo " [2] Enter packages to install" - echo " [3] Remove all from configuration" - echo " [4] Enter packages to remove from configuration" - echo " [5] Decide on editor" - echo " [6] Cancel" + printf "\nPackages on configuration but not installed: %s\n" "$not_installed" + echo " [1] Install all" + echo " [2] Enter packages to install" + echo " [3] Remove all from configuration" + echo " [4] Enter packages to remove from configuration" + echo " [5] Decide on editor" + echo " [6] Cancel" - read -r -p "Choose an option [1-6]: " strategy - log debug "Input: strategy = $strategy" + read -r -p "Choose an option [1-6]: " strategy + log debug "Input: strategy = $strategy" - if [ $strategy -eq 1 ]; then - : # TODO - elif [ $strategy -eq 6 ]; then - return 0 - fi - fi + if [ -z "$strategy" ] || [ "$strategy" -eq 6 ]; then + log debug "[resolve_packages] User choice: Cancel or empty" + elif [ "$strategy" -eq 1 ]; then + package_manager install "$not_installed" + else + log debug "[resolve_packages] Unexpected input: $strategy" + fi + fi }