In Travis CI, I want to check that my Go files are formatted properly via gofmt
and goimports
. During my CI builds, I only care about formatting issues in my own code, not in third-party repos.
Unfortunately, running a simple gofmt -l .
in the root of my project does not work because I'm using Godep, which checks in all of my external dependencies at ./Godep/_workspace
. While running go fmt ./...
ignores underscore-prefixed subdirectories, the plain gofmt .
does not. Neither gofmt
nor goimports
take the ./...
arg:
➜ goimports -l ./...
stat ./...: no such file or directory
Since I can use go list ./...
to get a list of all subpackages in my project (exluding vendored imports in an underscore-prefixed directory), I'm using the following to run gofmt
and goimports
on each of my own Go files (including _test.go
files):
$ OURGOFILES=($(go list -f '{{$p := .}}{{range $f := .GoFiles}}{{$p.Dir}}/{{$f}} {{end}} {{range $f := .TestGoFiles}}{{$p.Dir}}/{{$f}} {{end}}' ./... | xargs))
$ test -z "$(gofmt -l $OURGOFILES | tee /dev/stderr)"
$ test -z "$(goimports -l $OURGOFILES | tee /dev/stderr)"
As Keith commented below, the following is a much simpler version. An unrelated bug caused the wildcards to not work for me the first time I tried them, but they do work.
$ dirs=$(go list -f {{.Dir}} ./...)
$ test -z "`for d in $dirs; do goimports -l $d/*.go | tee /dev/stderr; done`"
Thanks!