This is a script I wrote to automatically restart godoc whenever Go code is updated on my Macbooks. It is stored at ~/bin/godoc
and ~/bin/
is a part of my Linux path.
Last active
February 28, 2022 20:23
-
-
Save protosam/446719136cfcbfb224149ce8a8b29a74 to your computer and use it in GitHub Desktop.
This is a script I wrote to automatically restart godoc whenever Go code is updated on my Macbooks. It is stored at ~/bin/godoc and ~/bin/ is a part of my Linux path.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# starts a godoc server and restarts on content updates | |
# enable job control | |
set -m | |
# ensure this directory has go.mod | |
if [ ! -f "go.mod" ]; then | |
echo "go.mod not found" | |
exit 1 | |
fi | |
# kills all background jobs | |
function kill_background_jobs() { | |
joblist=$(jobs -l | awk '{if($3 == "Running") print "%"$1}' | tr -dc '0-9%\n') | |
if [ "${joblist}" != "" ]; then | |
kill -9 ${joblist} | |
fi | |
} | |
# ensure that kill_background_jobs is ran on exit | |
trap kill_background_jobs EXIT | |
# used for tracking file changes | |
FILESUM="" | |
# main event loop | |
while true; do | |
NEWSUM=$(find . -type f -exec shasum {} \; | shasum) | |
if [ "${FILESUM}" != "${NEWSUM}" ]; then | |
# kill any old background processes | |
kill_background_jobs | |
# run the documentation server | |
~/go/bin/godoc -play -http 127.0.0.1:6060 & | |
# first godoc start | |
if [ "${FILESUM}" == "" ]; then | |
# wait for documentation to become accessible | |
while ! curl -s http://127.0.0.1:6060 > /dev/null; do | |
sleep 1s | |
done | |
# open browser to documentation for current module | |
open http://127.0.0.1:6060/pkg/$(grep ^module go.mod | cut -d' ' -f2) | |
fi | |
# update the tracking sum | |
FILESUM="${NEWSUM}" | |
fi | |
sleep 1s | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment