Skip to content

Instantly share code, notes, and snippets.

@dvliman
Created November 21, 2013 05:34
Show Gist options
  • Save dvliman/7576561 to your computer and use it in GitHub Desktop.
Save dvliman/7576561 to your computer and use it in GitHub Desktop.
go-reload
#!/bin/bash
# Watch all *.go files in the specified directory
# Call the restart function when they are saved
function monitor() {
inotifywait -q -m -r -e close_write --exclude '[^g][^o]$' $1 |
while read line; do
restart
done
}
# Terminate and rerun the main Go program
function restart {
if [ "$(pidof $PROCESS_NAME)" ]; then
killall -q -w -9 $PROCESS_NAME
fi
echo ">> Reloading..."
go run $FILE_PATH $ARGS &
}
# Make sure all background processes get terminated
function close {
killall -q -w -9 inotifywait
exit 0
}
trap close INT
echo "== Go-reload"
echo ">> Watching directories, CTRL+C to stop"
FILE_PATH=$1
FILE_NAME=$(basename $FILE_PATH)
PROCESS_NAME=${FILE_NAME%%.*}
shift
ARGS=$@
# Start the main Go program
go run $FILE_PATH $ARGS &
# Monitor the /src directories in all directories on the GOPATH
OIFS="$IFS"
IFS=':'
for path in $GOPATH
do
monitor $path/src &
done
IFS="$OIFS"
# Monitor the current directory
monitor .
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment