From 63562be37a034fc684266a90970f747c5b68a669 Mon Sep 17 00:00:00 2001 From: Nathaniel Landau Date: Sat, 23 Oct 2021 23:00:49 -0400 Subject: [PATCH] update commands --- README.md | 4 +- test/alerts.bats | 8 +-- test/files.bats | 97 ++--------------------------- utilities/alerts.bash | 2 +- utilities/checks.bash | 13 +--- utilities/dates.bash | 2 +- utilities/files.bash | 141 +++++------------------------------------- 7 files changed, 30 insertions(+), 237 deletions(-) diff --git a/README.md b/README.md index 40e8f1d..bfbc255 100644 --- a/README.md +++ b/README.md @@ -225,16 +225,14 @@ Functions for working with files. - **`_decryptFile_`** Decrypts a file with `openssl` - **`_encryptFile_`** Encrypts a file with `openssl` - **`_extractArchive_`** Extract a compressed file -- **`_fileAbsPath_`** Finds the absolute path to a relative file or directory - **`_fileBasename_`** Gets the basename of a file from a file name - **`_fileContains_`** Tests whether a file contains a given pattern -- **`_fileDirectory_`** Finds the directory name from a file path +- **`_filePath_`** Gets the absolute path to a file - **`_fileExtension_`** Gets the extension of a file - **`_fileName_`** Prints a filename from a path - **`_json2yaml_`** Convert JSON to YAML uses python - **`_listFiles_`** Find files in a directory. Use either glob or regex. - **`_makeSymlink_`** Creates a symlink and backs up a file which may be overwritten by the new symlink. If the exact same symlink already exists, nothing is done. -- **`_parseFilename_`** Break a filename into its component parts which and place them into global variables for use in your script (dir, basename, extension, path, etc.) - **`_parseYAML_`** Convert a YAML file into BASH variables for use in a shell script - **`_readFile_`** Prints each line of a file - **`_sourceFile_`** Source a file into a script diff --git a/test/alerts.bats b/test/alerts.bats index ade562a..74c8e68 100755 --- a/test/alerts.bats +++ b/test/alerts.bats @@ -84,7 +84,7 @@ teardown() { @test "_alert_: notice: with LINE" { run notice "testing" "$LINENO" - assert_output --regexp ".*\[ notice\] testing .*\(line: [0-9]{1,3}\)" + assert_output --regexp "\[ notice\] testing .*\(line: [0-9]{1,3}\)" } @test "_alert_: refute debug" { @@ -105,17 +105,17 @@ teardown() { @test "_alert_: info" { run info "testing" - assert_output --regexp "[0-9]+:[0-9]+:[0-9]+ (AM|PM) \[ info\] testing" + assert_output --regexp "\[ info\] testing" } @test "_alert_: fatal: with LINE" { run fatal "testing" "$LINENO" - assert_line --index 0 --regexp ".*\[ fatal\] testing .*\(line: [0-9]{1,3}\) \( run:.*\)" + assert_line --index 0 --regexp "\[ fatal\] testing .*\(line: [0-9]{1,3}\) \(.*\)" } @test "_alert_: error" { run error "testing" - assert_output --regexp ".*\[ error\] testing .*\( run:.*\)" + assert_output --regexp "\[ error\] testing .*\(.*\)" } @test "_alert_: input" { diff --git a/test/files.bats b/test/files.bats index f26d055..4d71280 100755 --- a/test/files.bats +++ b/test/files.bats @@ -188,80 +188,6 @@ _testListFiles_() { } } -_testParseFilename_() { - - @test "_parseFilename_: file with one extension" { - - touch "testfile.txt" - VERBOSE=true - run _parseFilename_ "testfile.txt" - set +o nounset - - assert_success - assert_line --index 0 --regexp "\[ debug\].*{PARSE_FULL}: /.*testfile\.txt$" - assert_line --index 1 --regexp "\[ debug\].*${PARSE_BASE}: testfile\.txt$" - assert_line --index 2 --regexp "\[ debug\].*${PARSE_PATH}: /.*" - assert_line --index 3 --regexp "\[ debug\].*${PARSE_EXT}: txt$" - assert_line --index 4 --regexp "\[ debug\].*${PARSE_BASENOEXT}: testfile$" - } - - @test "_parseFilename_: file with dots in name" { - - touch "testfile.for.testing.txt" - VERBOSE=true - run _parseFilename_ "testfile.for.testing.txt" - set +o nounset - assert_success - assert_line --index 0 --regexp "\[ debug\].*{PARSE_FULL}: /.*testfile\.for\.testing\.txt$" - assert_line --index 1 --regexp "\[ debug\].*${PARSE_BASE}: testfile\.for\.testing\.txt$" - assert_line --index 2 --regexp "\[ debug\].*${PARSE_PATH}: /.*" - assert_line --index 3 --regexp "\[ debug\].*${PARSE_EXT}: txt$" - assert_line --index 4 --regexp "\[ debug\].*${PARSE_BASENOEXT}: testfile\.for\.testing$" - } - - @test "_parseFilename_: file with no extension" { - - touch "testfile" - VERBOSE=true - run _parseFilename_ "testfile" - set +o nounset - assert_success - assert_line --index 0 --regexp "\[ debug\].*{PARSE_FULL}: /.*testfile$" - assert_line --index 1 --regexp "\[ debug\].*${PARSE_BASE}: testfile$" - assert_line --index 2 --regexp "\[ debug\].*${PARSE_PATH}: /.*" - assert_line --index 3 --regexp "\[ debug\].*${PARSE_EXT}: $" - assert_line --index 4 --regexp "\[ debug\].*${PARSE_BASENOEXT}: testfile$" - } - - @test "_parseFilename_: file with tar.gz" { - - touch "testfile.tar.gz" - VERBOSE=true - run _parseFilename_ "testfile.tar.gz" - set +o nounset - - assert_success - assert_line --index 0 --regexp "\[ debug\].*{PARSE_FULL}: /.*testfile\.tar\.gz$" - assert_line --index 1 --regexp "\[ debug\].*${PARSE_BASE}: testfile\.tar\.gz$" - assert_line --index 2 --regexp "\[ debug\].*${PARSE_PATH}: /.*" - assert_line --index 3 --regexp "\[ debug\].*${PARSE_EXT}: tar\.gz$" - assert_line --index 4 --regexp "\[ debug\].*${PARSE_BASENOEXT}: testfile$" - } - - @test "_parseFilename_: file with three extensions" { - touch "testfile.tar.gzip.bzip" - VERBOSE=true - run _parseFilename_ -n3 "testfile.tar.gzip.bzip" - set +o nounset - assert_success - assert_line --index 0 --regexp "\[ debug\].*{PARSE_FULL}: /.*testfile\.tar\.gzip\.bzip$" - assert_line --index 1 --regexp "\[ debug\].*${PARSE_BASE}: testfile\.tar\.gzip\.bzip$" - assert_line --index 2 --regexp "\[ debug\].*${PARSE_PATH}: /.*" - assert_line --index 3 --regexp "\[ debug\].*${PARSE_EXT}: tar\.gzip\.bzip$" - assert_line --index 4 --regexp "\[ debug\].*${PARSE_BASENOEXT}: testfile$" - } -} - _testMakeSymlink_() { @test "_makeSymlink_: Fail with no source fire" { @@ -477,29 +403,17 @@ _testParseYAML_() { assert_output "tar.bz2" } -@test "_fileDirectory_" { - run _fileDirectory_ "path/to/file/test.txt" +@test "_filePath_: does not exist" { + run _filePath_ "path/to/file/test.txt" assert_success assert_output "path/to/file" } -@test "_fileAbsPath_: file" { +@test "_filePath_: exists" { touch "./test.txt" - run _fileAbsPath_ "./test.txt" + run _filePath_ "./test.txt" assert_success - assert_output --regexp "/.*/files\.bats.*/test\.txt$" -} - -@test "_fileAbsPath_: directory" { - mkdir "./testdir" - run _fileAbsPath_ "./testdir" - assert_success - assert_output --regexp "/.*/files\.bats.*/testdir$" -} - -@test "_fileAbsPath_: fail when not found" { - run _fileAbsPath_ "./test.txt" - assert_failure + assert_output --regexp "^/.*/files\.bats-" } @test "_fileContains_: No match" { @@ -516,6 +430,5 @@ _testParseYAML_() { _testBackupFile_ _testListFiles_ -_testParseFilename_ _testMakeSymlink_ _testParseYAML_ diff --git a/utilities/alerts.bash b/utilities/alerts.bash index 4a822f4..6d4abf9 100644 --- a/utilities/alerts.bash +++ b/utilities/alerts.bash @@ -115,7 +115,7 @@ _alert_() { reset="" fi - printf "%s ${_color}[%7s] %s${reset}\n" "$(date +"%r")" "${_alertType}" "${_message}" + printf "${_color}[%7s] %s${reset}\n" "${_alertType}" "${_message}" } _writeToScreen_ diff --git a/utilities/checks.bash b/utilities/checks.bash index 1958173..c99166b 100644 --- a/utilities/checks.bash +++ b/utilities/checks.bash @@ -10,6 +10,7 @@ _commandExists_() { # 1 if false # USAGE: # (_commandExists_ ffmpeg ) && [SUCCESS] || [FAILURE] + [[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}" if ! command -v "$1" >/dev/null 2>&1; then @@ -50,8 +51,6 @@ _isAlpha_() { # 1 - Input contains non-alphabetic characters # USAGE: # _isAlpha_ "${var}" - # NOTES: - # [[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}" local _re='^[[:alpha:]]+$' @@ -71,8 +70,6 @@ _isAlphaNum_() { # 1 - Input contains alpha-numeric characters # USAGE: # _isAlphaNum_ "${var}" - # NOTES: - # [[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}" local _re='^[[:alnum:]]+$' @@ -92,8 +89,6 @@ _isAlphaDash_() { # 1 - Input is not only alpha-numeric or dash or underscore characters # USAGE: # _isAlphaDash_ "${var}" - # NOTES: - # [[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}" local _re='^[[:alnum:]_-]+$' @@ -136,8 +131,6 @@ _isInternetAvailable_() { # stdout: # USAGE: # _isInternetAvailable_ - # NOTES: - # local _checkInternet if [[ -t 1 || -z ${TERM} ]]; then @@ -298,7 +291,7 @@ _varIsTrue_() { [[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}" - [[ ${1} == true || ${1} -eq 0 ]] && return 0 || return 1 + [[ ${1} == "true" || ${1} == 0 ]] && return 0 || return 1 } _varIsFalse_() { @@ -314,7 +307,7 @@ _varIsFalse_() { [[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}" - [[ ${1} == false || ${1} -eq 1 ]] && return 0 || return 1 + [[ ${1} == false || ${1} == 1 ]] && return 0 || return 1 } _varIsEmpty_() { diff --git a/utilities/dates.bash b/utilities/dates.bash index 94c4a5c..26735c6 100644 --- a/utilities/dates.bash +++ b/utilities/dates.bash @@ -232,7 +232,7 @@ _parseDate_() { trap "$(shopt -p nocasematch)" RETURN # reset nocasematch when function exits shopt -s nocasematch # Use case-insensitive regex - debug "_parseDate_() input ${tan}$date${purple}" + debug "_parseDate_() input ${tan}${_stringToTest}${purple}" # YYYY MM DD or YYYY-MM-DD _pat="(.*[^0-9]|^)((20[0-2][0-9])[-\.\/_ ]+([0-9]{1,2})[-\.\/_ ]+([0-9]{1,2}))([^0-9].*|$)" diff --git a/utilities/files.bash b/utilities/files.bash index 6dc8967..74c6280 100644 --- a/utilities/files.bash +++ b/utilities/files.bash @@ -339,7 +339,7 @@ _fileName_() { # _fileName_ "some/path/to/file.txt" --> "file.txt" # _fileName_ "some/path/to/file" --> "file" [[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}" - printf "%s" "${1##*/}" + printf "%s\n" "${1##*/}" } @@ -404,16 +404,15 @@ _fileExtension_() { fi _fn=${_fn%.$_ext} done - debug "_exts: $_exts" [[ ${_file} == "${_exts}" ]] && return 1 printf "%s" "${_exts}" } -_fileDirectory_() { +_filePath_() { # DESC: - # Finds the directory name from a file path + # Finds the directory name from a file path. If it exists on filesystem, print absolute path. If a string, remove the filename and return the path # ARGS: # $1 (Required) - Input string path # OUTS: @@ -421,47 +420,24 @@ _fileDirectory_() { # 1 - Failure # stdout: Directory path # USAGE: - # _fileDirectory_ "some/path/to/file.txt" --> "some/path/to" + # _fileDir_ "some/path/to/file.txt" --> "some/path/to" # CREDIT: # https://github.com/labbots/bash-utility/ [[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}" - local _tmp=${1:-.} + local _tmp=${1} - [[ ${_tmp} != *[!/]* ]] && { printf '/\n' && return; } - _tmp="${_tmp%%"${_tmp##*[!/]}"}" - - [[ ${_tmp} != */* ]] && { printf '.\n' && return; } - _tmp=${_tmp%/*} && _tmp="${_tmp%%"${_tmp##*[!/]}"}" - - printf '%s' "${_tmp:-/}" -} - -_fileAbsPath_() { - # DESC: - # Gets the absolute path of a file or directory - # ARGS: - # $1 (Required) - Relative path to a file or directory - # OUTS: - # 0 - Success - # 1 - If file/directory does not exist - # stdout: String relative or absolute path to file/directory - # USAGE: - # _fileAbsPath_ "../path/to/file.md" --> /home/user/docs/path/to/file.md - # CREDIT: - # https://github.com/labbots/bash-utility/ - - [[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}" - - local _input="${1}" - if [[ -f ${_input} ]]; then - printf "%s/%s\n" "$(cd "$(_fileDirectory_ "${_input}")" && pwd)" "${_input##*/}" - elif [[ -d ${_input} ]]; then - printf "%s\n" "$(cd "${_input}" && pwd)" + if [ -e "${_tmp}" ]; then + _tmp="$(dirname "$(realpath "${_tmp}")")" else - return 1 + [[ ${_tmp} != *[!/]* ]] && { printf '/\n' && return; } + _tmp="${_tmp%%"${_tmp##*[!/]}"}" + + [[ ${_tmp} != */* ]] && { printf '.\n' && return; } + _tmp=${_tmp%/*} && _tmp="${_tmp%%"${_tmp##*[!/]}"}" fi + printf '%s' "${_tmp:-/}" } _fileContains_() { @@ -523,12 +499,12 @@ _listFiles_() { [Gg]*) while read -r _fileMatch; do printf "%s\n" "$(realpath "${_fileMatch}")" - done < <(find "${_directory}" -iname "${_pattern}" -type f -maxdepth 1 | sort) + done < <(find "${_directory}" -maxdepth 1 -iname "${_pattern}" -type f | sort) ;; [Rr]*) while read -r _fileMatch; do printf "%s\n" "$(realpath "${_fileMatch}")" - done < <(find "${_directory}" -iregex "${_pattern}" -type f -maxdepth 1 | sort) + done < <(find "${_directory}" -maxdepth 1 -iregex "${_pattern}" -type f | sort) ;; *) fatal "_listFiles_: Could not determine if search was glob or regex" @@ -654,93 +630,6 @@ _makeSymlink_() { return 0 } -_parseFilename_() { - # DESC: - # Break a filename into its component parts which and place them into prefixed - # variables for use in your script. Run with VERBOSE=true to see the variables while - # running your script. - # ARGS: - # $1 (Required) - Path to file to parse. (Must exist in filesystem) - # OPTS: - # -n - optional flag for number of extension levels (Ex: -n2) - # OUTS: - # 0 - Success - # 1 - Error - # Variables created - # $PARSE_FULL - File and its real path (ie, resolve symlinks) - # $PARSE_PATH - Path to the file - # $PARSE_BASE - Name of the file WITH extension - # $PARSE_BASENOEXT - Name of file WITHOUT extension - # $PARSE_EXT - The extension of the file - # USAGE: - # _parseFilename_ "some/file.txt" - - # Error handling - if [[ $# -lt 1 ]] \ - || ! command -v dirname &>/dev/null \ - || ! command -v basename &>/dev/null \ - || ! command -v realpath &>/dev/null; then - fatal "Missing dependency or input to ${FUNCNAME[0]}" - fi - - local _levels - local option - local _exts - local _ext - local i - local _fn - - local OPTIND=1 - while getopts ":n:" option; do - case ${option} in - n) _levels=${OPTARG} ;; - *) continue ;; - esac - done && shift $((OPTIND - 1)) - - local _fileToParse="${1}" - - if [ ! -f "${_fileToParse}" ]; then - debug "_parseFile_: Could not find file: ${_fileToParse}" - return 1 - fi - - PARSE_FULL="$(realpath "${_fileToParse}")" \ - && debug "\${PARSE_FULL}: ${PARSE_FULL:-}" - PARSE_BASE=$(basename "${_fileToParse}") \ - && debug "\${PARSE_BASE}: ${PARSE_BASE}" - PARSE_PATH="$(realpath "$(dirname "${_fileToParse}")")" \ - && debug "\${PARSE_PATH}: ${PARSE_PATH:-}" - - # Detect some common multi-extensions - if [[ -z ${_levels:-} ]]; then - case $(tr '[:upper:]' '[:lower:]' <<<"${PARSE_BASE}") in - *.tar.gz | *.tar.bz2 | *.log.[0-9]) _levels=2 ;; - *) _levels=1 ;; - esac - fi - - # Find Extension - _fn="${PARSE_BASE}" - for ((i = 0; i < _levels; i++)); do - _ext=${_fn##*.} - if [ $i == 0 ]; then - _exts=${_ext}${_exts:-} - else - _exts=${_ext}.${_exts:-} - fi - _fn=${_fn%.$_ext} - done - if [[ ${_exts} == "${PARSE_BASE}" ]]; then - PARSE_EXT="" && debug "\${PARSE_EXT}: ${PARSE_EXT}" - else - PARSE_EXT="${_exts}" && debug "\${PARSE_EXT}: ${PARSE_EXT}" - fi - - PARSE_BASENOEXT="${PARSE_BASE%.$PARSE_EXT}" \ - && debug "\${PARSE_BASENOEXT}: ${PARSE_BASENOEXT}" -} - _parseYAML_() { # DESC: # Convert a YAML file into BASH variables for use in a shell script