feat: Code refactoring

This commit is contained in:
ge 2022-08-13 18:33:12 +03:00
parent 3e61338c0d
commit a5284b303d
7 changed files with 38 additions and 19 deletions

View File

@ -33,13 +33,14 @@ process_source() {
echo -e "Processing source $uri ..."
case "$scheme" in
file) handler='handler::tar';;
mysql) handler='handler::mysqldump';;
postgres) handler='handler::pg_dump';;
sqlite) handler='handler::sqlite';;
file) handler='handler::tar';;
mysql) handler='handler::mysqldump';;
postgres) handler='handler::pg_dump';;
sqlite) handler='handler::sqlite';;
*) # shellcheck disable=SC2154
# '__user_script' is assigned in main script.
echo "Error: $__user_script: Unsupported URI scheme: $scheme" >&2; exit 1;;
echo "Error: $__user_script: Unsupported URI scheme: $scheme" >&2
exit 1;;
esac
# Run handler function

View File

@ -46,9 +46,11 @@ log() {
while read -r line; do
if [ -n "$line" ]; then
# shellcheck disable=SC2154
printf '[%s] %s\n' "$(date +"$log_date_fmt")" "$line" >> "$__log_file"
printf '[%s] %s\n' "$(date +"$log_date_fmt")" "$line" \
>> "$__log_file"
fi
done <<< "$(sed -r 's/\x1B\[(([0-9]+)(;[0-9]+)*)?[m,K,H,f,J]//g' <<< "$message")"
done <<< "$(<<< "$message" \
sed -r 's/\x1B\[(([0-9]+)(;[0-9]+)*)?[m,K,H,f,J]//g')"
}
err() {
@ -69,7 +71,8 @@ err() {
shift
case "$args" in
-*)
args="$(echo "${args:1}" | grep -o . | xargs -I {} echo -n '-{} ')"
args="$(echo "${args:1}" | grep -o . |
xargs -I {} echo -n '-{} ')"
# shellcheck disable=SC2086
set -- "$@" $args;; # 'args' must be unquoted!
*)
@ -135,7 +138,8 @@ is_installed() {
if ! command -v "$1" >/dev/null 2>&1; then
echo "Error: Command $1 not found." \
"Please install $1 or check your PATH if it's actually installed." >&2
"Please install $1 or check your" \
"PATH if it's actually installed." >&2
exit 1
fi
}

View File

@ -76,7 +76,8 @@ handler::mysqldump() {
$compr_cmd > "$dump_name"
if [ ! -s "$dump_name" ]; then
err -e "Something went wrong: Dump size is 0 bytes. Removing $dump_name"
err -e "Something went wrong:" \
"Dump size is 0 bytes. Removing $dump_name"
rm "$dump_name"
fi
}

View File

@ -45,7 +45,8 @@ handler::tar() {
fi
if [[ "$src_path" == "$dst_path" ]]; then
echo "Error: Source and destination paths is the same: $src_path; $dst_path" >&2
echo "Error: Source and destination paths is the same:" \
"$src_path; $dst_path" >&2
exit 1
fi
@ -67,7 +68,8 @@ handler::tar() {
# Select filename extension by compression type.
if [ "$compression" ]; then
# Make sure for the `--auto-compress` is enabled in __tar_options
# Refference: https://www.gnu.org/software/tar/manual/html_node/gzip.html
# Refference:
# https://www.gnu.org/software/tar/manual/html_node/gzip.html
case "$compression" in
gzip|gz) file_ext='.tar.gz';; # gzip
tgz) file_ext='.tgz';; # gzip

View File

@ -52,7 +52,9 @@ handler::cp() {
for backup in "${backups[@]}"; do
log "Copying file $backup to $dst_path ..."
log "Run command: cp --archive $backup $dst_path"
[ "$__verbose" ] && echo "Run command: cp --archive $backup $dst_path"
if [ "$__verbose" ]; then
echo "Run command: cp --archive $backup $dst_path"
fi
try cp --archive "$backup" "$dst_path"
done
fi

View File

@ -36,7 +36,8 @@ validate_sources() {
case "$scheme" in
file|mysql|postgres|sqlite) : ;; # do nothing, this is OK
*) # shellcheck disable=SC2154
echo "Error: $__user_script: Unsupported URI scheme: $scheme" >&2; exit 1;;
echo "Error: $__user_script:" \
"Unsupported URI scheme: $scheme" >&2; exit 1;;
esac
done
}
@ -65,7 +66,11 @@ validate_targets() {
file_targets+=("$uri")
fi
;;
*) echo "Error: $__user_script: Unsupported URI scheme: $scheme" >&2; exit 1;;
*)
echo "Error: $__user_script:" \
"Unsupported URI scheme: $scheme" >&2
exit 1
;;
esac
done
@ -83,7 +88,8 @@ validate_targets() {
if [ -d "$path" ]; then
__main_target_path="$path"
else
echo "Error: $__user_script: Path '$path' from URI '$__main_target'" \
echo "Error: $__user_script:" \
"Path '$path' from URI '$__main_target'" \
"does not exists or not a directory" >&2
exit 1
fi

View File

@ -80,12 +80,15 @@ parse_uri() {
fragment="$(<<< "$uri" grep -Po '(?<=#)(.*)')" || true
# * Get query
query="$(<<< "$uri" grep -Po '(?<=\?)(.*)' | sed "s/#${fragment}//g")" || true
query="$(<<< "$uri" grep -Po '(?<=\?)(.*)' |
sed "s/#${fragment}//g")" || true
# * Get path
if [[ "$uri" =~ ^${scheme}:// ]]; then
path="$(<<< "$uri" sed "s/${scheme}:\/\/${authority//\[/\\[}//g")" || true
path="$(<<< "$path" sed "s/\?${query}//g;s/#${fragment}//g")" || true
path="$(<<< "$uri" sed "s/${scheme}:\/\/${authority//\[/\\[}//g")" \
|| true
path="$(<<< "$path" sed "s/\?${query}//g;s/#${fragment}//g")" \
|| true
# Dirty hack for 'schema://host:~/path'
if [[ "$uri" =~ :~/ ]]; then
path="~$path"