Implement "(un)install all" resolution strategies

This commit is contained in:
Juno Takano 2024-07-10 16:39:06 -03:00
parent cb54fdd708
commit c3d53a92b2
4 changed files with 77 additions and 58 deletions

View file

@ -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`

View file

@ -25,15 +25,15 @@ scan_packages() {
return 1 return 1
fi fi
system_packages="$(get_system_packages)" system_packages="$(package_manager get_manually_installed)"
user_packages="$(get_user_packages)" user_packages="$(get_user_packages)"
if [ "$system_packages" = "$user_packages" ]; then if [ "$system_packages" = "$user_packages" ]; then
log debug "packages match" log debug "packages match"
else else
log debug "packages mismatch" log debug "packages mismatch"
log debug "system: $system_packages" log debug "system:\n$system_packages"
log debug "user: $user_packages" log debug "user:\n$user_packages"
log user "System and configuration packages differ" log user "System and configuration packages differ"
resolve_packages resolve_packages

View file

@ -6,22 +6,33 @@ get_user_packages() {
package_manager() { package_manager() {
local command="$1" local command="$1"
local manager
local args__get_manually_installed
local output local output
local manager
local authorizer="sudo" # TODO: make configurable
local args__install
local args__uninstall
local args__get_manually_installed
set_opts +
local args__user_args="$2"
set_opts -
if [ $OS = "FreeBSD" ]; then if [ $OS = "FreeBSD" ]; then
manager="pkg" manager="pkg"
args__get_manually_installed='query -e "%a = 0" "%n"' args__get_manually_installed='query -e "%a = 0" "%n"'
args__install='install'
args__uninstall='delete'
fi fi
# shellcheck disable=SC2086
if [ "$command" = 'get_manually_installed' ]; then if [ "$command" = 'get_manually_installed' ]; then
output=$(eval $manager $args__get_manually_installed) eval $manager "$args__get_manually_installed"
printf "$output" 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 fi
} }
get_system_packages() {
local packages=$(package_manager get_manually_installed)
printf "$packages"
}

View file

@ -9,7 +9,7 @@ resolve_packages() {
if [ -n "$not_on_configuration" ]; then if [ -n "$not_on_configuration" ]; then
printf "\nInstalled packages not on configuration: $not_on_configuration\n" printf "\nInstalled packages not on configuration: %s\n" "$not_on_configuration"
echo " [1] Uninstall all" echo " [1] Uninstall all"
echo " [2] Enter packages to uninstall" echo " [2] Enter packages to uninstall"
echo " [3] Add all to configuration" echo " [3] Add all to configuration"
@ -20,16 +20,18 @@ resolve_packages() {
read -r -p "Choose an option [1-6]: " strategy read -r -p "Choose an option [1-6]: " strategy
log debug "Input: strategy = $strategy" log debug "Input: strategy = $strategy"
if [ "$strategy" = 1 ]; then if [ -z "$strategy" ] || [ "$strategy" -eq 6 ]; then
: # TODO log debug "[resolve_packages] User choice: Cancel or empty"
elif [ $strategy -eq 6 ]; then elif [ "$strategy" = 1 ]; then
return 0 package_manager uninstall "$not_on_configuration"
else
log debug "[resolve_packages] Unexpected input: $strategy"
fi fi
fi fi
if [ -n "$not_installed" ]; then if [ -n "$not_installed" ]; then
printf "\nPackages on configuration but not installed: $not_installed\n" printf "\nPackages on configuration but not installed: %s\n" "$not_installed"
echo " [1] Install all" echo " [1] Install all"
echo " [2] Enter packages to install" echo " [2] Enter packages to install"
echo " [3] Remove all from configuration" echo " [3] Remove all from configuration"
@ -40,10 +42,12 @@ resolve_packages() {
read -r -p "Choose an option [1-6]: " strategy read -r -p "Choose an option [1-6]: " strategy
log debug "Input: strategy = $strategy" log debug "Input: strategy = $strategy"
if [ $strategy -eq 1 ]; then if [ -z "$strategy" ] || [ "$strategy" -eq 6 ]; then
: # TODO log debug "[resolve_packages] User choice: Cancel or empty"
elif [ $strategy -eq 6 ]; then elif [ "$strategy" -eq 1 ]; then
return 0 package_manager install "$not_installed"
else
log debug "[resolve_packages] Unexpected input: $strategy"
fi fi
fi fi
} }