Skip to content

Commit

Permalink
qc-build-index new option -n <name>
Browse files Browse the repository at this point in the history
With this new option a update of the index can be named. This might be
useful when logging the output, but is especially thought for crontab
handling.

When using qc --cron the entry with the name "default update" is handled,
other entries, using other names are ignored.
  • Loading branch information
rakus committed Dec 29, 2023
1 parent 63ba78c commit 38a425a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 24 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -343,12 +343,20 @@ The command `qc` has a own option to manipulate the crontab. With

the following entry is added to the crontab

# Quick Change Directory: update index
*/10 * * * * ${HOME}/.qc/qc-build-index >${HOME}/.qc/index/qc-build-index.log 2>&1
# Quick Change Directory: default update
*/10 * * * * ${HOME}/.qc/qc-build-index -n "default update" >${HOME}/.qc/index/qc-build-index.log 2>&1

With `qc --cron` the current crontab entry is shown and with `qc --cron 0` the
entry is removed.

**NOTES**

* If you want to add other custom crontab entries for qc-build-index, use a
different name than "default update" (in the comment and for the `-n` option),
so the entry won't be processed by `qc --cron`.
* Older versions of qc created a slightly different entry in crontab, that is
not detected by the current version. Use `crontab -e` to clean that up.


### The Manual Index

Expand Down
56 changes: 34 additions & 22 deletions qc-build-index
Original file line number Diff line number Diff line change
Expand Up @@ -52,29 +52,32 @@ fi
show_help()
{
echo "USAGE:"
echo " $script_name [-H] [-E] [-i dir] [index...]"
echo " $script_name [-n name] [-H] [-E] [-i dir] [index...]"
echo " $script_name --cron [minutes]"
echo ""
echo " -H Don't update hidden index(es)."
echo " -n name Label the update run. Used as label when configuring crontab"
echo " when creating entries with option '--cron"
echo ""
echo " -E Don't update extension index(es)."
echo " -H Don't update hidden index(es)."
echo ""
echo " -i dir Incremental update the given dir in the affected index(es)."
echo " Indexes that does not contain the dir are not touched."
echo " -E Don't update extension index(es)."
echo ""
echo " index Name of the indexes that should be updated. The given names are"
echo " matched against the index names. So 'home' will match home.index"
echo " and 'home.hidden.index.ext'."
echo " To only update extension indexes use '*.ext'."
echo " -i dir Incremental update the given dir in the affected index(es)."
echo " Indexes that does not contain the dir are not touched."
echo ""
echo " index Name of the indexes that should be updated. The given names are"
echo " matched against the index names. So 'home' will match home.index"
echo " and 'home.hidden.index.ext'."
echo " To only update extension indexes use '*.ext'."
echo ""
echo " --cron [minutes]"
echo " Configure crontab to run $script_name."
echo " minutes:"
echo " not given: Lists current crontab config."
echo " 0: Removes $script_name from crontab."
echo " 1 - 30: Configures to run on the full hour and every multiple"
echo " of given minutes. E.g. for '12' it will run at "
echo " *:00, *:12, *:24, *:36 and *:48."
echo " Configure crontab to run $script_name."
echo " minutes:"
echo " not given: Lists current crontab config."
echo " 0: Removes $script_name from crontab."
echo " 1 - 30: Configures to run on the full hour and every multiple"
echo " of given minutes. E.g. for '12' it will run at "
echo " *:00, *:12, *:24, *:36 and *:48."
echo ""
}

Expand Down Expand Up @@ -476,10 +479,12 @@ configure_crontab()
echo "ERROR: Command 'crontab' not available"
return 1
fi
local cron_label="# Quick Change Directory: update index"

local job_name="default update"
local cron_label="# Quick Change Directory: $job_name"

if [ -z "${1:-}" ]; then
crontab -l 2>/dev/null | grep "$cron_label\|$script_name"
crontab -l 2>/dev/null | grep "^$cron_label$\|\($script_name -n \"$job_name\"\)"
return 0
fi
if [ -n "${1//[0-9]/}" ]; then
Expand All @@ -490,14 +495,14 @@ configure_crontab()
# shellcheck disable=SC2086 # minutes is a int number
if [ $minutes -eq 0 ] ; then
echo "Removing $script_name from crontab"
crontab -l 2>/dev/null | grep -v "$cron_label\|$script_name" | crontab -
crontab -l 2>/dev/null | grep -v "^$cron_label$\|\($script_name -n \"$job_name\"\)" | crontab -
return
elif [ $minutes -lt 1 ] || [ $minutes -gt 30 ]; then
echo >&2 "ERROR: Minutes value for '--cron' out of range 1-30"
return 1
fi

local exe="${script_file/#$HOME/\${HOME\}}"
local exe="${script_file/#$HOME/\${HOME\}} -n \"$job_name\""
local log_file="$QC_DIR_LOG/qc-build-index.log"
log_file="${log_file/#$HOME/\${HOME\}}"

Expand All @@ -507,7 +512,7 @@ configure_crontab()
echo "$cron_label"
echo "$cron_entry"

(crontab -l 2>/dev/null | grep -v "$cron_label" | grep -v "$script_name"; echo "$cron_label"; echo "$cron_entry") | crontab -
(crontab -l 2>/dev/null | grep -v "^$cron_label$\|\($script_name -n \"$job_name\"\)"; echo "$cron_label"; echo "$cron_entry") | crontab -
}

check_unknown_index_files()
Expand Down Expand Up @@ -582,14 +587,18 @@ trap "cleanup" EXIT

typeset -a inc_upd=()

if ! _args="$(getopt -n "$script_name" -o "i:HE" --long "help,version,cron" -- "$@")"; then
if ! _args="$(getopt -n "$script_name" -o "n:i:HE" --long "help,version,cron" -- "$@")"; then
echo >&2 "Try '$script_name --help' for more information."
exit 1
fi
eval "set -- $_args"

while true; do
case $1 in
-n)
shift
job_name="$1"
;;
-i)
shift
d=$1
Expand Down Expand Up @@ -649,6 +658,9 @@ if [ ! -e "$CFG_FILE" ]; then
copy_default_config
fi

if [ -n "${job_name:-}" ]; then
echo "Update: $job_name"
fi
if [[ -n "${QC_USE_FIND:-}" || -z "$FD_CMD" ]]; then
echo "Using find command"
QC_USE_FIND=true
Expand Down

0 comments on commit 38a425a

Please sign in to comment.