Skip to content

Commit

Permalink
Merge pull request #17 from devilbox/release-1.1.0
Browse files Browse the repository at this point in the history
Add ignore/exclude option
  • Loading branch information
cytopia authored Jan 3, 2023
2 parents 0ced4b9 + 6b0b6b0 commit 59cbd39
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 24 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ watcherd -v \

### Usage

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

Optional arguments:
-e <regex> Exclude regex for directories to ignore.
E.g.: -e '\.*' to ignore dot directories.
-t <cmd> Command to execute after all directories have been added or
deleted during one round.
No argument will be appended.
Expand Down
89 changes: 66 additions & 23 deletions bin/watcherd
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ IFS=$'\n'

# Versioning
MY_NAME="watcherd"
MY_DATE="2022-12-18"
MY_DATE="2023-03-01"
MY_URL="https://github.com/devilbox/watcherd"
MY_AUTHOR="cytopia <[email protected]>"
MY_GPGKEY="0xA02C56F0"
MY_VERSION="1.0.7"
MY_VERSION="1.1.0"
MY_LICENSE="MIT"

# Default settings
Expand All @@ -38,6 +38,7 @@ INTERVAL=1
VERBOSE=0
WATCHER="bash"
WATCH_DIR=
EXCLUDE=
CMD_ADD=
CMD_DEL=
CMD_TRIGGER=
Expand Down Expand Up @@ -107,8 +108,10 @@ function print_help() {
printf " %s\\n" "You can also append the following placeholders to your command string:"
printf " %s\\n" "%p The full path of the directory that changed (added, deleted)."
printf " %s\\n" "%n The name of the directory that changed (added, deleted)."
printf " %s\\n" "Example: -b \"script.sh -f %p -c %n -a %p\""
printf " %s\\n" "Example: -d \"script.sh -f %p -c %n -a %p\""
printf "\\nOptional arguments:\\n"
printf " -e <regex> %s\\n" "Exclude regex for directories to ignore."
printf " %s\\n" "E.g.: -e '\\.*' to ignore dot directories."
printf " -t <cmd> %s\\n" "Command to execute after all directories have been added or deleted during one round."
printf " %s\\n" "No argument will be appended."
printf " -w <str> %s\\n" "The directory watcher to use. Valid values are:"
Expand Down Expand Up @@ -141,15 +144,25 @@ function get_subdirs() {
# | grep -Ev "^${path}/.+/" \
# | sort
path="${path%/}/"
(ls -1 -a "${path}" || true) \
# shellcheck disable=SC2012
cd "${path}" && ls -1 -a . \
| tr '\r\n' '\000' \
| tr '\n' '\000' \
| tr '\r' '\000' \
| xargs \
-0 \
-P"$(getconf _NPROCESSORS_ONLN)" \
-n1 \
sh -c "if [ -d \"${path}\${1}\" ] && [ \"\${1}\" != \".\" ] && [ \"\${1}\" != \"..\" ]; then echo \"${path}\${1}\"; fi" -- \
sh -c "
if [ -d \"\${1}\" ] && [ \"\${1}\" != \".\" ] && [ \"\${1}\" != \"..\" ]; then
if [ -n \"${EXCLUDE}\" ]; then
if ! echo \"\${1}\" | grep -E '${EXCLUDE}' >/dev/null; then
echo \"${path}\${1}\";
fi
else
echo \"${path}\${1}\";
fi;
fi" -- \
| sort
}

Expand Down Expand Up @@ -241,6 +254,14 @@ while [ $# -gt 0 ]; do
fi
CMD_DEL="${1}"
;;
-e)
shift
if [ -z "${1:-}" ]; then
>&2 echo "${MY_NAME}: -e requires an argument."
exit 1
fi
EXCLUDE="${1}"
;;
-t)
shift
if [ -z "${1:-}" ]; then
Expand Down Expand Up @@ -354,26 +375,48 @@ CHANGES=0
# Use native inotify
if [ "${WATCHER}" = "inotify" ]; then
log "info" "Using native inotify to watch for changes."
inotifywait \
--quiet \
--monitor \
--event create \
--event modify \
--event delete \
--event move \
--format '%e/\\%w%f' \
"${WATCH_DIR}" | while read -r output; do
d="${output##*\\}"
if [[ "${output}" =~ ^(CREATE|MOVED_TO),ISDIR/\\ ]]; then
if action "${d}" "${CMD_ADD}" "ADD" "${VERBOSE}"; then
trigger "${CMD_TRIGGER}" "1" "${VERBOSE}"
if [ -n "${EXCLUDE}" ]; then
inotifywait \
--monitor \
--exclude "${EXCLUDE}" \
--event create \
--event modify \
--event delete \
--event move \
--format '%e/\\%w%f' \
"${WATCH_DIR}" | while read -r output; do
d="${output##*\\}"
if [[ "${output}" =~ ^(CREATE|MOVED_TO),ISDIR/\\ ]]; then
if action "${d}" "${CMD_ADD}" "ADD" "${VERBOSE}"; then
trigger "${CMD_TRIGGER}" "1" "${VERBOSE}"
fi
elif [[ "${output}" =~ ^(DELETE|MOVED_FROM),ISDIR/\\ ]]; then
if action "${d}" "${CMD_DEL}" "DEL" "${VERBOSE}"; then
trigger "${CMD_TRIGGER}" "1" "${VERBOSE}"
fi
fi
elif [[ "${output}" =~ ^(DELETE|MOVED_FROM),ISDIR/\\ ]]; then
if action "${d}" "${CMD_DEL}" "DEL" "${VERBOSE}"; then
trigger "${CMD_TRIGGER}" "1" "${VERBOSE}"
done
else
inotifywait \
--monitor \
--event create \
--event modify \
--event delete \
--event move \
--format '%e/\\%w%f' \
"${WATCH_DIR}" | while read -r output; do
d="${output##*\\}"
if [[ "${output}" =~ ^(CREATE|MOVED_TO),ISDIR/\\ ]]; then
if action "${d}" "${CMD_ADD}" "ADD" "${VERBOSE}"; then
trigger "${CMD_TRIGGER}" "1" "${VERBOSE}"
fi
elif [[ "${output}" =~ ^(DELETE|MOVED_FROM),ISDIR/\\ ]]; then
if action "${d}" "${CMD_DEL}" "DEL" "${VERBOSE}"; then
trigger "${CMD_TRIGGER}" "1" "${VERBOSE}"
fi
fi
fi
done
done
fi
# Use custom inotify
else
log "info" "Using bash loop to watch for changes."
Expand Down

0 comments on commit 59cbd39

Please sign in to comment.