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

View file

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

View file

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