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

View file

@ -68,14 +68,15 @@ ask() {
dialog_options="$dialog_options\n [$options_count] $option"
done;
IFS=
dialog_options="$dialog_options\n [0] Exit"
printf "%s" "$question" >&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
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"
else
log debug "[ask] Invalid choice"