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

@ -1,27 +1,38 @@
# package management functions # package management functions
get_user_packages() { get_user_packages() {
cat $CONFIG_ROOT/packages | sort | uniq cat $CONFIG_ROOT/packages | sort | uniq
} }
package_manager() { package_manager() {
local command="$1" local command="$1"
local manager local output
local args__get_manually_installed
local output
if [ $OS = "FreeBSD" ]; then local manager
manager="pkg" local authorizer="sudo" # TODO: make configurable
args__get_manually_installed='query -e "%a = 0" "%n"' local args__install
fi local args__uninstall
local args__get_manually_installed
if [ "$command" = 'get_manually_installed' ]; then set_opts +
output=$(eval $manager $args__get_manually_installed) local args__user_args="$2"
printf "$output" set_opts -
fi
} if [ $OS = "FreeBSD" ]; then
manager="pkg"
get_system_packages() { args__get_manually_installed='query -e "%a = 0" "%n"'
local packages=$(package_manager get_manually_installed) args__install='install'
printf "$packages" 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
} }

View file

@ -1,49 +1,53 @@
resolve_packages() { resolve_packages() {
local strategy= local strategy=
echo "$system_packages" > "$TMP_DIR/system_packages" echo "$system_packages" > "$TMP_DIR/system_packages"
echo "$user_packages" > "$TMP_DIR/user_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_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_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" 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"
echo " [4] Enter packages to add to configuration" echo " [4] Enter packages to add to configuration"
echo " [5] Decide on editor" echo " [5] Decide on editor"
echo " [6] Cancel" echo " [6] Cancel"
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"
fi else
fi 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" 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"
echo " [4] Enter packages to remove from configuration" echo " [4] Enter packages to remove from configuration"
echo " [5] Decide on editor" echo " [5] Decide on editor"
echo " [6] Cancel" echo " [6] Cancel"
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"
fi else
fi log debug "[resolve_packages] Unexpected input: $strategy"
fi
fi
} }