Handle file not exiting when merging files

This commit is contained in:
Juno Takano 2024-08-31 07:56:35 -03:00
parent 1b9e519e05
commit 85151e1de6

View file

@ -5,9 +5,9 @@ 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
} }
@ -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