Skip to content

Commit 59cbd39

Browse files
authored
Merge pull request #17 from devilbox/release-1.1.0
Add ignore/exclude option
2 parents 0ced4b9 + 6b0b6b0 commit 59cbd39

File tree

2 files changed

+69
-24
lines changed

2 files changed

+69
-24
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ watcherd -v \
4747

4848
### Usage
4949

50-
```shell
50+
```bash
5151
Usage: watcherd -p <path> -a <cmd> -d <cmd> [-t <cmd> -w <str> -i <int> -v -c]
5252
watcherd --help
5353
watcherd --version
@@ -73,6 +73,8 @@ Required arguments:
7373
Example: -d "script.sh -f %p -c %n -a %p"
7474

7575
Optional arguments:
76+
-e <regex> Exclude regex for directories to ignore.
77+
E.g.: -e '\.*' to ignore dot directories.
7678
-t <cmd> Command to execute after all directories have been added or
7779
deleted during one round.
7880
No argument will be appended.

bin/watcherd

+66-23
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ IFS=$'\n'
2525

2626
# Versioning
2727
MY_NAME="watcherd"
28-
MY_DATE="2022-12-18"
28+
MY_DATE="2023-03-01"
2929
MY_URL="https://github.com/devilbox/watcherd"
3030
MY_AUTHOR="cytopia <[email protected]>"
3131
MY_GPGKEY="0xA02C56F0"
32-
MY_VERSION="1.0.7"
32+
MY_VERSION="1.1.0"
3333
MY_LICENSE="MIT"
3434

3535
# Default settings
@@ -38,6 +38,7 @@ INTERVAL=1
3838
VERBOSE=0
3939
WATCHER="bash"
4040
WATCH_DIR=
41+
EXCLUDE=
4142
CMD_ADD=
4243
CMD_DEL=
4344
CMD_TRIGGER=
@@ -107,8 +108,10 @@ function print_help() {
107108
printf " %s\\n" "You can also append the following placeholders to your command string:"
108109
printf " %s\\n" "%p The full path of the directory that changed (added, deleted)."
109110
printf " %s\\n" "%n The name of the directory that changed (added, deleted)."
110-
printf " %s\\n" "Example: -b \"script.sh -f %p -c %n -a %p\""
111+
printf " %s\\n" "Example: -d \"script.sh -f %p -c %n -a %p\""
111112
printf "\\nOptional arguments:\\n"
113+
printf " -e <regex> %s\\n" "Exclude regex for directories to ignore."
114+
printf " %s\\n" "E.g.: -e '\\.*' to ignore dot directories."
112115
printf " -t <cmd> %s\\n" "Command to execute after all directories have been added or deleted during one round."
113116
printf " %s\\n" "No argument will be appended."
114117
printf " -w <str> %s\\n" "The directory watcher to use. Valid values are:"
@@ -141,15 +144,25 @@ function get_subdirs() {
141144
# | grep -Ev "^${path}/.+/" \
142145
# | sort
143146
path="${path%/}/"
144-
(ls -1 -a "${path}" || true) \
147+
# shellcheck disable=SC2012
148+
cd "${path}" && ls -1 -a . \
145149
| tr '\r\n' '\000' \
146150
| tr '\n' '\000' \
147151
| tr '\r' '\000' \
148152
| xargs \
149153
-0 \
150154
-P"$(getconf _NPROCESSORS_ONLN)" \
151155
-n1 \
152-
sh -c "if [ -d \"${path}\${1}\" ] && [ \"\${1}\" != \".\" ] && [ \"\${1}\" != \"..\" ]; then echo \"${path}\${1}\"; fi" -- \
156+
sh -c "
157+
if [ -d \"\${1}\" ] && [ \"\${1}\" != \".\" ] && [ \"\${1}\" != \"..\" ]; then
158+
if [ -n \"${EXCLUDE}\" ]; then
159+
if ! echo \"\${1}\" | grep -E '${EXCLUDE}' >/dev/null; then
160+
echo \"${path}\${1}\";
161+
fi
162+
else
163+
echo \"${path}\${1}\";
164+
fi;
165+
fi" -- \
153166
| sort
154167
}
155168

@@ -241,6 +254,14 @@ while [ $# -gt 0 ]; do
241254
fi
242255
CMD_DEL="${1}"
243256
;;
257+
-e)
258+
shift
259+
if [ -z "${1:-}" ]; then
260+
>&2 echo "${MY_NAME}: -e requires an argument."
261+
exit 1
262+
fi
263+
EXCLUDE="${1}"
264+
;;
244265
-t)
245266
shift
246267
if [ -z "${1:-}" ]; then
@@ -354,26 +375,48 @@ CHANGES=0
354375
# Use native inotify
355376
if [ "${WATCHER}" = "inotify" ]; then
356377
log "info" "Using native inotify to watch for changes."
357-
inotifywait \
358-
--quiet \
359-
--monitor \
360-
--event create \
361-
--event modify \
362-
--event delete \
363-
--event move \
364-
--format '%e/\\%w%f' \
365-
"${WATCH_DIR}" | while read -r output; do
366-
d="${output##*\\}"
367-
if [[ "${output}" =~ ^(CREATE|MOVED_TO),ISDIR/\\ ]]; then
368-
if action "${d}" "${CMD_ADD}" "ADD" "${VERBOSE}"; then
369-
trigger "${CMD_TRIGGER}" "1" "${VERBOSE}"
378+
if [ -n "${EXCLUDE}" ]; then
379+
inotifywait \
380+
--monitor \
381+
--exclude "${EXCLUDE}" \
382+
--event create \
383+
--event modify \
384+
--event delete \
385+
--event move \
386+
--format '%e/\\%w%f' \
387+
"${WATCH_DIR}" | while read -r output; do
388+
d="${output##*\\}"
389+
if [[ "${output}" =~ ^(CREATE|MOVED_TO),ISDIR/\\ ]]; then
390+
if action "${d}" "${CMD_ADD}" "ADD" "${VERBOSE}"; then
391+
trigger "${CMD_TRIGGER}" "1" "${VERBOSE}"
392+
fi
393+
elif [[ "${output}" =~ ^(DELETE|MOVED_FROM),ISDIR/\\ ]]; then
394+
if action "${d}" "${CMD_DEL}" "DEL" "${VERBOSE}"; then
395+
trigger "${CMD_TRIGGER}" "1" "${VERBOSE}"
396+
fi
370397
fi
371-
elif [[ "${output}" =~ ^(DELETE|MOVED_FROM),ISDIR/\\ ]]; then
372-
if action "${d}" "${CMD_DEL}" "DEL" "${VERBOSE}"; then
373-
trigger "${CMD_TRIGGER}" "1" "${VERBOSE}"
398+
done
399+
else
400+
inotifywait \
401+
--monitor \
402+
--event create \
403+
--event modify \
404+
--event delete \
405+
--event move \
406+
--format '%e/\\%w%f' \
407+
"${WATCH_DIR}" | while read -r output; do
408+
d="${output##*\\}"
409+
if [[ "${output}" =~ ^(CREATE|MOVED_TO),ISDIR/\\ ]]; then
410+
if action "${d}" "${CMD_ADD}" "ADD" "${VERBOSE}"; then
411+
trigger "${CMD_TRIGGER}" "1" "${VERBOSE}"
412+
fi
413+
elif [[ "${output}" =~ ^(DELETE|MOVED_FROM),ISDIR/\\ ]]; then
414+
if action "${d}" "${CMD_DEL}" "DEL" "${VERBOSE}"; then
415+
trigger "${CMD_TRIGGER}" "1" "${VERBOSE}"
416+
fi
374417
fi
375-
fi
376-
done
418+
done
419+
fi
377420
# Use custom inotify
378421
else
379422
log "info" "Using bash loop to watch for changes."

0 commit comments

Comments
 (0)