Skip to content

Commit

Permalink
src: backup: Adding verbose mode
Browse files Browse the repository at this point in the history
This commit adds support for the verbose parameter within `kw backup`.
The verbose parameter gives details of the commands that are executed
behind the scenes.

Note: This is part of the issue: #179

Reviewed-by: Rodrigo Siqueira <siqueirajordao@riseup.net>
Signed-off-by: Aquila Macedo <aquilamacedo@riseup.net>
  • Loading branch information
aquilamacedo authored and rodrigosiqueira committed Apr 30, 2023
1 parent c0efb7f commit 60eda44
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 19 deletions.
6 changes: 6 additions & 0 deletions documentation/man/features/backup.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ SYNOPSIS
========
| *kw* *backup* [<path>]
| *kw* *backup* (-r | \--restore) <path> [(-f | \--force)]
| *kw* *backup* (\--verbose)
DESCRIPTION
===========
Expand All @@ -24,6 +25,11 @@ OPTIONS
then it will by default ask you on how to proceed. To simply replace all files
from **KW_DATA_DIR** with the backup, you may use the `\--force` option.

\--verbose:
Verbose mode is an option that causes the kw program to display debug messages to track
its progress. This functionality is very useful during the debugging process, allowing
you to identify possible errors more easily.

EXAMPLES
========

Expand Down
1 change: 1 addition & 0 deletions src/_kw
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ _kw_backup()
fi

_arguments : \
'(--verbose)--verbose[enable verbose mode]' \
'(-r --restore)'{-r,--restore}'[restore kw data from specified path]: :_files' \
$force \
$args
Expand Down
75 changes: 56 additions & 19 deletions src/backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,25 @@ decompress_path="$KW_CACHE_DIR/tmp-kw-backup"
# recieves an argument and call other functions based on that.
function backup()
{
local flag

flag=${flag:-'SILENT'}

parse_backup_options "$@"
if [[ "$?" -gt 0 ]]; then
complain "${options_values['ERROR']}"
return 22 # EINVAL
fi

[[ -n "${options_values['VERBOSE']}" ]] && flag='VERBOSE'

if [[ -n "${options_values['BACKUP_PATH']}" ]]; then
create_backup "${options_values['BACKUP_PATH']}"
create_backup "${options_values['BACKUP_PATH']}" "$flag"
return
fi

if [[ -n "${options_values['RESTORE_PATH']}" ]]; then
restore_backup "${options_values['RESTORE_PATH']}"
restore_backup "${options_values['RESTORE_PATH']}" "$flag"
return
fi
}
Expand Down Expand Up @@ -74,6 +80,7 @@ function restore_backup()
local ret
local config_file
local difference
local cmd

flag=${flag:-'SILENT'}

Expand All @@ -85,7 +92,8 @@ function restore_backup()
if [[ -n "${options_values['FORCE']}" ]]; then
decompress_path="$KW_DATA_DIR"
else
mkdir -p "$decompress_path"
cmd="mkdir -p ${decompress_path}"
cmd_manager "$flag" "$cmd"
fi
extract_tarball "$path" "$decompress_path" 'gzip' "$flag"

Expand All @@ -96,8 +104,8 @@ function restore_backup()
fi

if [[ -z "${options_values['FORCE']}" ]]; then
restore_config
restore_pomodoro
restore_config "$flag"
restore_pomodoro "$flag"
restore_statistics
fi

Expand All @@ -108,43 +116,58 @@ function restore_backup()
#
function restore_config()
{
local flag="$1"
local config_file
local cmd

flag=${flag:-'SILENT'}

if [[ -d "$decompress_path/"configs ]]; then
mkdir -p "$KW_DATA_DIR/configs/configs"
mkdir -p "$KW_DATA_DIR/configs/metadata"
cmd="mkdir -p ${KW_DATA_DIR}/configs/configs"
cmd_manager "$flag" "$cmd"
cmd="mkdir -p ${KW_DATA_DIR}/configs/metadata"
cmd_manager "$flag" "$cmd"

for file in "$decompress_path/"configs/configs/*; do
config_file="$(get_file_name_from_path "$file")"
if [[ -f "$KW_DATA_DIR/configs/configs/$config_file" ]] &&
! cmp -s "$KW_DATA_DIR/configs/configs/$config_file" "$file"; then
complain "It looks like that the file $config_file differs from the backup version."
diff -u --color=always "$KW_DATA_DIR/configs/configs/$config_file" "$file"
cmd="diff -u --color=always ${KW_DATA_DIR}/configs/configs/${config_file} ${file}"
cmd_manager "$flag" "$cmd"
if [[ $(ask_yN 'Do you want to replace it and its metadata?') =~ "0" ]]; then
continue
fi
fi
cp -r "$decompress_path/configs/configs/$config_file" "$KW_DATA_DIR/configs/configs"
cp -r "$decompress_path/configs/metadata/$config_file" "$KW_DATA_DIR/configs/metadata"
cmd="cp -r ${decompress_path}/configs/configs/${config_file} ${KW_DATA_DIR}/configs/configs"
cmd_manager "$flag" "$cmd"
cmd="cp -r ${decompress_path}/configs/metadata/${config_file} ${KW_DATA_DIR}/configs/metadata"
cmd_manager "$flag" "$cmd"
done
fi
}

function restore_data_from_dir()
{
local dir="$1"
local flag="$2"
local decision
local year
local month
local day
local difference
local cmd

flag=${flag:-'SILENT'}

for year_dir in "$decompress_path/$dir/"*/; do
year="$(str_remove_prefix "$year_dir" "$decompress_path/$dir/")"
mkdir -p "$KW_DATA_DIR/$dir/$year"
cmd="mkdir -p ${KW_DATA_DIR}/${dir}/${year}"
cmd_manager "$flag" "$cmd"
for month_dir in "$year_dir"*; do
month="$(str_remove_prefix "$month_dir" "$decompress_path/$dir/$year")"
mkdir -p "$KW_DATA_DIR/$dir/$year$month"
cmd="mkdir -p ${KW_DATA_DIR}/${dir}/${year}${month}"
cmd_manager "$flag" "$cmd"
for day_file in "$month_dir"/*; do
day="$(str_remove_prefix "$day_file" "$decompress_path/$dir/$year$month/")"
if [[ -f "$KW_DATA_DIR/$dir/$year$month/$day" ]] &&
Expand All @@ -160,7 +183,8 @@ function restore_data_from_dir()

case "$decision" in
1)
cp "$day_file" "$KW_DATA_DIR/$dir/$year$month/$day"
cmd="cp ${day_file} ${KW_DATA_DIR}/${dir}/${year}${month}/${day}"
cmd_manager "$flag" "$cmd"
continue
;;
2)
Expand All @@ -173,19 +197,24 @@ function restore_data_from_dir()
;;
esac
fi
cp "$day_file" "$KW_DATA_DIR/$dir/$year$month/$day"
cmd="cp ${day_file} ${KW_DATA_DIR}/${dir}/${year}${month}/${day}"
cmd_manager "$flag" "$cmd"
done
done
done
}

function restore_pomodoro()
{
local flag="$1"
local decision
local difference
local cmd

flag=${flag:-'SILENT'}

if [[ -d "$decompress_path/pomodoro" ]]; then
restore_data_from_dir 'pomodoro'
restore_data_from_dir 'pomodoro' "$flag"

if [[ -f "$decompress_path/pomodoro/tags" ]]; then
if [[ -f "$KW_DATA_DIR/pomodoro/tags" ]] &&
Expand All @@ -200,7 +229,8 @@ function restore_pomodoro()
fi
case "$decision" in
1)
cp "$decompress_path/pomodoro/tags" "$KW_DATA_DIR/pomodoro/tags"
cmd="cp ${decompress_path}/pomodoro/tags ${KW_DATA_DIR}/pomodoro/tags"
cmd_manager "$flag" "$cmd"
;;
2)
return
Expand All @@ -212,7 +242,8 @@ function restore_pomodoro()
esac
return
fi
cp "$decompress_path/pomodoro/tags" "$KW_DATA_DIR/pomodoro/tags"
cmd="cp ${decompress_path}/pomodoro/tags ${KW_DATA_DIR}/pomodoro/tags"
cmd_manager "$flag" "$cmd"
fi
fi
}
Expand All @@ -228,7 +259,7 @@ function restore_statistics()
# and populates the options_values variable accordingly.
function parse_backup_options()
{
local long_options='help,restore:,force'
local long_options='help,restore:,force,verbose'
local short_options='h,r:,f'

options="$(kw_parse "$short_options" "$long_options" "$@")"
Expand All @@ -243,6 +274,7 @@ function parse_backup_options()
options_values['RESTORE_PATH']=''
options_values['BACKUP_PATH']=''
options_values['FORCE']=''
options_values['VERBOSE']=''

eval "set -- $options"

Expand All @@ -264,6 +296,10 @@ function parse_backup_options()
options_values['FORCE']=1
shift
;;
--verbose)
options_values['VERBOSE']=1
shift
;;
--)
shift
;;
Expand All @@ -284,5 +320,6 @@ function backup_help()
fi
printf '%s\n' 'kw backup:' \
' backup (<path>) - Create a compressed file containing kw data' \
' backup (-r | --restore) <path> [(-f | --force)] - Extract a tar.gz file into kw data directory'
' backup (-r | --restore) <path> [(-f | --force)] - Extract a tar.gz file into kw data directory' \
' backup (--verbose) - Show a detailed output'
}
5 changes: 5 additions & 0 deletions tests/backup_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,11 @@ function test_parse_backup_options()
parse_backup_options -f
assert_equals_helper 'FORCE could not be set' "($LINENO)" '1' "${options_values['FORCE']}"

unset options_values
declare -gA options_values
parse_backup_options --verbose > /dev/null
assert_equals_helper 'VERBOSE could not be set' "($LINENO)" '1' "${options_values['VERBOSE']}"

unset options_values
declare -gA options_values
parse_backup_options /path
Expand Down

0 comments on commit 60eda44

Please sign in to comment.