Created
October 6, 2024 04:17
-
-
Save crissilvaeng/5717edddabae2f4fa98dcb345b10568d to your computer and use it in GitHub Desktop.
Golang Makefile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
APP_NAME ?= $(shell basename `go list`) | |
GOPRIVATE ?= $(dir $(shell go list))* | |
LOCAL := $(dir $(shell go list)) | |
OUTPUT := $(CURDIR)/out | |
SOURCES := $(shell find . -type f -name '*.go' -not -path "./vendor/*") | |
TOOLS := goimports golangci-lint | |
SHELL := /bin/bash | |
.PHONY: help all \ | |
env deps tidy update vet imports fmt lint test coverage build \ | |
run check clean install uninstall | |
default: help | |
$(foreach bin,$(TOOLS), \ | |
$(if $(shell command -v $(bin) 2> /dev/null),,$(error please install `$(bin)`))) | |
ifeq (run, $(firstword $(MAKECMDGOALS))) | |
runargs := $(wordlist 2, $(words $(MAKECMDGOALS)), $(MAKECMDGOALS)) | |
$(eval $(runargs):;@true) | |
endif | |
help: ## show this help message | |
@printf 'Makefile for $(APP_NAME).\n\n' | |
@printf 'Usage:\n\n' | |
@printf '\tmake <target> [arguments]\n\n' | |
@printf 'The targets are:\n\n' | |
@egrep '^(.+)\:\ .*##\ (.+)' ${MAKEFILE_LIST} | sed 's/:.*##/#/' | column -t -c 2 -s '#' | sed 's/^/ /' | |
env: ## display environment variables used in this Makefile for debugging purposes | |
@echo "APP_NAME=$(APP_NAME)" | |
deps: ## install project dependencies (using Go modules' vendor command) | |
@go mod vendor | |
tidy: deps ## tidy up go modules, remove unnecessary dependencies | |
@go mod tidy | |
update: ## update go modules to latest versions | |
@go get -u ./... | |
@go mod tidy | |
vet: deps ## run 'go vet' for static code analysis | |
@go vet ./... | |
imports: deps ## run goimports to format imports | |
@goimports -local $(LOCAL) -w $(SOURCES) | |
fmt: deps imports ## format go code to maintain consistent coding style | |
@go fmt ./... | |
lint: deps ## lint the go code using golangci-lint | |
@golangci-lint run ./... # available linters: https://golangci-lint.run/usage/linters/ | |
test: deps ## run tests with race detector and generate coverage profile | |
@go test -race -count=1 -coverprofile=$(OUTPUT)/coverage.out -covermode=atomic ./... | |
coverage: test ## display test coverage in an HTML format | |
@go tool cover -html=$(OUTPUT)/coverage.out | |
build: deps ## build the binary and place it in the OUTPUT | |
@go build -race -o $(OUTPUT)/$(APP_NAME) main.go | |
run: build ## run the binary | |
@$(OUTPUT)/$(APP_NAME) $(runargs) | |
check: ## run all quality checks such as formatting, vetting, and linting | |
@make fmt | |
@make vet | |
@make lint | |
clean: ## clean up all generated files and directories | |
@go clean ./... | |
@rm -rf $(OUTPUT) | |
install: ## install the binary globally | |
@go install | |
uninstall: ## uninstall the binary and remove installed binaries | |
@go clean -i | |
all: check build test ## run all tasks: checks, build, and tests | |
@echo "Running all tasks..." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment