From a5284b303dd48f4ef18ac8de4c381d5942a00ab0 Mon Sep 17 00:00:00 2001 From: ge Date: Sat, 13 Aug 2022 18:33:12 +0300 Subject: [PATCH] feat: Code refactoring --- src/lib/backup.sh | 11 ++++++----- src/lib/common.sh | 12 ++++++++---- src/lib/handlers/sources/mysqldump.sh | 3 ++- src/lib/handlers/sources/tar.sh | 6 ++++-- src/lib/handlers/targets/cp.sh | 4 +++- src/lib/source.sh | 12 +++++++++--- src/lib/uri.sh | 9 ++++++--- 7 files changed, 38 insertions(+), 19 deletions(-) diff --git a/src/lib/backup.sh b/src/lib/backup.sh index ae1b05c..7dcced8 100644 --- a/src/lib/backup.sh +++ b/src/lib/backup.sh @@ -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 diff --git a/src/lib/common.sh b/src/lib/common.sh index 2cb4557..55497b8 100644 --- a/src/lib/common.sh +++ b/src/lib/common.sh @@ -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 } diff --git a/src/lib/handlers/sources/mysqldump.sh b/src/lib/handlers/sources/mysqldump.sh index 840dc4f..e64523f 100644 --- a/src/lib/handlers/sources/mysqldump.sh +++ b/src/lib/handlers/sources/mysqldump.sh @@ -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 } diff --git a/src/lib/handlers/sources/tar.sh b/src/lib/handlers/sources/tar.sh index 848aeb1..6414fd0 100644 --- a/src/lib/handlers/sources/tar.sh +++ b/src/lib/handlers/sources/tar.sh @@ -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 diff --git a/src/lib/handlers/targets/cp.sh b/src/lib/handlers/targets/cp.sh index d61430a..6e65902 100644 --- a/src/lib/handlers/targets/cp.sh +++ b/src/lib/handlers/targets/cp.sh @@ -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 diff --git a/src/lib/source.sh b/src/lib/source.sh index b06bd0e..56bd70d 100644 --- a/src/lib/source.sh +++ b/src/lib/source.sh @@ -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 diff --git a/src/lib/uri.sh b/src/lib/uri.sh index 08fe442..ee93815 100644 --- a/src/lib/uri.sh +++ b/src/lib/uri.sh @@ -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"