2017-07-18 19:19:40 +00:00
# This is the default Clever Golang Makefile.
# It is stored in the dev-handbook repo, github.com/Clever/dev-handbook
# Please do not alter this file directly.
2018-08-09 23:32:10 +00:00
GOLANG_MK_VERSION := 0.3.8
2017-07-18 19:19:40 +00:00
SHELL := /bin/bash
2017-11-08 17:46:29 +00:00
SYSTEM := $( shell uname -a | cut -d" " -f1 | tr '[:upper:]' '[:lower:]' )
.PHONY : golang -test -deps bin /dep golang -ensure -curl -installed
2017-07-18 19:19:40 +00:00
2018-08-09 23:32:10 +00:00
# set timezone to UTC for golang to match circle and deploys
export TZ = UTC
2017-07-18 19:19:40 +00:00
# if the gopath includes several directories, use only the first
GOPATH = $( shell echo $$ GOPATH | cut -d: -f1)
# This block checks and confirms that the proper Go toolchain version is installed.
2017-11-03 17:48:50 +00:00
# It uses ^ matching in the semver sense -- you can be ahead by a minor
# version, but not a major version (patch is ignored).
2017-07-18 19:19:40 +00:00
# arg1: golang version
d e f i n e g o l a n g - v e r s i o n - c h e c k
2017-11-03 17:48:50 +00:00
_ := $( if \
$( shell \
expr >/dev/null \
2018-01-25 15:13:11 +00:00
` go version | cut -d" " -f3 | cut -c3- | cut -d. -f2 | sed -E 's/beta[0-9]+//' ` \
2017-11-03 17:48:50 +00:00
\> = ` echo $( 1) | cut -d. -f2` \
\& \
` go version | cut -d" " -f3 | cut -c3- | cut -d. -f1` \
= ` echo $( 1) | cut -d. -f1` \
&& echo 1) , \
@echo "" , \
$( error must be running Go version ^$( 1) - you are running $( shell go version | cut -d" " -f3 | cut -c3-) ) )
2017-07-18 19:19:40 +00:00
e n d e f
# FGT is a utility that exits with 1 whenever any stderr/stdout output is recieved.
FGT := $( GOPATH) /bin/fgt
$(FGT) :
go get github.com/GeertJohan/fgt
2017-11-08 17:46:29 +00:00
golang-ensure-curl-installed :
@command -v curl >/dev/null 2>& 1 || { echo >& 2 "curl not installed. Please install curl." ; exit 1; }
2018-08-09 23:32:10 +00:00
DEP_VERSION = v0.4.1
2017-11-08 17:46:29 +00:00
DEP_INSTALLED := $( shell [ [ -e "bin/dep" ] ] && bin/dep version | grep version | grep -v go | cut -d: -f2 | tr -d '[:space:]' )
# Dep is a tool used to manage Golang dependencies. It is the offical vendoring experiment, but
# not yet the official tool for Golang.
2018-08-09 23:32:10 +00:00
i f e q ( $( DEP_VERSION ) , $( DEP_INSTALLED ) )
bin/dep : # nothing to do, dep is already up-to-date
e l s e
CACHED_DEP = /tmp/dep-$( DEP_VERSION)
2017-11-08 17:46:29 +00:00
bin/dep : golang -ensure -curl -installed
2018-08-09 23:32:10 +00:00
@echo "Updating dep..."
2017-11-08 17:46:29 +00:00
@mkdir -p bin
2018-08-09 23:32:10 +00:00
@if [ ! -f $( CACHED_DEP) ] ; then curl -o $( CACHED_DEP) -sL https://github.com/golang/dep/releases/download/$( DEP_VERSION) /dep-$( SYSTEM) -amd64; fi ;
@cp $( CACHED_DEP) bin/dep
@chmod +x bin/dep || true
e n d i f
# figure out "github.com/<org>/<repo>"
# `go list` will fail if there are no .go files in the directory
# if this is the case, fall back to assuming github.com/Clever
REF = $( shell go list || echo github.com/Clever/$( notdir $( shell pwd ) ) )
golang-verify-no-self-references :
@if grep -q -i " $( REF) " Gopkg.lock; then echo " Error: Gopkg.lock includes a self-reference ( $( REF) ), which is not allowed. See: https://github.com/golang/dep/issues/1690 " && exit 1; fi ;
@if grep -q -i " $( REF) " Gopkg.toml; then echo " Error: Gopkg.toml includes a self-reference ( $( REF) ), which is not allowed. See: https://github.com/golang/dep/issues/1690 " && exit 1; fi ;
2017-11-08 17:46:29 +00:00
2018-08-09 23:32:10 +00:00
golang-dep-vendor-deps : bin /dep golang -verify -no -self -references
2017-11-08 17:46:29 +00:00
# golang-godep-vendor is a target for saving dependencies with the dep tool
# to the vendor/ directory. All nested vendor/ directories are deleted via
# the prune command.
2018-01-25 15:13:11 +00:00
# In CI, -vendor-only is used to avoid updating the lock file.
i f n d e f C I
2017-11-08 17:46:29 +00:00
d e f i n e g o l a n g - d e p - v e n d o r
2018-01-25 15:13:11 +00:00
b i n / d e p e n s u r e - v
2017-11-08 17:46:29 +00:00
e n d e f
2018-01-25 15:13:11 +00:00
e l s e
d e f i n e g o l a n g - d e p - v e n d o r
b i n / d e p e n s u r e - v - v e n d o r - o n l y
e n d e f
e n d i f
2017-07-18 19:19:40 +00:00
# Golint is a tool for linting Golang code for common errors.
GOLINT := $( GOPATH) /bin/golint
$(GOLINT) :
go get github.com/golang/lint/golint
# golang-fmt-deps requires the FGT tool for checking output
golang-fmt-deps : $( FGT )
# golang-fmt checks that all golang files in the pkg are formatted correctly.
# arg1: pkg path
d e f i n e g o l a n g - f m t
@ e c h o "FORMATTING $(1)..."
@ $( FGT ) g o f m t -l = true $( GOPATH) /src/$( 1) /*.go
e n d e f
# golang-lint-deps requires the golint tool for golang linting.
golang-lint-deps : $( GOLINT )
# golang-lint calls golint on all golang files in the pkg.
# arg1: pkg path
d e f i n e g o l a n g - l i n t
@ e c h o "LINTING $(1)..."
@ f i n d $( GOPATH ) / s r c / $( 1) / * . g o - t y p e f | g r e p - v g e n _ | x a r g s $( GOLINT )
e n d e f
# golang-lint-deps-strict requires the golint tool for golang linting.
golang-lint-deps-strict : $( GOLINT ) $( FGT )
# golang-lint-strict calls golint on all golang files in the pkg and fails if any lint
# errors are found.
# arg1: pkg path
d e f i n e g o l a n g - l i n t - s t r i c t
@ e c h o "LINTING $(1)..."
@ f i n d $( GOPATH ) / s r c / $( 1) / * . g o - t y p e f | g r e p - v g e n _ | x a r g s $( FGT ) $( GOLINT )
e n d e f
# golang-test-deps is here for consistency
golang-test-deps :
# golang-test uses the Go toolchain to run all tests in the pkg.
# arg1: pkg path
d e f i n e g o l a n g - t e s t
@ e c h o "TESTING $(1)..."
@ g o t e s t - v $( 1)
e n d e f
# golang-test-strict-deps is here for consistency
golang-test-strict-deps :
# golang-test-strict uses the Go toolchain to run all tests in the pkg with the race flag
# arg1: pkg path
d e f i n e g o l a n g - t e s t - s t r i c t
@ e c h o "TESTING $(1)..."
@ g o t e s t - v - r a c e $( 1)
e n d e f
# golang-vet-deps is here for consistency
golang-vet-deps :
# golang-vet uses the Go toolchain to vet all the pkg for common mistakes.
# arg1: pkg path
d e f i n e g o l a n g - v e t
@ e c h o "VETTING $(1)..."
@ g o v e t $( GOPATH ) / s r c / $( 1) / * . g o
e n d e f
# golang-test-all-deps installs all dependencies needed for different test cases.
golang-test-all-deps : golang -fmt -deps golang -lint -deps golang -test -deps golang -vet -deps
# golang-test-all calls fmt, lint, vet and test on the specified pkg.
# arg1: pkg path
d e f i n e g o l a n g - t e s t - a l l
$( call golang -fmt ,$ ( 1) )
$( call golang -lint ,$ ( 1) )
$( call golang -vet ,$ ( 1) )
$( call golang -test ,$ ( 1) )
e n d e f
# golang-test-all-strict-deps: installs all dependencies needed for different test cases.
golang-test-all-strict-deps : golang -fmt -deps golang -lint -deps -strict golang -test -strict -deps golang -vet -deps
# golang-test-all-strict calls fmt, lint, vet and test on the specified pkg with strict
# requirements that no errors are thrown while linting.
# arg1: pkg path
d e f i n e g o l a n g - t e s t - a l l - s t r i c t
$( call golang -fmt ,$ ( 1) )
$( call golang -lint -strict ,$ ( 1) )
$( call golang -vet ,$ ( 1) )
$( call golang -test -strict ,$ ( 1) )
e n d e f
2018-01-25 15:13:11 +00:00
# golang-build: builds a golang binary. ensures CGO build is done during CI. This is needed to make a binary that works with a Docker alpine image.
# arg1: pkg path
# arg2: executable name
d e f i n e g o l a n g - b u i l d
@ e c h o "BUILDING..."
@ i f [ - z "$$CI" ] ; t h e n \
go build -o bin/$( 2) $( 1) ; \
e l s e \
echo "-> Building CGO binary" ; \
CGO_ENABLED = 0 go build -installsuffix cgo -o bin/$( 2) $( 1) ; \
f i ;
e n d e f
2017-07-18 19:19:40 +00:00
# golang-update-makefile downloads latest version of golang.mk
golang-update-makefile :
@wget https://raw.githubusercontent.com/Clever/dev-handbook/master/make/golang.mk -O /tmp/golang.mk 2>/dev/null
@if ! grep -q $( GOLANG_MK_VERSION) /tmp/golang.mk; then cp /tmp/golang.mk golang.mk && echo "golang.mk updated" ; else echo "golang.mk is up-to-date" ; fi