add internal unique number to _uniqueFilename_

This commit is contained in:
Nathaniel Landau
2021-08-19 17:12:11 -04:00
parent de803a2af9
commit 58cac3ea3b
2 changed files with 78 additions and 25 deletions

View File

@@ -356,6 +356,21 @@ _testParseYAML_() {
assert_output "hello world" assert_output "hello world"
} }
@test "_uniqueFileName_: no extension" {
touch "test"
run _uniqueFileName_ "test"
assert_output --regexp ".*/test\.1$"
}
@test "_uniqueFileName_: no extension - internal integer" {
touch "test"
touch "test.1"
run _uniqueFileName_ -i "test"
assert_output --regexp ".*/test\.2$"
}
@test "_uniqueFileName_: Count to 3" { @test "_uniqueFileName_: Count to 3" {
touch "test.txt" touch "test.txt"
touch "test.txt.1" touch "test.txt.1"
@@ -365,6 +380,15 @@ _testParseYAML_() {
assert_output --regexp ".*/test\.txt\.3$" assert_output --regexp ".*/test\.txt\.3$"
} }
@test "_uniqueFileName_: internal integer" {
touch "test.txt"
touch "test.1.txt"
touch "test.2.txt"
run _uniqueFileName_ -i "test.txt"
assert_output --regexp ".*/test\.3\.txt$"
}
@test "_uniqueFileName_: Don't confuse existing numbers" { @test "_uniqueFileName_: Don't confuse existing numbers" {
touch "test-2.txt" touch "test-2.txt"

View File

@@ -1,3 +1,4 @@
_listFiles_() { _listFiles_() {
# DESC: Find files in a directory. Use either glob or regex # DESC: Find files in a directory. Use either glob or regex
# ARGS: $1 (Required) - 'g|glob' or 'r|regex' # ARGS: $1 (Required) - 'g|glob' or 'r|regex'
@@ -45,7 +46,8 @@ _backupFile_() {
# ARGS: $1 (Required) - Source file # ARGS: $1 (Required) - Source file
# $2 (Optional) - Destination dir name used only with -d flag (defaults to ./backup) # $2 (Optional) - Destination dir name used only with -d flag (defaults to ./backup)
# OPTS: -d - Move files to a backup direcory # OPTS: -d - Move files to a backup direcory
# -m - Replaces copy (default) with move, effectively removing the # -m - Replaces copy (default) with move, effectively removing
# the original file
# OUTS: None # OUTS: None
# USAGE: _backupFile_ "sourcefile.txt" "some/backup/dir" # USAGE: _backupFile_ "sourcefile.txt" "some/backup/dir"
# NOTE: dotfiles have their leading '.' removed in their backup # NOTE: dotfiles have their leading '.' removed in their backup
@@ -53,15 +55,15 @@ _backupFile_() {
local opt local opt
local OPTIND=1 local OPTIND=1
local useDirectory=false local useDirectory=false
local moveFile=false local MOVE_FILE=false
while getopts ":dDmM" opt; do while getopts ":dDmM" opt; do
case ${opt} in case ${opt} in
d | D) useDirectory=true ;; d | D) useDirectory=true ;;
m | M) moveFile=true ;; m | M) MOVE_FILE=true ;;
*) *)
{ {
error "Unrecognized option '$1' passed to _makeSymlink_" "${LINENO}" error "Unrecognized option '${1}' passed to _backupFile_" "${LINENO}"
return 1 return 1
} }
;; ;;
@@ -71,7 +73,7 @@ _backupFile_() {
[[ $# -lt 1 ]] && fatal 'Missing required argument to _backupFile_()!' [[ $# -lt 1 ]] && fatal 'Missing required argument to _backupFile_()!'
local s="${1}" local SOURCE_FILE="${1}"
local d="${2:-backup}" local d="${2:-backup}"
local n # New filename (created by _uniqueFilename_) local n # New filename (created by _uniqueFilename_)
@@ -86,9 +88,9 @@ _backupFile_() {
warning "need function _uniqueFileName_" warning "need function _uniqueFileName_"
return 1 return 1
} }
[ ! -e "$s" ] \ [ ! -e "${SOURCE_FILE}" ] \
&& { && {
warning "Source '${s}' not found" warning "Source '${SOURCE_FILE}' not found"
return 1 return 1
} }
@@ -97,21 +99,20 @@ _backupFile_() {
[ ! -d "${d}" ] \ [ ! -d "${d}" ] \
&& _execute_ "mkdir -p \"${d}\"" "Creating backup directory" && _execute_ "mkdir -p \"${d}\"" "Creating backup directory"
if [ -e "$s" ]; then if [ -e "${SOURCE_FILE}" ]; then
n="$(basename "${s}")" n="$(_uniqueFileName_ "${d}/${SOURCE_FILE#.}")"
n="$(_uniqueFileName_ "${d}/${s#.}")" if [ ${MOVE_FILE} == true ]; then
if [ ${moveFile} == true ]; then _execute_ "mv \"${SOURCE_FILE}\" \"${d}/${n##*/}\"" "Moving: '${SOURCE_FILE}' to '${d}/${n##*/}'"
_execute_ "mv \"${s}\" \"${d}/${n##*/}\"" "Moving: '${s}' to '${d}/${n##*/}'"
else else
_execute_ "cp -R \"${s}\" \"${d}/${n##*/}\"" "Backing up: '${s}' to '${d}/${n##*/}'" _execute_ "cp -R \"${SOURCE_FILE}\" \"${d}/${n##*/}\"" "Backing up: '${SOURCE_FILE}' to '${d}/${n##*/}'"
fi fi
fi fi
else else
n="$(_uniqueFileName_ "${s}.bak")" n="$(_uniqueFileName_ "${SOURCE_FILE}.bak")"
if [ ${moveFile} == true ]; then if [ ${MOVE_FILE} == true ]; then
_execute_ "mv \"${s}\" \"${n}\"" "Moving '${s}' to '${n}'" _execute_ "mv \"${SOURCE_FILE}\" \"${n}\"" "Moving '${SOURCE_FILE}' to '${n}'"
else else
_execute_ "cp -R \"${s}\" \"${n}\"" "Backing up '${s}' to '${n}'" _execute_ "cp -R \"${SOURCE_FILE}\" \"${n}\"" "Backing up '${SOURCE_FILE}' to '${n}'"
fi fi
fi fi
} }
@@ -520,19 +521,37 @@ _sourceFile_() {
} }
_uniqueFileName_() { _uniqueFileName_() {
# DESC: Ensure a file to be created has a unique filename to avoid overwriting other files # DESC: Ensure a file to be created has a unique filename to avoid overwriting other
# filenames by appending an integer to the filename if it already exists.
# ARGS: $1 (Required) - Name of file to be created # ARGS: $1 (Required) - Name of file to be created
# $2 (Optional) - Separation characted (Defaults to a period '.') # $2 (Optional) - Separation characted (Defaults to a period '.')
# OUTS: Prints unique filename to STDOUT # OUTS: Prints unique filename to STDOUT
# OPTS: -i - Places the unique integer before the file extension
# USAGE: _uniqueFileName_ "/some/dir/file.txt" "-" # USAGE: _uniqueFileName_ "/some/dir/file.txt" "-"
local opt
local OPTIND=1
local INTERNAL_INTEGER=false
while getopts ":iI" opt; do
case ${opt} in
i | I) INTERNAL_INTEGER=true ;;
*)
{
error "Unrecognized option '${1}' passed to _uniqueFileName_" "${LINENO}"
return 1
}
;;
esac
done
shift $((OPTIND - 1))
local fullfile="${1:?_uniqueFileName_ needs a file}" local fullfile="${1:?_uniqueFileName_ needs a file}"
local spacer="${2:-.}" local spacer="${2:-.}"
local directory local directory
local filename local filename
local extension local extension
local newfile local newfile
local n local num
if ! command -v realpath >/dev/null 2>&1; then if ! command -v realpath >/dev/null 2>&1; then
error "We must have 'realpath' installed and available in \$PATH to run." error "We must have 'realpath' installed and available in \$PATH to run."
@@ -552,19 +571,29 @@ _uniqueFileName_() {
filename="$(basename "${fullfile}")" filename="$(basename "${fullfile}")"
# Extract extensions only when they exist # Extract extensions only when they exist
if [[ ${filename} =~ \.[a-zA-Z]{2,4}$ ]]; then if [[ "${filename}" =~ \.[a-zA-Z]{2,4}$ ]]; then
extension=".${filename##*.}" extension=".${filename##*.}"
filename="${filename%.*}" filename="${filename%.*}"
fi fi
if [[ "${filename}" == "${extension}" ]]; then
extension=""
fi
newfile="${directory}/${filename}${extension:-}" newfile="${directory}/${filename}${extension:-}"
if [ -e "${newfile}" ]; then if [ -e "${newfile}" ]; then
n=1 num=1
while [[ -e "${directory}/${filename}${extension:-}${spacer}${n}" ]]; do if [ "${INTERNAL_INTEGER}" = true ]; then
((n++)) while [[ -e "${directory}/${filename}${spacer}${num}${extension:-}" ]]; do
((num++))
done done
newfile="${directory}/${filename}${extension:-}${spacer}${n}" newfile="${directory}/${filename}${spacer}${num}${extension:-}"
else
while [[ -e "${directory}/${filename}${extension:-}${spacer}${num}" ]]; do
((num++))
done
newfile="${directory}/${filename}${extension:-}${spacer}${num}"
fi
fi fi
echo "${newfile}" echo "${newfile}"