Skip to content

Instantly share code, notes, and snippets.

@kfish
Last active August 29, 2015 14:21
Show Gist options
  • Save kfish/f539ceebd1c1fd9d8623 to your computer and use it in GitHub Desktop.
Save kfish/f539ceebd1c1fd9d8623 to your computer and use it in GitHub Desktop.
ci: Continuously invoke a command when input files change
#!/bin/sh
#
# ci: Continuously invoke a command when input files change
#
# Requires inotifywait, from inotify-tools:
# # apt-get install inotify-tools
# # yum install inotify-tools
#
# Conrad Parker 2015
THIS="ci"
############################################################
## help
############################################################
ci_help() {
echo >&2 "ci: Continuously invoke a command when input files change"
echo >&2
echo >&2 "Usage: ci [command] ..."
echo >&2
echo >&2 "Options:"
echo >&2 " -l CMD, --list CMD Command that lists files to watch"
echo >&2
echo >&2 "Miscellaneous options"
echo >&2 " -n, --dry-run Don't actually run commands, print"
echo >&2 " -h, --help Display this help"
echo >&2 " -v, --verbose Print informative messages"
echo >&2
exit 1
}
############################################################
## General functions
############################################################
ci_verbose_echo () {
if test "x$CI_QUIET" = "x"; then
echo $THIS: $*
fi
}
#
# ci_try_run desc cmd
#
ci_try_run () {
desc=$1
shift
ci_verbose_echo ==========================================================================
ci_verbose_echo $desc ...
if test "x$CI_DRY_RUN" != "x" || test "x$CI_QUIET" = "x"; then
echo $*
fi
if test "x$CI_DRY_RUN" = "x" ; then
eval $*
if test "$?" != "0"; then
echo $*
#exit 1
fi
fi
}
############################################################
## Cleanup
############################################################
# exit status
stat=1
cleanup () {
exit $stat
}
trap cleanup 0
############################################################
## Continuously invoke
############################################################
continuously_invoke () {
while true; do
eval $CI_LIST | inotifywait $CI_QUIET -e modify --fromfile -
ci_try_run "$CI_COMMAND" $CI_COMMAND
done
}
############################################################
## Main
############################################################
CI_DRY_RUN=""
CI_QUIET="-q"
CI_LIST=""
CI_COMMAND=""
GETOPTEST=`getopt --version`
SHORTOPTS="nhvl:"
case $GETOPTEST in
getopt*) # GNU getopt
TEMP=`getopt -l dry-run -l help -l verbose -l "list:" -- +$SHORTOPTS "$@"`
;;
*) # POSIX getopt ?
TEMP=`getopt $SHORTOPTS "$@"`
;;
esac
if test "$?" != "0"; then
ci_help
fi
eval set -- "$TEMP"
while test "x$1" != "x--" ; do
case "$1" in
-n | --dry-run)
CI_DRY_RUN="y"
CI_QUIET=""
;;
-v | verbose)
CI_QUIET=""
;;
-h | --help)
ci_help
;;
-l | --list)
CI_LIST="$2"
shift
;;
esac
shift
done
# Check that all options parsed ok
if test "x$1" != "x--"; then
ci_help
return
fi
shift # Get rid of the '--'
CI_COMMAND=$*
continuously_invoke
# Set exit status to 0
stat=0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment