Skip to content

Instantly share code, notes, and snippets.

@kisom
Created August 14, 2015 23:00
Show Gist options
  • Save kisom/8e0dced9dab805d53ffa to your computer and use it in GitHub Desktop.
Save kisom/8e0dced9dab805d53ffa to your computer and use it in GitHub Desktop.
Auto fmt / vet / build / test Go code

This is my workflow for automatically building and testing Go code. It uses steeloverseer to watch for changes to the tree and run the standard Go tools (vet, build, test) on the package as well as golint on any changed files.

Install steeloverseer with

cabal install steeloverseer && cp ~/.cabal/bin/sos ~/bin

I keep the two shell scripts in my $HOME/bin directory; they both need to be chmod +x.

#!/bin/sh
# sos: steeloverser / http://hackage.haskell.org/package/steeloverseer
# cabal install steeloverseer && cp ~/bin
#
# Inspired by Levi Cook's `glitch` (https://github.com/levicook/glitch)
# the regex makes sure that the file isn't editor crap.
sos -c 'clear; $HOME/bin/go-builder.sh' -p '/[^.~#][a-zA-Z0-9_]+\.go$'
#!/bin/sh
gotool () {
echo "go $@ $PKG"
go $@ $PKG || exit 1
}
PKG="$1"
if [ -z "$PKG" ]
then
PKG="./..."
fi
gotool vet
gotool build
gotool test -cover
TOPLEVEL="$(git rev-parse --show-toplevel)"
CWD="$(pwd)"
cd $TOPLEVEL
MODIFIED="$(git ls-files --full-name -cm | grep '\.go' | xargs)"
if [ ! -z "$MODIFIED" ]
then
echo "golint $MODIFIED"
golint $MODIFIED
fi
cd $CWD
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment