make

package module
Version: v2.1.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 28, 2017 License: BSD-3-Clause Imports: 0 Imported by: 0

README

Makefile snippets (aka "makelets" 😄) that I use for building golang projects. Everyone likes their own, I like these :bowtie: Just wanted a public place to put them so I don't end up with scattered copies everywhere .. so this repo is kind of intended just for my benefit, but maybe you'll find a use too. If you'd like something else included, then fork away and send a PR if you like.

For full batteries-included simplicity, use in a makefile as follows (make sure you replace the spaces with a tab).

(See gopkg.in/make.v2 for a list of versions)

-include $(GOPATH)/src/gopkg.in/make.v2/batteries.mk
$(GOPATH)/src/gopkg.in/make.v2/batteries.mk:
	go get gopkg.in/make.v2

Included in this repo is complete working Makefile, just drop that file in the root of your go project.

Take a look in that file to see the make rules included. If you want more control over things then you can instead include just the tool definitions:

-include $(GOPATH)/src/gopkg.in/make.v2/gotools.mk
$(GOPATH)/src/gopkg.in/make.v2/gotools.mk:
	go get gopkg.in/make.v2

Features

Make targets

It's definitely recommended that you use autocomplete for make.

These targets are supported by batteries.mk:

Target(s) Description
install Does a go install
vendor Uses glide to install versioned packages to your vendor directory
lint-fast, lint, lint-full Uses gometalinter to scan for possible nasties. The different targets just enable different options, either --fast, default, or --enable-all
test-short, test invokes go test on all local packages (it skips the vendored ones). test-short will pass the --short option in case you have some tests that take a long time to run (e.g. testing timeouts etc)
coverage-short, coverage uses gocov to invoke go test on all your local packages, generating code coverage info. Also builds an HTML file that shows which source lines are not covered by your tests
clean it's a double-colon make rule so that it can be extended by your own makefile
clobber gets things really really clean, it will invoke the clean target first

and these are defined elsewhere, included above:

Target(s) Description
gopath Allows you to run eval $(make gopath) from your bash shell, sets up $GOPATH in your environment and adds $GOPATH/bin to your $PATH

Macros etc

Variable Description
$(call PROMPT, ...) Use in a make rule, just prints a little banner heading with some text
STRIP_DEBUG Define options to pass to go build via -ldflags to strip debug for smaller binaries

Supported Tools

Most of the tool support is quite simple, in many cases not much more than something to install the tool and a make variable to refer to the binary. Others are more fully featured. Brief info follows below.

glide

There are a plethora of package managers / vendoring tools for go at the moment. I currently use glide.

Defined variables:

Variable Description
$(GLIDE) Refers to the glide executable. You can depend on this to get glide installed
$(GLIDE_OPT_INSTALL) options to be passed to glide install (used to install package dependencies)

gometalinter

Superb wrapper to simplify running a whole bunch of linters on your code. See gometalinter

This makelet defines a few settings which you can override before you include gomake.

Defined variables:

Variable Description
$(GOMETALINTER) Refers to the gometalinter executable. You can depend on this to get gometalinter installed
$(GOMETALINTER_OPT) Sets up some default options for gometalinter (including the settings just below)
$(GOMETALINTER_LINELENGTH) Sets the text line length used during linting
$(GOMETALINTER_DEADLINE) Sets the a linting time deadline (some linters can take quite a while...)
$(GOMETALINTER_CYCLO) Sets the cyclomatic complexity used during linting

goconvey

The goconvey makelet needs no other integration in your main makefile. Just run as follows (from your repo):

$ make goconvey

And it should install itself and fire up a webpage on http://127.0.0.1:8080. All your go test tests will be run on any code change, and you can get browser notifications when a test fails.

gocov

gocov provides a nice wrapper around go test by simplifying the process of getting code coverage on a per-package basis. The gocov-html generates a nice HTML file where you can easily see which source lines are not tested.

rice

Useful to embed directories of static files into binaries. See go.rice

Variable Description
$(RICE) Refers to the rice executable. You can depend on this to get it installed

go-bindata

Sometimes, or for gopherjs, you may want to include assets as part of the compilation process instead of using rice (above). In those cases, go-bindata can be very useful.

Variable Description
$(GO_BINDATA) Refers to the go-bindata executable. You can depend on this to get it installed

gopherjs

golang in the browser #FTW. See gopherjs

Variable Description
$(GOPHERJS) Refers to the gopherjs executable. You can depend on this to get gopherjs installed
$(TEMPLE) Refers to the temple executable (used to enable go-templates for client-side markup generation). You can depend on this to get temple installed

gravitational/version

Allows you to print the git tag/hash of the repo when your app runs. See github.com/gravitational/version or the godocs for more info.

Use this define something like:

$(BIN): | $(CMD_LINKFLAGS)
    $(GO) build -ldflags='$(call VERSION_LDFLAGS_VENDOR,github.com/example/myrepo)'

NB, this assumes you've vendored this package. If not, then omit the _VENDOR in the line above.

docker

docker.mk is not included with the batteries, but provides some functions for working with docker containers:

Target(s) Description
image Builds a docker image, tagged with $(IMAGE_NAME)
docker-push Pushes to a docker registry
ca-bundle.crt Downloads a trusted list of certificates from mkcert.org. Useful for a SCRATCH container that needs to contact HTTPS APIs etc. Use in Dockerfile as ADD ca-bundle.crt /etc/pki/tls/certs/ca-bundle.crt
docker-clean Cleans the myriad untagged docker images that build up during development
docker-tar Exports container image to a tar file - possibly useful for backing up
clobber:: Removes previously-built images

Optionally-defined variables (only added if not already defined):

Variable Description Default Value
$(DOCKERFILE) Used to build the image Dockerfile
$(IMAGE_VERSION) Used to tag the image (git tag)
$(CONTAINER_NAME) Used to tag the image (directory name, with hypens removed)
$(DOCKER_SHELL) Used for docker-shell and docker-entry above. /bin/bash

Defined variables:

Variable Description Default Value
$(IMAGE_NAME) Used to tag the image [$(DOCKER_REGISTRY)/][$(DOCKER_ORGANISATION)/]$(CONTAINER_NAME):$(IMAGE_VERSION)
Extras

Some additional rules that can be useful to run a container with some default options:

Target(s) Description
docker-run Run container with $(EXPOSE_PORTS), $(BIND_VOLUMES) and $(EXTRA_RUN_OPTS)
docker-shell Does docker exec with $(DOCKER_SHELL) in the container
docker-entry Runs a container but with entrypoint set to $(DOCKER_SHELL), useful for debugging entry scripts

Optionally-defined variables (only added if not already defined):

Variable Description Default Value
$(EXPOSE_PORTS) Option passed to docker-run and docker-entry (maps all EXPOSE ports defined in the Dockerfile)

Documentation

Overview

A no-op package that only serves to enable you to 'go get' this repo. See https://github.com/go-make/make for more info.

Source Files

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL