Skip to content

Instantly share code, notes, and snippets.

@Integralist
Last active February 22, 2021 14:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Integralist/6aacefc40dbde3d39a17c4813721f063 to your computer and use it in GitHub Desktop.
Save Integralist/6aacefc40dbde3d39a17c4813721f063 to your computer and use it in GitHub Desktop.
[Go manage external tools via go modules] #go #golang #modules #dependencies #tools #external #ci

Go 1.16

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

  • install: the recommended way to install packages (ignoring
  • get: adds dependencies to your project's go modules file (i.e. go.mod).

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*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment