Skip to content

Instantly share code, notes, and snippets.

@kegsay
Last active October 17, 2022 09:25
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save kegsay/84ce060f237cb9ab4e0d2d321a91d920 to your computer and use it in GitHub Desktop.
Save kegsay/84ce060f237cb9ab4e0d2d321a91d920 to your computer and use it in GitHub Desktop.
Generate static docs for a Go package
#!/bin/bash
set -u
DOC_DIR=godoc
PKG=github.com/matrix-org/go-neb
# Run a godoc server which we will scrape. Clobber the GOPATH to include
# only our dependencies.
GOPATH=$(pwd):$(pwd)/vendor godoc -http=localhost:6060 &
DOC_PID=$!
# Wait for the server to init
while :
do
curl -s "http://localhost:6060" > /dev/null
if [ $? -eq 0 ] # exit code is 0 if we connected
then
break
fi
done
# Scrape the pkg directory for the API docs. Scrap lib for the CSS/JS. Ignore everything else.
# The output is dumped to the directory "localhost:6060".
wget -r -m -k -E -p -erobots=off --include-directories="/pkg,/lib" --exclude-directories="*" "http://localhost:6060/pkg/$PKG/"
# Stop the godoc server
kill -9 $DOC_PID
# Delete the old directory or else mv will put the localhost dir into
# the DOC_DIR if it already exists.
rm -rf $DOC_DIR
mv localhost\:6060 $DOC_DIR
echo "Docs can be found in $DOC_DIR"
echo "Replace /lib and /pkg in the gh-pages branch to update gh-pages"
@derFunk
Copy link

derFunk commented Sep 20, 2018

Since generating the docs takes some time, you'd have to curl for the full path in line #15: `curl -s "http://localhost:6060/pkg/$PKG/"

@derFunk
Copy link

derFunk commented Sep 21, 2018

Also you should add the fail flag to curl, to prevent continuing on a 404 page. This happens while godoc is still generating the doc pages. (Line #15 -> curl -f -s "http://localhost:6060" > /dev/null

@silbermm
Copy link

Is this still the best way to generate static docs for my GO Module?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment