Last active
December 18, 2023 03:50
-
-
Save pszponder/28ed7553b5d6d0847edd4c65cad880de to your computer and use it in GitHub Desktop.
Makefile for Go Project
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
# Makefile | |
# https://www.alexedwards.net/blog/a-time-saving-makefile-for-your-go-projects | |
# https://earthly.dev/blog/golang-makefile/ | |
# target: dependencies | |
# action | |
# Variables | |
BINARY_DIR = ./bin | |
CMD_DIR = ./cmd | |
CMD_NAME = CHANGE_ME | |
# Set default goal (when you run make without specifying a goal) | |
.DEFAULT_GOAL := help | |
# ==================================================================================== # | |
# HELPERS | |
# ==================================================================================== # | |
## help: print this help message | |
.PHONY: help | |
help: | |
@echo 'Usage:' | |
@sed -n 's/^##//p' ${MAKEFILE_LIST} | column -t -s ':' | sed -e 's/^/ /' | |
.PHONY: confirm | |
confirm: | |
# Throws error if not confirmed | |
@echo -n 'Are you sure? [y/N] ' && read ans && [ $${ans:-N} = y ] | |
.PHONY: no-dirty | |
no-dirty: | |
# Check for uncommitted changes | |
git diff --exit-code | |
# ==================================================================================== # | |
# QUALITY CONTROL | |
# ==================================================================================== # | |
## tidy: format code and tidy modfile | |
.PHONY: tidy | |
tidy: | |
go fmt ./... | |
go mod tidy -v | |
## audit: run quality control checks | |
.PHONY: audit | |
audit: | |
go mod verify | |
go vet ./... | |
go run honnef.co/go/tools/cmd/staticcheck@latest -checks=all,-ST1000,-U1000 ./... | |
go run golang.org/x/vuln/cmd/govulncheck@latest ./... | |
go test -race -buildvcs -vet=off ./... | |
# ==================================================================================== # | |
# DEPENDENCIES | |
# ==================================================================================== # | |
## deps: download dependencies | |
.PHONY: deps | |
deps: | |
go mod download | |
# ==================================================================================== # | |
# TESTING | |
# ==================================================================================== # | |
## test: run all tests | |
.PHONY: test | |
test: | |
go test -v -race -buildvcs ./... | |
## test/cover: run all tests and display coverage | |
.PHONY: test/cover | |
test/cover: | |
go test -v -race -buildvcs -coverprofile=/tmp/coverage.out ./... | |
go tool cover -html=/tmp/coverage.out | |
# ==================================================================================== # | |
# COMPILING | |
# ==================================================================================== # | |
## build: build executable | |
.PHONY: build | |
build: | |
@echo "Building executable..." | |
mkdir -p $(BINARY_DIR) | |
go build -o $(BINARY_DIR)/$(CMD_NAME) $(CMD_DIR)/$(CMD_NAME) | |
# ==================================================================================== # | |
# RUNNING | |
# ==================================================================================== # | |
## run: run executable | |
.PHONY: run | |
run: build | |
@echo "Running executable..." | |
$(BINARY_DIR)/$(CMD_NAME) | |
# ==================================================================================== # | |
# LIVE RELOADING | |
# ==================================================================================== # | |
## run/live: run executable with live reloading | |
.PHONY: run/live | |
run/live: | |
go run github.com/cosmtrek/air@latest \ | |
--build.cmd "make build" \ | |
--build.bin "$(BINARY_DIR)/$(CMD_NAME)" \ | |
--build.delay "100" \ | |
--build.exclude_dir "" \ | |
--build.include_ext "go, tpl, tmpl, html, css, scss, js, ts, sql, jpeg, jpg, gif, png, bmp, svg, webp, ico" \ | |
--misc.clean_on_exit "true" | |
# ==================================================================================== # | |
# CLEANING | |
# ==================================================================================== # | |
## clean: clean up all build artifacts | |
.PHONY: clean | |
clean: | |
@echo "Cleaning up..." | |
go clean | |
@rm -rf $(BINARY_DIR)/* | |
# ==================================================================================== # | |
# GIT | |
# ==================================================================================== # | |
## push: push changes to the remote Git repository, after running quality control checks | |
.PHONY: push | |
push: tidy audit no-dirty | |
git push |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment