From 00d1fa3ff04afa6ab6598918ae1c4ee779fb4da1 Mon Sep 17 00:00:00 2001 From: jutty Date: Sat, 20 Jul 2024 15:54:32 -0300 Subject: [PATCH] Setup resolve-on-editor resolution logic --- src/index.sh | 1 + src/package/package_conflict_input_parser.sh | 77 ++++++++++++++++++++ src/package/package_conflict_resolution.sh | 4 + 3 files changed, 82 insertions(+) create mode 100644 src/package/package_conflict_input_parser.sh diff --git a/src/index.sh b/src/index.sh index 17e9813..f168b74 100644 --- a/src/index.sh +++ b/src/index.sh @@ -7,4 +7,5 @@ . "$TORI_ROOT/src/package/package_conflict_resolution.sh" . "$TORI_ROOT/src/package/package_tracking.sh" . "$TORI_ROOT/src/package/validate_input_packages.sh" +. "$TORI_ROOT/src/package/package_conflict_input_parser.sh" . "$TORI_ROOT/src/package/update_package_cache.sh" diff --git a/src/package/package_conflict_input_parser.sh b/src/package/package_conflict_input_parser.sh new file mode 100644 index 0000000..555f914 --- /dev/null +++ b/src/package/package_conflict_input_parser.sh @@ -0,0 +1,77 @@ +package_conflict_input_parser() { + local packages="$1" + local conflict_type="$2" + local input="$TMP_DIR/package_conflict_input" + local input_choices="$TMP_DIR/package_conflict_input_choices" + local choices= + local packages_to_install= + local packages_to_uninstall= + local packages_to_track= + local packages_to_untrack= + + help_text_generator "$conflict_type" > "$input" + + echo "$packages" | sed 's/ /\n/g' | while read -r package; do + echo "skip $package" >> "$input" + done + + $EDITOR "$input" + + choices="$(cat "$input" | grep -v '^#' | grep '.')" > "$input_choices" + + # validation + while read -r action package; do + + validate_input_packages "$package" + + if [ "$action" = install ] || [ "$action" = i ]; then + packages_to_install="$packages_to_install $package" + elif [ "$action" = uninstall ] || [ "$action" = u ]; then + packages_to_uninstall="$packages_to_uninstall $package" + elif [ "$action" = add ] || [ "$action" = a ]; then + packages_to_track="$packages_to_track $package" + elif [ "$action" = remove ] || [ "$action" = r ]; then + packages_to_untrack="$packages_to_untrack $package" + elif [ "$action" = skip ] || [ "$action" = s ]; then + log debug "[package_conflict_input_parser] Skipped: $package" + else + log user "Invalid action provided: $action" + fi + done < "$input_choices" + + # actual system or configuration change + echo "$choices" | while read -r action package; do + if [ "$action" = install ] || [ "$action" = i ]; then + package_manager install "$packages_to_install" + elif [ "$action" = uninstall ] || [ "$action" = u ]; then + debug info "Calling package manager to uninstall $packages_to_uninstall" + package_manager uninstall "$packages_to_uninstall" + elif [ "$action" = add ] || [ "$action" = a ]; then + track_packages "$package" + elif [ "$action" = remove ] || [ "$action" = r ]; then + untrack_packages "$package" + fi + done +} + + +help_text_generator() { + local conflict_type="$1" + + echo "# Options:" + + if [ "$conflict_type" == not_installed ]; then + echo "# [i]nstall Install package to system" + echo "# [r]emove Remove from configuration" + elif [ "$conflict_type" == not_on_configuration ]; then + echo "# [u]ninstall Uninstall package from system" + echo "# [a]dd Add to configuration" + else + debug fatal "Invalid conflict type provided: $conflict_type" + return 1 + fi + + echo "# [s]kip Do not take any action" + echo -e "\n# Providing just the value between brackets is sufficient" + echo -e "# Replace 'skip' below with the desired option\n" +} diff --git a/src/package/package_conflict_resolution.sh b/src/package/package_conflict_resolution.sh index e73a8d6..f2ba8dd 100644 --- a/src/package/package_conflict_resolution.sh +++ b/src/package/package_conflict_resolution.sh @@ -57,6 +57,8 @@ not_on_configuration_dialog() { if validate_input_packages "$input_packages"; then track_packages "$input_packages" fi + elif [ "$strategy" = 5 ]; then + package_conflict_input_parser "$conflicted_packages" 'not_on_configuration' else log debug "[resolve_packages] Unexpected input: $strategy" not_on_configuration_dialog "$conflicted_packages" @@ -97,6 +99,8 @@ not_installed_dialog() { read -r -p "Enter space-separated packages to remove from the configuation: " input_packages log debug "Input: input_packages = $input_packages" untrack_packages "$input_packages" + elif [ "$strategy" = 5 ]; then + package_conflict_input_parser "$conflicted_packages" 'not_installed' else log debug "[resolve_packages] Unexpected input: $strategy" not_installed_dialog "$conflicted_packages"