sequencer - addConf support for backslash escapes
sequencer - new api function rmold to remove oldest files in a directory
This commit is contained in:
83
sequencer.sh
83
sequencer.sh
@@ -151,13 +151,13 @@ helpApi(){
|
||||
sequencer.sh API
|
||||
|
||||
The sequencer.sh build-in functions are available in all sequence functions:
|
||||
- seq_config
|
||||
- seq_config()
|
||||
If optional seq_config is defined in the sequence, it will be called once before execution of steps.
|
||||
- step_[1-${_sqr_stepMax}]_info
|
||||
- step_[1-${_sqr_stepMax}]_alias
|
||||
- step_[1-${_sqr_stepMax}]_info()
|
||||
- step_[1-${_sqr_stepMax}]_alias()
|
||||
- step_[1-${_sqr_stepMax}]_noconf=
|
||||
No need to call seq_config for this step.
|
||||
- step_[1-${_sqr_stepMax}]
|
||||
- step_[1-${_sqr_stepMax}]()
|
||||
|
||||
sequencer.sh global variables:
|
||||
|
||||
@@ -189,6 +189,7 @@ USAGE_API
|
||||
-a : append to existing file
|
||||
-s : skip if CONFIGFILE exists (no backup and entry in missing conf)
|
||||
-m : only add content to missing conf and warn user
|
||||
-e : enable interpretation of backslash escapes (ignored for -f)
|
||||
-f : <SOURCE> is a file
|
||||
<SOURCE>
|
||||
Text or file (-f) to create or added to <DESTINATION FILE>
|
||||
@@ -342,6 +343,14 @@ USAGE_API
|
||||
cat <<USAGE_API
|
||||
Returns true if seq runs non-interactive (-q|-qq).
|
||||
|
||||
USAGE_API
|
||||
echo -e "$(col green) rmold [OPTION] <DIRECTORY>$(col off)"
|
||||
cat <<USAGE_API
|
||||
Remove oldest non hidden files in DIRECTORY and retain given NUMBER (default 5).
|
||||
[OPTION]
|
||||
-h : Remove also hidden files
|
||||
-r <NUMBER> : Retain NUMBER files in Directory
|
||||
|
||||
USAGE_API
|
||||
echo -e "$(col green) root$(col off)"
|
||||
cat <<USAGE_API
|
||||
@@ -591,6 +600,9 @@ contextExe() { (( _sqr_contextExe )); }
|
||||
# Starts the detected system text editor
|
||||
editor() { exe "${_sqr_editor}" "$@"; }
|
||||
|
||||
# Escaping non-printable characters with the proposed POSIX $'' syntax
|
||||
escpath() { printf "%q" "$*"; }
|
||||
|
||||
### interactive
|
||||
# confirm [OPTIONS] [--] [QUESTION]
|
||||
# Default (empty character) = no
|
||||
@@ -679,8 +691,54 @@ ask() {
|
||||
fi
|
||||
}
|
||||
|
||||
# Escaping non-printable characters with the proposed POSIX $'' syntax
|
||||
escpath() { printf "%q" "$*"; }
|
||||
# rmold [OPTIONS] <DIRECTORY>
|
||||
# Delete oldest files from DIRECTORY and keep the 5 (default) newest
|
||||
# -r <AMOUNT FILES TO KEEP>
|
||||
rmold() {
|
||||
local arg
|
||||
local total
|
||||
local line
|
||||
local retain=5
|
||||
local target
|
||||
local hideOpt=
|
||||
|
||||
while getopts "hr:" arg ; do
|
||||
case "${arg}" in
|
||||
h) hideOpt="-A" ;;
|
||||
r) [[ ${OPTARG} =~ [0-9]+ ]] && retain="${OPTARG}" || retain="" ;;
|
||||
*) ;;
|
||||
esac
|
||||
done
|
||||
shift "$((OPTIND-1))"; OPTIND=1
|
||||
|
||||
if [ -z "${retain}" ] ; then
|
||||
error -e "Invalid retain option"
|
||||
return 1
|
||||
fi
|
||||
target="${1:-}"
|
||||
|
||||
endIfEmpty target "No directory provided"
|
||||
if [ ! -x "${target}" ] ; then
|
||||
error -e "Cannot access ${target}"
|
||||
return 1
|
||||
fi
|
||||
|
||||
# shellcheck disable=SC2012 # Use find instead
|
||||
total="$(ls ${hideOpt} -1 -- "${target}" | wc -l)"
|
||||
if (( total <= retain )) ; then
|
||||
info "Nothing to do"
|
||||
return 0
|
||||
fi
|
||||
|
||||
info "Deleting $((total - retain)) oldest files in ${target}"
|
||||
info -a "and retaining ${retain}."
|
||||
exe cd "${target}"
|
||||
# It is easier to sort with ls
|
||||
# shellcheck disable=SC2012 # Use find instead
|
||||
while IFS= read -r line; do
|
||||
exe rm -f "${line}"
|
||||
done < <(ls ${hideOpt} -rt1 "${target}" | head -$((total - retain)))
|
||||
}
|
||||
|
||||
# saveReturn <ERRNO>
|
||||
# Function returns with <ERRNO> in case step wants additional evaluation
|
||||
@@ -782,10 +840,11 @@ addConfMultiError() {
|
||||
# if all fails, a log file is created with the conflicts to be resolved by the user
|
||||
addConf() {
|
||||
local addConfBackup=
|
||||
local echoOpt=()
|
||||
local confMode=""
|
||||
local transferCmd="echo"
|
||||
|
||||
while getopts "acfms" arg ; do
|
||||
while getopts "acefms" arg ; do
|
||||
case "${arg}" in
|
||||
c) # create a new file
|
||||
addConfMultiError "${confMode}" && confMode="-c" ;;
|
||||
@@ -795,6 +854,8 @@ addConf() {
|
||||
addConfMultiError "${confMode}" && confMode="-s" ;;
|
||||
m) # only add content to missing conf and warn user
|
||||
addConfMultiError "${confMode}" && confMode="-m" ;;
|
||||
e) # Enable backslash escapes for echo
|
||||
echoOpt+=("-e") ;;
|
||||
f) # choose if source is a file or text
|
||||
transferCmd="cat" ;;
|
||||
*) ;;
|
||||
@@ -807,6 +868,8 @@ addConf() {
|
||||
exit 0
|
||||
fi
|
||||
|
||||
[[ "${transferCmd}" == "cat" ]] && echoOpt=()
|
||||
|
||||
local source="${1:-}"
|
||||
local dest="${2:?}"
|
||||
|
||||
@@ -828,7 +891,7 @@ addConf() {
|
||||
if [[ ${confMode} != "-m" ]] ; then
|
||||
# try writing config directly if it doesn't exist
|
||||
if [ ! -f "$dest" ] ; then
|
||||
"${transferCmd}" "${source}" > "${dest}"
|
||||
"${transferCmd}" "${echoOpt[@]}" "${source}" > "${dest}"
|
||||
sqr::echo "ok"
|
||||
return 0
|
||||
fi
|
||||
@@ -842,9 +905,9 @@ addConf() {
|
||||
if [ ! -f "${addConfBackup}" ] ; then
|
||||
cp -ar "${dest}" "${addConfBackup}"
|
||||
if [[ ${confMode} == "-c" ]] ; then
|
||||
"${transferCmd}" "${source}" > "${dest}"
|
||||
"${transferCmd}" "${echoOpt[@]}" "${source}" > "${dest}"
|
||||
else
|
||||
"${transferCmd}" "${source}" >> "${dest}"
|
||||
"${transferCmd}" "${echoOpt[@]}" "${source}" >> "${dest}"
|
||||
fi
|
||||
sqr::printf 'ok\n [i] %s\n' "Existing config saved to ${addConfBackup}"
|
||||
return 0
|
||||
|
Reference in New Issue
Block a user