diff --git a/.config/bash/module/archive.sh b/.config/bash/module/archive.sh index e1f8a59..72d7d9a 100644 --- a/.config/bash/module/archive.sh +++ b/.config/bash/module/archive.sh @@ -208,7 +208,7 @@ function unarchive() { ;; esac else - _error "Validation failed!" + _error "Failed." return 1 fi } diff --git a/.config/bash/module/checksum.sh b/.config/bash/module/checksum.sh index d949545..d2052e9 100644 --- a/.config/bash/module/checksum.sh +++ b/.config/bash/module/checksum.sh @@ -4,45 +4,20 @@ # Usage: checksum_create [FILES] function checksum_create() { local IFS=$'\n' - local targets=("${@}") - local count=0 - local total=${#} - local failed=0 + local targets=(${@}) + [[ "${targets}" = "" ]] && targets=($(_ls_file)) - # All files by default. - if [[ "${targets}" = "" ]]; then - targets=($(ls --classify | grep -v /\$)) - total=${#targets[@]} - fi - - # Iterate each file. - for target in "${targets[@]}"; do + process() { local hashfile=".${target#./}.sha1" - # Increment count. - ((count++)) - - # Status info. - local status="[${count}/${total}] ${target}" - echo -e "${color_bwhite}${status}${color_default}" - # Skip if hash exists. - [[ -f "${hashfile}" ]] && continue + [[ -f "${hashfile}" ]] && return 0 # Calculate hash. - pv "${target}" | sha1sum > ${hashfile} + pv ${target} | sha1sum > ${hashfile} + } - # Report success. - if [[ ${?} != 0 ]]; then - echo -e "${color_bred}${status}: Failed.${color_default}" - ((failed++)) - fi - done - - if [[ ${failed} != 0 ]]; then - echo -e "${color_bred}Failed: ${failed}.${color_default}" - return 1 - fi + _iterate_targets process ${targets[@]} } # Check stored values against actual files. @@ -50,51 +25,26 @@ function checksum_create() { # Usage: checksum_check [FILES] function checksum_check() { local IFS=$'\n' - local targets=("${@}") - local count=0 - local total=${#} - local failed=0 - local skipped=0 + local targets=(${@}) + [[ "${targets}" = "" ]] && targets=($(_ls_file)) - # All files by default. - if [[ "${targets}" = "" ]]; then - targets=($(ls --classify | grep -v /\$)) - total=${#targets[@]} - fi - - # Iterate each file. - for target in "${targets[@]}"; do + process() { local hashfile=".${target#./}.sha1" - # Increment count. - ((count++)) - - # Status info. - local status="[${count}/${total}] ${target}" - echo -e "${color_bwhite}${status}${color_default}" - # Skip if hash doesn't exist. - [[ -f "${hashfile}" ]] || { ((skipped++)); continue; } + [[ -f "${hashfile}" ]] || { ((skipped++)); _warn "No hash found."; return 0; } # Calculate hash. local stored=$(cat "${hashfile}" | cut -d\ -f1) local actual=$(pv "${target}" | sha1sum | cut -d\ -f1) if [[ "${stored}" != "${actual}" ]]; then - echo -e "${color_bred}${status}: Failed.${color_default}" - ((failed++)) + _error "Failed." + return 1 fi - done + } - if [[ ${skipped} != 0 ]]; then - echo -e "${color_byellow}Skipped: ${skipped}.${color_default}" - return 1 - fi - - if [[ ${failed} != 0 ]]; then - echo -e "${color_bred}Failed: ${failed}.${color_default}" - return 1 - fi + _iterate_targets process ${targets[@]} } # Calculate hashes for all files recursively and store in a file called `checksum.sha1`. diff --git a/.config/bash/module/name.sh b/.config/bash/module/name.sh index ffc1151..1d961bd 100644 --- a/.config/bash/module/name.sh +++ b/.config/bash/module/name.sh @@ -85,7 +85,7 @@ function name_hash_check() { # compare hashes. if [[ "${stored}" != "${actual}" ]]; then - _error "Validation failed!" + _error "Failed." return 1 fi } diff --git a/.config/bash/module/util.sh b/.config/bash/module/util.sh index c6feca8..4604f70 100644 --- a/.config/bash/module/util.sh +++ b/.config/bash/module/util.sh @@ -10,7 +10,7 @@ function _parse_ints() { # Function-wrapper to iterate with specified function with provided files. # By default Iterates on all non-hidden files and directories. -# List of variables available to FUNCTION: target - current file, count - current item index, total - sum of targets, failed - count of previously failed items, status - status line (not recommended to use). +# List of variables available to FUNCTION: target - current file, count - current item index, total - sum of targets, failed - count of previously failed items, skipped - count of skipped files, status - status line (not recommended to use). # Usage: _iterate_targets [FILES] function _iterate_targets() { local IFS=$'\n' @@ -19,6 +19,7 @@ function _iterate_targets() { local total=${#targets[@]} local count=0 local failed=0 + local skipped=0 local code=0 # set dafult value to target all supported archives. @@ -49,18 +50,30 @@ function _iterate_targets() { [[ "${count}" = "${total}" ]] || echo done + # Show skipped. + if [[ ${skipped} != 0 ]]; then + echo + echo -e "${color_byellow}Skipped: ${skipped}.${color_default}" + fi + # Show error. if [[ ${failed} != 0 ]]; then - echo + [[ "${skipped}" = 0 ]] && echo echo -e "${color_bred}Failed: ${failed}.${color_default}" false fi } -# Report error to stdout. +# Report an error to stdout. # Always returns code 1. -# If no message specified, prints "An error has occured". -# Usage: _error [MESSAGE] +# Usage: _error function _error() { echo -e "${color_bred}${*}${color_default}" + return 1 +} + +# Report a warning to stdout. +# Usage: _warn +function _warn() { + echo -e "${color_byellow}${*}${color_default}" }