Skip to content

Commit

Permalink
Merge pull request #8 from devilbox/release-1.0.0
Browse files Browse the repository at this point in the history
Release 1.0.0
  • Loading branch information
cytopia authored Nov 21, 2020
2 parents 015ebbe + 7832ae2 commit 56b21a3
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 72 deletions.
31 changes: 31 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# top-most EditorConfig file
root = true

# Default for all files
[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

# Custom files
[*.py]
indent_style = space
indent_size = 4

[.sh}]
indent_style = tab
indent_size = 4

[Makefile]
indent_style = tab
indent_size = 4

[*.{yml,yaml}]
indent_style = space
indent_size = 2

[*.md]
indent_style = space
trim_trailing_whitespace = false
indent_size = 2
34 changes: 34 additions & 0 deletions .github/workflows/linting.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---

###
### Lints all generic and json files in the whole git repository
###

name: linting
on:
pull_request:
push:
branches:
- master
tags:

jobs:
lint:
runs-on: ubuntu-latest
strategy:
fail-fast: False
matrix:
target:
- Linting
name: "[ ${{ matrix.target }} ]"
steps:
- name: Checkout repository
uses: actions/checkout@master

- name: Lint files
run: |
make _lint-files
- name: Shellcheck
run: |
make _lint-shell
25 changes: 0 additions & 25 deletions .travis.yml

This file was deleted.

45 changes: 45 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
ifneq (,)
.error This Makefile requires GNU Make.
endif

# -------------------------------------------------------------------------------------------------
# Default configuration
# -------------------------------------------------------------------------------------------------
.PHONY: help lint

FL_VERSION = 0.3
FL_IGNORES = .git/,.github/


# -------------------------------------------------------------------------------------------------
# Default Target
# -------------------------------------------------------------------------------------------------
help:
@echo "lint Lint repository"


# -------------------------------------------------------------------------------------------------
# Lint Targets
# -------------------------------------------------------------------------------------------------
lint: _lint-files
lint: _lint-shell


.PHONY: _lint-files
_lint-files:
@echo "# -------------------------------------------------------------------- #"
@echo "# Lint files"
@echo "# -------------------------------------------------------------------- #"
@docker run --rm $$(tty -s && echo "-it" || echo) -v $(PWD):/data cytopia/file-lint:$(FL_VERSION) file-cr --text --ignore '$(FL_IGNORES)' --path .
@docker run --rm $$(tty -s && echo "-it" || echo) -v $(PWD):/data cytopia/file-lint:$(FL_VERSION) file-crlf --text --ignore '$(FL_IGNORES)' --path .
@docker run --rm $$(tty -s && echo "-it" || echo) -v $(PWD):/data cytopia/file-lint:$(FL_VERSION) file-trailing-single-newline --text --ignore '$(FL_IGNORES)' --path .
@docker run --rm $$(tty -s && echo "-it" || echo) -v $(PWD):/data cytopia/file-lint:$(FL_VERSION) file-trailing-space --text --ignore '$(FL_IGNORES)' --path .
@docker run --rm $$(tty -s && echo "-it" || echo) -v $(PWD):/data cytopia/file-lint:$(FL_VERSION) file-utf8 --text --ignore '$(FL_IGNORES)' --path .
@docker run --rm $$(tty -s && echo "-it" || echo) -v $(PWD):/data cytopia/file-lint:$(FL_VERSION) file-utf8-bom --text --ignore '$(FL_IGNORES)' --path .

.PHONY: _lint-shell
_lint-shell:
@echo "# -------------------------------------------------------------------- #"
@echo "# Shellcheck"
@echo "# -------------------------------------------------------------------- #"
@docker run --rm $$(tty -s && echo "-it" || echo) -v $(PWD):/mnt -w /mnt koalaman/shellcheck:stable watcherd
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# watcherd

[![Build Status](https://travis-ci.org/devilbox/watcherd.svg?branch=master)](https://travis-ci.org/devilbox/watcherd)
[![linting](https://github.com/devilbox/watcherd/workflows/linting/badge.svg)](https://github.com/devilbox/watcherd/actions?query=workflow%3Alinting)

**[watcherd](https://github.com/devilbox/watcherp/blob/master/watcherd)** will look for directory changes (added and deleted directories) under the specified path (`-p`) and will execute specified commands or shell scripts (`-a`, `-d`) depending on the event.
Once all events have happened during one round (`-i`), a trigger command can be executed (`-t`).
Expand Down
97 changes: 51 additions & 46 deletions watcherd
Original file line number Diff line number Diff line change
Expand Up @@ -53,53 +53,58 @@ WITHOUT_SUBSHELL=1
############################################################

function print_help() {
printf "Usage: %s %s\n" "${MY_NAME}" "-p <path> -a <cmd> -d <cmd> [-t <cmd> -w <str> -i <int> -v]"
printf " %s %s\n" "${MY_NAME}" "--help"
printf " %s %s\n" "${MY_NAME}" "--version"
printf "\n"
printf "%s\n" "${MY_NAME} will look for directory changes (added and deleted directories) under the specified"
printf "path (-p) and will execute specified commands or shell scripts (-a, -d) depending on the event.\n"
printf "Once all events have happened during one round (-i), a trigger command can be executed (-t).\n"
printf "Usage: %s %s\\n" "${MY_NAME}" "-p <path> -a <cmd> -d <cmd> [-t <cmd> -w <str> -i <int> -v]"
printf " %s %s\\n" "${MY_NAME}" "--help"
printf " %s %s\\n" "${MY_NAME}" "--version"
printf "\\n"
printf "%s\\n" "${MY_NAME} will look for directory changes (added and deleted directories) under the specified"
printf "path (-p) and will execute specified commands or shell scripts (-a, -d) depending on the event.\\n"
printf "Once all events have happened during one round (-i), a trigger command can be executed (-t).\\n"
printf "Note, the trigger command will only be execute when at least one add or delete command has succeeded with exit code 0."
printf "\n"
printf "\nRequired arguments:\n"
printf " -p <path> %s\n" "Path to directoy to watch for changes."
printf " -a <cmd> %s\n" "Command to execute when a directory was added."
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: -a \"script.sh -f %p -c %n -a %p\""
printf " -d <cmd> %s\n" "Command to execute when a directory was deletd."
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 "\nOptional arguments:\n"
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:"
printf " %s\n" "'inotify': Uses inotifywait to watch for directory changes."
printf " %s\n" "'bash': Uses a bash loop to watch for directory changes."
printf " %s\n" "The default is to use 'bash' as the watcher."
printf " -i <int> %s\n" "When using the bash watcher, specify the interval in seconds for how often"
printf " %s\n" "to look for directory changes."
printf " -v %s\n" "Verbose output."
printf "\nMisc arguments:\n"
printf " --help %s\n" "Show this help screen."
printf " --version %s\n" "Show version information."
printf "\\n"
printf "\\nRequired arguments:\\n"
printf " -p <path> %s\\n" "Path to directoy to watch for changes."
printf " -a <cmd> %s\\n" "Command to execute when a directory was added."
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: -a \"script.sh -f %p -c %n -a %p\""
printf " -d <cmd> %s\\n" "Command to execute when a directory was deletd."
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 "\\nOptional arguments:\\n"
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:"
printf " %s\\n" "'inotify': Uses inotifywait to watch for directory changes."
printf " %s\\n" "'bash': Uses a bash loop to watch for directory changes."
printf " %s\\n" "The default is to use 'bash' as the watcher."
printf " -i <int> %s\\n" "When using the bash watcher, specify the interval in seconds for how often"
printf " %s\\n" "to look for directory changes."
printf " -v %s\\n" "Verbose output."
printf "\\nMisc arguments:\\n"
printf " --help %s\\n" "Show this help screen."
printf " --version %s\\n" "Show version information."
}

function print_version() {
printf "Name: %s\n" "${MY_NAME}"
printf "Version: %s (%s)\n" "${MY_VERSION}" "${MY_DATE}"
printf "Author: %s (%s)\n" "${MY_AUTHOR}" "${MY_GPGKEY}"
printf "License: %s\n" "${MY_LICENSE}"
printf "URL: %s\n" "${MY_URL}"
printf "Name: %s\\n" "${MY_NAME}"
printf "Version: %s (%s)\\n" "${MY_VERSION}" "${MY_DATE}"
printf "Author: %s (%s)\\n" "${MY_AUTHOR}" "${MY_GPGKEY}"
printf "License: %s\\n" "${MY_LICENSE}"
printf "URL: %s\\n" "${MY_URL}"
}

function get_subdirs() {
local path="${1}"
find "${path}" -type d \! -name "$( basename "${path}" )" -prune | sort
# shellcheck disable=SC2016
find "${path}" \
| grep -Ev "^${path}\$" \
| grep -Ev "^${path}/.+/" \
| xargs -n1 sh -c 'if [ -d "${1}" ]; then echo "${1}"; fi' -- \
| sort
}

function action() {
Expand All @@ -116,12 +121,12 @@ function action() {

if eval "${action}"; then
if [ "${verbose}" -gt "0" ]; then
printf "%s: [%s] [OK] %s succeeded: %s\n" "${MY_NAME}" "$( date '+%Y-%m-%d %H:%M:%S' )" "${info}" "${directory}"
printf "%s: [%s] [OK] %s succeeded: %s\\n" "${MY_NAME}" "$( date '+%Y-%m-%d %H:%M:%S' )" "${info}" "${directory}"
fi
return 0
else
if [ "${verbose}" -gt "0" ]; then
printf "%s: [%s] [ERR] %s failed: %s\n" "${MY_NAME}" "$( date '+%Y-%m-%d %H:%M:%S' )" "${info}" "${directory}"
printf "%s: [%s] [ERR] %s failed: %s\\n" "${MY_NAME}" "$( date '+%Y-%m-%d %H:%M:%S' )" "${info}" "${directory}"
fi
return 1
fi
Expand All @@ -137,12 +142,12 @@ function trigger() {
if [ "${changes}" -eq "1" ]; then
if eval "${action}"; then
if [ "${verbose}" -gt "0" ]; then
printf "%s: [%s] [OK] %s succeeded: %s\n" "${MY_NAME}" "$( date '+%Y-%m-%d %H:%M:%S' )" "TRIGGER" "${action}"
printf "%s: [%s] [OK] %s succeeded: %s\\n" "${MY_NAME}" "$( date '+%Y-%m-%d %H:%M:%S' )" "TRIGGER" "${action}"
fi
return 0
else
if [ "${verbose}" -gt "0" ]; then
printf "%s: [%s] [ERR] %s failed: %s\n" "${MY_NAME}" "$( date '+%Y-%m-%d %H:%M:%S' )" "TRIGGER" "${action}"
printf "%s: [%s] [ERR] %s failed: %s\\n" "${MY_NAME}" "$( date '+%Y-%m-%d %H:%M:%S' )" "TRIGGER" "${action}"
fi
# Also return 0 here in order to not abort the loop
return 0
Expand Down Expand Up @@ -255,7 +260,7 @@ fi

# Log startup
if [ "${VERBOSE}" -gt "0" ]; then
printf "%s: [%s] Starting daemon.\n" "${MY_NAME}" "$( date '+%Y-%m-%d %H:%M:%S' )"
printf "%s: [%s] Starting daemon.\\n" "${MY_NAME}" "$( date '+%Y-%m-%d %H:%M:%S' )"
fi


Expand Down Expand Up @@ -285,7 +290,7 @@ CHANGES=0
# Use native inotify
if [ "${WATCHER}" = "inotify" ]; then
if [ "${VERBOSE}" -gt "0" ]; then
printf "%s: [%s] Using native inotify to watch for changes.\n" "${MY_NAME}" "$( date '+%Y-%m-%d %H:%M:%S' )"
printf "%s: [%s] Using native inotify to watch for changes.\\n" "${MY_NAME}" "$( date '+%Y-%m-%d %H:%M:%S' )"
fi
inotifywait \
--quiet \
Expand All @@ -310,7 +315,7 @@ if [ "${WATCHER}" = "inotify" ]; then
# Use custom inotify
else
if [ "${VERBOSE}" -gt "0" ]; then
printf "%s: [%s] Using bash loop to watch for changes.\n" "${MY_NAME}" "$( date '+%Y-%m-%d %H:%M:%S' )"
printf "%s: [%s] Using bash loop to watch for changes.\\n" "${MY_NAME}" "$( date '+%Y-%m-%d %H:%M:%S' )"
fi
while true; do
# Get all directories
Expand Down

0 comments on commit 56b21a3

Please sign in to comment.