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 ..." echo -e "Processing source $uri ..."
case "$scheme" in case "$scheme" in
file) handler='handler::tar';; file) handler='handler::tar';;
mysql) handler='handler::mysqldump';; mysql) handler='handler::mysqldump';;
postgres) handler='handler::pg_dump';; postgres) handler='handler::pg_dump';;
sqlite) handler='handler::sqlite';; sqlite) handler='handler::sqlite';;
*) # shellcheck disable=SC2154 *) # shellcheck disable=SC2154
# '__user_script' is assigned in main script. # '__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 esac
# Run handler function # Run handler function

View File

@ -46,9 +46,11 @@ log() {
while read -r line; do while read -r line; do
if [ -n "$line" ]; then if [ -n "$line" ]; then
# shellcheck disable=SC2154 # 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 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() { err() {
@ -69,7 +71,8 @@ err() {
shift shift
case "$args" in 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 # shellcheck disable=SC2086
set -- "$@" $args;; # 'args' must be unquoted! set -- "$@" $args;; # 'args' must be unquoted!
*) *)
@ -135,7 +138,8 @@ is_installed() {
if ! command -v "$1" >/dev/null 2>&1; then if ! command -v "$1" >/dev/null 2>&1; then
echo "Error: Command $1 not found." \ 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 exit 1
fi fi
} }

View File

@ -76,7 +76,8 @@ handler::mysqldump() {
$compr_cmd > "$dump_name" $compr_cmd > "$dump_name"
if [ ! -s "$dump_name" ]; then 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" rm "$dump_name"
fi fi
} }

View File

@ -45,7 +45,8 @@ handler::tar() {
fi fi
if [[ "$src_path" == "$dst_path" ]]; then 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 exit 1
fi fi
@ -67,7 +68,8 @@ handler::tar() {
# Select filename extension by compression type. # Select filename extension by compression type.
if [ "$compression" ]; then if [ "$compression" ]; then
# Make sure for the `--auto-compress` is enabled in __tar_options # 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 case "$compression" in
gzip|gz) file_ext='.tar.gz';; # gzip gzip|gz) file_ext='.tar.gz';; # gzip
tgz) file_ext='.tgz';; # gzip tgz) file_ext='.tgz';; # gzip

View File

@ -52,7 +52,9 @@ handler::cp() {
for backup in "${backups[@]}"; do for backup in "${backups[@]}"; do
log "Copying file $backup to $dst_path ..." log "Copying file $backup to $dst_path ..."
log "Run command: cp --archive $backup $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" try cp --archive "$backup" "$dst_path"
done done
fi fi

View File

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

View File

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