Skip to content

Instantly share code, notes, and snippets.

@eze-kiel
Created April 22, 2022 13:25
Show Gist options
  • Save eze-kiel/a21c3f9b081c8f68f9d9584dee784a7f to your computer and use it in GitHub Desktop.
Save eze-kiel/a21c3f9b081c8f68f9d9584dee784a7f to your computer and use it in GitHub Desktop.
Base code I use in my go projects Makefiles
GOCMD=go
GOTEST=$(GOCMD) test
GOVET=$(GOCMD) vet
BINARY_NAME=
VERSION=$(shell git describe --tags)
DOCKER_REGISTRY?=
BUILD_DATE=$(shell date +'%Y-%m-%d_%H:%M:%ST%Z')
GREEN := $(shell tput -Txterm setaf 2)
YELLOW := $(shell tput -Txterm setaf 3)
WHITE := $(shell tput -Txterm setaf 7)
RESET := $(shell tput -Txterm sgr0)
.PHONY: all build clean
all: help
## Build:
build: ## Build the Go project
mkdir -p out/bin
GO111MODULE=on $(GOCMD) build \
-o out/bin/$(BINARY_NAME) \
-ldflags="-X 'main.Version=$(VERSION)' -X 'main.BuildDate=$(BUILD_DATE)'" \
.
clean: ## Clean all the files and binaries generated by the Makefile
rm -rf ./out
## Test:
test: ## Run the tests of the project
ifeq ($(EXPORT_RESULT), true)
GO111MODULE=off go get -u github.com/jstemmer/go-junit-report
$(eval OUTPUT_OPTIONS = | tee /dev/tty | go-junit-report -set-exit-code > junit-report.xml)
endif
$(GOTEST) -v -race ./... $(OUTPUT_OPTIONS)
coverage: ## Run the tests of the project and export the coverage
$(GOTEST) -cover -covermode=count -coverprofile=profile.cov ./...
$(GOCMD) tool cover -func profile.cov
ifeq ($(EXPORT_RESULT), true)
GO111MODULE=off go get -u github.com/AlekSi/gocov-xml
GO111MODULE=off go get -u github.com/axw/gocov/gocov
gocov convert profile.cov | gocov-xml > coverage.xml
endif
## Help:
help: ## Show this help
@echo ''
@echo 'Usage:'
@echo ' ${YELLOW}make${RESET} ${GREEN}<target>${RESET}'
@echo ''
@echo 'Targets:'
@awk 'BEGIN {FS = ":.*?## "} { \
if (/^[0-9a-zA-Z_-]+:.*?##.*$$/) {printf " ${YELLOW}%-20s${GREEN}%s${RESET}\n", $$1, $$2} \
else if (/^## .*$$/) {printf " ${CYAN}%s${RESET}\n", substr($$1,4)} \
}' $(MAKEFILE_LIST)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment