« Back to Index

Go: manage external tools via go modules

View original Gist on GitHub

Tags: #go #dependencies

Go manage external tools via go modules.md

[!IMPORTANT] Read https://gist.github.com/Integralist/e19c9faee86e797125e6d95fe1188912 instead.

Go 1.16

As of Go version 1.16 the go command changed behaviour for get and install:

The go install command now accepts a version suffix:

go install example.com/cmd@v1.0.0

Pre Go 1.16

Before Go version 1.16 we needed to resort to other ways of getting a resource.

Have the following package tools/tools.go:

// +build tools

package tools

import (
	// document generation
	_ "github.com/hashicorp/terraform-plugin-docs/cmd/tfplugindocs"
)

Note: the use of a build tag means go build won’t compile the code into your binary.

Have the following Makefile:

BIN=$(CURDIR)/bin
$(BIN)/%:
	@echo "Installing tools from tools/tools.go"
	@cat tools/tools.go | grep _ | awk -F '"' '{print $$2}' | GOBIN=$(BIN) xargs -tI {} go install {}

# Inject ./bin into PATH to allow scripts/generate-docs.go to access local tfplugindocs binary
generate-docs: $(BIN)/tfplugindocs
	PATH=$(PATH):$(BIN) go run scripts/generate-docs.go

validate-docs: $(BIN)/tfplugindocs
	$(BIN)/tfplugindocs validate

Note: pay special attention to the fact that $ is a reserved symbol in a Makefile so we have to escape it: $$.

Now you’ll find you have the dependency installed:

ls -la $GOPATH/bin

-rwxr-xr-x   1 integralist  staff  22965276  2 Feb 13:52 tfplugindocs*