Compare commits

..

2 commits

Author SHA1 Message Date
a0fdbe0149 Add an exit option to ask dialog 2024-08-31 07:56:41 -03:00
85151e1de6 Handle file not exiting when merging files 2024-08-31 07:56:35 -03:00
2 changed files with 23 additions and 12 deletions

View file

@ -5,11 +5,11 @@ merge_files() {
if [ "$strategy" == tree ]; then if [ "$strategy" == tree ]; then
log info "[merge_files] Merging with $strategy strategy" log info "[merge_files] Merging with $strategy strategy"
if ! file_scan_tree "$base_files"; then if ! file_scan_tree "$base_files"; then
file_merge_tree "$base_files" if ! file_merge_tree "$base_files"; then
log info "[merge_files] Recursing"
merge_files "$base_files" merge_files "$base_files"
fi fi
fi fi
fi
} }
file_scan_tree() { file_scan_tree() {
@ -19,14 +19,13 @@ file_scan_tree() {
local absolute_path="$(echo "$file" | sed 's/^base//')" local absolute_path="$(echo "$file" | sed 's/^base//')"
local config_path="$CONFIG_ROOT/$file" local config_path="$CONFIG_ROOT/$file"
if ! diff "$absolute_path" "$config_path" > /dev/null; then if ! diff "$absolute_path" "$config_path" > /dev/null 2>&1; then
return 1 return 1
fi fi
done done
return 0 return 0
} }
# TODO Check if files exist before acting
file_merge_tree() { file_merge_tree() {
local base_files="$1" local base_files="$1"
local strategy= local strategy=
@ -38,21 +37,32 @@ file_merge_tree() {
local config_path="$CONFIG_ROOT/$file" local config_path="$CONFIG_ROOT/$file"
log debug "[merge_tree] Config path: $config_path" log debug "[merge_tree] Config path: $config_path"
if diff "$absolute_path" "$config_path" > /dev/null; then if diff "$absolute_path" "$config_path" > /dev/null 2>&1; then
log debug "[merge_tree] Files match" log debug "[merge_tree] Files match"
else else
log debug "[merge_tree] Files differ" log debug "[merge_tree] Files differ"
strategy="$(ask "Differs: $(tildify "$absolute_path")" \ local prompt_verb="Differs"
"Overwrite system,Overwrite configuration,Show difference")" local prompt_options="Overwrite system,Overwrite configuration,Show difference"
if ! [ -f "$absolute_path" ]; then
local prompt_verb="In configuration only"
local prompt_options="Copy to system"
fi
strategy="$(ask "$prompt_verb: $(tildify "$absolute_path")" "$prompt_options")" ||
file_merge_tree "$base_files"
log debug "[merge_tree] Chosen strategy: $strategy" log debug "[merge_tree] Chosen strategy: $strategy"
if [ "$strategy" -eq 1 ]; then if [ "$strategy" -eq 0 ]; then
return 0
elif [ "$strategy" -eq 1 ]; then
cp -vi "$config_path" "$absolute_path" cp -vi "$config_path" "$absolute_path"
return 1
elif [ "$strategy" -eq 2 ]; then elif [ "$strategy" -eq 2 ]; then
cp -vi "$absolute_path" "$config_path" cp -vi "$absolute_path" "$config_path"
return 1
elif [ "$strategy" -eq 3 ]; then elif [ "$strategy" -eq 3 ]; then
echo "< $(tildify "$absolute_path") | $(echo "$config_path" | sed "s*$CONFIG_ROOT/**") >" echo "< $(tildify "$absolute_path") | $(echo "$config_path" | sed "s*$CONFIG_ROOT/**") >"
diff "$absolute_path" "$config_path" || return 0 diff "$absolute_path" "$config_path"
return 1
else else
log user 'Invalid choice' log user 'Invalid choice'
fi fi

View file

@ -68,14 +68,15 @@ ask() {
dialog_options="$dialog_options\n [$options_count] $option" dialog_options="$dialog_options\n [$options_count] $option"
done; done;
IFS= IFS=
dialog_options="$dialog_options\n [0] Exit"
printf "%s" "$question" >&2 printf "%s" "$question" >&2
printf "%b" "$dialog_options" >&2 printf "%b" "$dialog_options" >&2
printf "\n%s" "Choose an option [1-$options_count] " >&2 printf "\n%s" "Choose an option number: " >&2
read -r read_answer read -r read_answer
answer="$(echo "$read_answer" | xargs)" answer="$(echo "$read_answer" | xargs)"
if [ "$answer" -gt 0 ] 2> /dev/null && [ "$answer" -le $options_count ]; then if [ "$answer" -ge 0 ] 2> /dev/null && [ "$answer" -le $options_count ]; then
echo "$answer" echo "$answer"
else else
log debug "[ask] Invalid choice" log debug "[ask] Invalid choice"