update commands

This commit is contained in:
Nathaniel Landau
2021-10-23 23:00:49 -04:00
parent 46d7edf726
commit 63562be37a
7 changed files with 30 additions and 237 deletions

View File

@@ -225,16 +225,14 @@ Functions for working with files.
- **`_decryptFile_`** Decrypts a file with `openssl` - **`_decryptFile_`** Decrypts a file with `openssl`
- **`_encryptFile_`** Encrypts a file with `openssl` - **`_encryptFile_`** Encrypts a file with `openssl`
- **`_extractArchive_`** Extract a compressed file - **`_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 - **`_fileBasename_`** Gets the basename of a file from a file name
- **`_fileContains_`** Tests whether a file contains a given pattern - **`_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 - **`_fileExtension_`** Gets the extension of a file
- **`_fileName_`** Prints a filename from a path - **`_fileName_`** Prints a filename from a path
- **`_json2yaml_`** Convert JSON to YAML uses python - **`_json2yaml_`** Convert JSON to YAML uses python
- **`_listFiles_`** Find files in a directory. Use either glob or regex. - **`_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. - **`_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 - **`_parseYAML_`** Convert a YAML file into BASH variables for use in a shell script
- **`_readFile_`** Prints each line of a file - **`_readFile_`** Prints each line of a file
- **`_sourceFile_`** Source a file into a script - **`_sourceFile_`** Source a file into a script

View File

@@ -84,7 +84,7 @@ teardown() {
@test "_alert_: notice: with LINE" { @test "_alert_: notice: with LINE" {
run notice "testing" "$LINENO" 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" { @test "_alert_: refute debug" {
@@ -105,17 +105,17 @@ teardown() {
@test "_alert_: info" { @test "_alert_: info" {
run info "testing" 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" { @test "_alert_: fatal: with LINE" {
run fatal "testing" "$LINENO" 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" { @test "_alert_: error" {
run error "testing" run error "testing"
assert_output --regexp ".*\[ error\] testing .*\( run:.*\)" assert_output --regexp "\[ error\] testing .*\(.*\)"
} }
@test "_alert_: input" { @test "_alert_: input" {

View File

@@ -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_() { _testMakeSymlink_() {
@test "_makeSymlink_: Fail with no source fire" { @test "_makeSymlink_: Fail with no source fire" {
@@ -477,29 +403,17 @@ _testParseYAML_() {
assert_output "tar.bz2" assert_output "tar.bz2"
} }
@test "_fileDirectory_" { @test "_filePath_: does not exist" {
run _fileDirectory_ "path/to/file/test.txt" run _filePath_ "path/to/file/test.txt"
assert_success assert_success
assert_output "path/to/file" assert_output "path/to/file"
} }
@test "_fileAbsPath_: file" { @test "_filePath_: exists" {
touch "./test.txt" touch "./test.txt"
run _fileAbsPath_ "./test.txt" run _filePath_ "./test.txt"
assert_success assert_success
assert_output --regexp "/.*/files\.bats.*/test\.txt$" assert_output --regexp "^/.*/files\.bats-"
}
@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
} }
@test "_fileContains_: No match" { @test "_fileContains_: No match" {
@@ -516,6 +430,5 @@ _testParseYAML_() {
_testBackupFile_ _testBackupFile_
_testListFiles_ _testListFiles_
_testParseFilename_
_testMakeSymlink_ _testMakeSymlink_
_testParseYAML_ _testParseYAML_

View File

@@ -115,7 +115,7 @@ _alert_() {
reset="" reset=""
fi fi
printf "%s ${_color}[%7s] %s${reset}\n" "$(date +"%r")" "${_alertType}" "${_message}" printf "${_color}[%7s] %s${reset}\n" "${_alertType}" "${_message}"
} }
_writeToScreen_ _writeToScreen_

View File

@@ -10,6 +10,7 @@ _commandExists_() {
# 1 if false # 1 if false
# USAGE: # USAGE:
# (_commandExists_ ffmpeg ) && [SUCCESS] || [FAILURE] # (_commandExists_ ffmpeg ) && [SUCCESS] || [FAILURE]
[[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}" [[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
if ! command -v "$1" >/dev/null 2>&1; then if ! command -v "$1" >/dev/null 2>&1; then
@@ -50,8 +51,6 @@ _isAlpha_() {
# 1 - Input contains non-alphabetic characters # 1 - Input contains non-alphabetic characters
# USAGE: # USAGE:
# _isAlpha_ "${var}" # _isAlpha_ "${var}"
# NOTES:
#
[[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}" [[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
local _re='^[[:alpha:]]+$' local _re='^[[:alpha:]]+$'
@@ -71,8 +70,6 @@ _isAlphaNum_() {
# 1 - Input contains alpha-numeric characters # 1 - Input contains alpha-numeric characters
# USAGE: # USAGE:
# _isAlphaNum_ "${var}" # _isAlphaNum_ "${var}"
# NOTES:
#
[[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}" [[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
local _re='^[[:alnum:]]+$' local _re='^[[:alnum:]]+$'
@@ -92,8 +89,6 @@ _isAlphaDash_() {
# 1 - Input is not only alpha-numeric or dash or underscore characters # 1 - Input is not only alpha-numeric or dash or underscore characters
# USAGE: # USAGE:
# _isAlphaDash_ "${var}" # _isAlphaDash_ "${var}"
# NOTES:
#
[[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}" [[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
local _re='^[[:alnum:]_-]+$' local _re='^[[:alnum:]_-]+$'
@@ -136,8 +131,6 @@ _isInternetAvailable_() {
# stdout: # stdout:
# USAGE: # USAGE:
# _isInternetAvailable_ # _isInternetAvailable_
# NOTES:
#
local _checkInternet local _checkInternet
if [[ -t 1 || -z ${TERM} ]]; then if [[ -t 1 || -z ${TERM} ]]; then
@@ -298,7 +291,7 @@ _varIsTrue_() {
[[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}" [[ $# == 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_() { _varIsFalse_() {
@@ -314,7 +307,7 @@ _varIsFalse_() {
[[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}" [[ $# == 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_() { _varIsEmpty_() {

View File

@@ -232,7 +232,7 @@ _parseDate_() {
trap "$(shopt -p nocasematch)" RETURN # reset nocasematch when function exits trap "$(shopt -p nocasematch)" RETURN # reset nocasematch when function exits
shopt -s nocasematch # Use case-insensitive regex 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 # 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].*|$)" _pat="(.*[^0-9]|^)((20[0-2][0-9])[-\.\/_ ]+([0-9]{1,2})[-\.\/_ ]+([0-9]{1,2}))([^0-9].*|$)"

View File

@@ -339,7 +339,7 @@ _fileName_() {
# _fileName_ "some/path/to/file.txt" --> "file.txt" # _fileName_ "some/path/to/file.txt" --> "file.txt"
# _fileName_ "some/path/to/file" --> "file" # _fileName_ "some/path/to/file" --> "file"
[[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}" [[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
printf "%s" "${1##*/}" printf "%s\n" "${1##*/}"
} }
@@ -404,16 +404,15 @@ _fileExtension_() {
fi fi
_fn=${_fn%.$_ext} _fn=${_fn%.$_ext}
done done
debug "_exts: $_exts"
[[ ${_file} == "${_exts}" ]] && return 1 [[ ${_file} == "${_exts}" ]] && return 1
printf "%s" "${_exts}" printf "%s" "${_exts}"
} }
_fileDirectory_() { _filePath_() {
# DESC: # 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: # ARGS:
# $1 (Required) - Input string path # $1 (Required) - Input string path
# OUTS: # OUTS:
@@ -421,47 +420,24 @@ _fileDirectory_() {
# 1 - Failure # 1 - Failure
# stdout: Directory path # stdout: Directory path
# USAGE: # USAGE:
# _fileDirectory_ "some/path/to/file.txt" --> "some/path/to" # _fileDir_ "some/path/to/file.txt" --> "some/path/to"
# CREDIT: # CREDIT:
# https://github.com/labbots/bash-utility/ # https://github.com/labbots/bash-utility/
[[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}" [[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
local _tmp=${1:-.} local _tmp=${1}
if [ -e "${_tmp}" ]; then
_tmp="$(dirname "$(realpath "${_tmp}")")"
else
[[ ${_tmp} != *[!/]* ]] && { printf '/\n' && return; } [[ ${_tmp} != *[!/]* ]] && { printf '/\n' && return; }
_tmp="${_tmp%%"${_tmp##*[!/]}"}" _tmp="${_tmp%%"${_tmp##*[!/]}"}"
[[ ${_tmp} != */* ]] && { printf '.\n' && return; } [[ ${_tmp} != */* ]] && { printf '.\n' && return; }
_tmp=${_tmp%/*} && _tmp="${_tmp%%"${_tmp##*[!/]}"}" _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)"
else
return 1
fi fi
printf '%s' "${_tmp:-/}"
} }
_fileContains_() { _fileContains_() {
@@ -523,12 +499,12 @@ _listFiles_() {
[Gg]*) [Gg]*)
while read -r _fileMatch; do while read -r _fileMatch; do
printf "%s\n" "$(realpath "${_fileMatch}")" 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]*) [Rr]*)
while read -r _fileMatch; do while read -r _fileMatch; do
printf "%s\n" "$(realpath "${_fileMatch}")" 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" fatal "_listFiles_: Could not determine if search was glob or regex"
@@ -654,93 +630,6 @@ _makeSymlink_() {
return 0 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_() { _parseYAML_() {
# DESC: # DESC:
# Convert a YAML file into BASH variables for use in a shell script # Convert a YAML file into BASH variables for use in a shell script