joshuarubin / zb

an opinionated repo based tool for linting, testing and building go source
https://jrubin.io/zb
MIT License
110 stars 4 forks source link
build go golang lint test tools

zb — an opinionated repo based tool for linting, testing and building go source

GoDoc Go Report Card

███████╗██████╗     ██████╗  ██████╗ ███████╗███████╗    ██╗████████╗
╚══███╔╝██╔══██╗    ██╔══██╗██╔═══██╗██╔════╝██╔════╝    ██║╚══██╔══╝
  ███╔╝ ██████╔╝    ██║  ██║██║   ██║█████╗  ███████╗    ██║   ██║
 ███╔╝  ██╔══██╗    ██║  ██║██║   ██║██╔══╝  ╚════██║    ██║   ██║
███████╗██████╔╝    ██████╔╝╚██████╔╝███████╗███████║    ██║   ██║
╚══════╝╚═════╝     ╚═════╝  ╚═════╝ ╚══════╝╚══════╝    ╚═╝   ╚═╝

[Help! your logo here]

Benefits

Installation

Simply run go get jrubin.io/zb

Rationale

Many go repositories have multiple packages. It is often necessary to build/install, test, lint, etc. across all packages within the repository. Go "ellipsis" wildcards (...) can be used to select all subdirectories of a given repository, but don't exclude vendored packages which makes running tests and linting complicated. Some operations should be aware of vendored packages (e.g. build/install), while others should ignore them (e.g. lint). Yet others need to be aware of changes in vendored packages, but should not operate directly on them (e.g. test). Traditional build tools, like make, can be used to supplement the go command to work only on the intended packages. Building dependency lists for make targets, which are required, for example, to dynamically identify modified .go files and what would need to be rebuilt as a result, is at the very least complicated (consider .go files modified outside the repo) and at best slow.

zb fixes all of this

Packages → Repositories

zb is aware of the directory it is called from. If any of its commands is called without a package argument, it will use the current working directory.

zb can also be passed packages just like the go command. Both package names (e.g. fmt, jrubin.io/zb) and relative package names (e.g. ./jrubin.io/zb) are supported, as are ellipsis (...).

zb will identify the repository associated with each package by locating the directory and walking up the directory tree to find the repository directory (containing .git [only git is supported at present]). It will then execute the command for all packages in each repository it identified.

go generate

go generate is great, but sometimes it needs to be executed before a build. Forgetting to execute go generate can be a major problem if, for example, new values were added to a stringer.

zb does its own dependency calculation and can identify go generate dependencies provided an additional annotation is also present.

The following formats are available to define dependencies of go generate

zb:generate formats

Commands

install

Initially, zb install appears to do the same things as go install (just for all packages in the repositories). In fact, zb install just calls go install under the hood and supports all of its flags. There are a few differences though.

build

zb build differs from go build in that non main packages will be installed with go install. Commands, however, will not be installed (to $GOPATH/bin), they are built with the binary being placed in the root of the repository tree. If there is already a directory in the root of the repository with the same name as the command, the command will be placed in that directory instead.

Otherwise, zb build is identical to zb install.

lint

Delegates functionality to gometalinter but with more useful defaults and caching of results.

The -n flag can be used to hide golint warnings about missing comments.

Since dependency calculation can sometimes add a non-trivial amount of time to the zb lint command, go generate will not be executed.

Files matching certain suffixes will be excluded from the results. This list can be modified with the --ignore-suffix flag. By default files with the following suffixes will be excluded:

All other gometalinter flags will be honored as defined.

test

Delegates functionality to go test but caches the results (like gt). Honors all other flags just like go test except those intended to be passed directly to the test binary.

Use the -f flag to treat the test results as uncached, forcing the tests to be executed (and cached) again.

To see which tests would be executed (because their results are not-cached or the -f flag was provided), use the -l flag.

Since dependency calculation can sometimes add a non-trivial amount of time to the zb test command, go generate will not be executed.

complete

zb has full support for shell autocompletion in both bash and zsh. Simply execute eval "$(zb complete)" (or put in your init files) to enable.

clean

Removes the executables produced by zb build

commands

Lists the absolute paths where each of the commands (from main packages) will be placed with zb build

list

Similar to go list (and takes the same flags) but will list all of the packages in each of the repositories. Use the --vendor flag to exclude vendored packages.

help

zb contains a built-in, comprehensive help system. Running zb by itself (or with the -h or --help flags) will list the commands and global flags. zb help <command>, zb <command> -h and zb <command> --help will show contextual help for the given command.

Global Flags

--log-level, -l, $LOG_LEVEL

Defaults to info. Available levels are:

--no-warn-todo-fixme, -n, $NO_WARN_TODO_FIXME

Do not warn when finding WARN or FIXME in .go files

--cache, $CACHE

Modify the base directory used for storing results of commands that cache their results (test and lint). Defaults to $HOME/Library/Caches/zb on mac and $HOME/.cache/zb elsewhere.

--package, -p

Causes zb to execute only on the explicitly listed packages and not on all packages in their repositories.

Still Planned