add makefile

This commit is contained in:
Mike Mason
2023-07-17 19:36:37 +00:00
parent 544273cd0b
commit 5e1df7b29b

57
Makefile Normal file
View File

@@ -0,0 +1,57 @@
GOPKG=go.equinixmetal.net/infra9-metal-bridge
ROOT_DIR := $(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))
TOOLS_DIR := .tools
GO_FILES=$(shell git ls-files '*.go')
GOTOOLS_REPO = golang.org/x/tools
GOTOOLS_VERSION = v0.9.3
GOLANGCI_LINT_REPO = github.com/golangci/golangci-lint
GOLANGCI_LINT_VERSION = v1.51.2
.PHONY: all help lint test golint fix-lint unit-test coverage privkey
help: Makefile ## Print help.
@grep -h "##" $(MAKEFILE_LIST) | grep -v grep | sed -e 's/:.*##/#/' | column -c 2 -t -s#
all: lint test ## Lints and tests.
lint: golint ## Runs all lint checks.
golint: | $(TOOLS_DIR)/golangci-lint ## Runs Go lint checks.
@echo Linting Go files...
@$(TOOLS_DIR)/golangci-lint run --timeout=5m
fix-lint: $(GO_FILES) | $(TOOLS_DIR)/goimports ## Runs goimports on all go files.
@echo Linting go files...
@$(TOOLS_DIR)/goimports -w -local $(GOPKG) $^
test: | unit-test ## Regenerate files and run unit tests.
unit-test: ## Runs unit tests.
@echo Running unit tests...
@go test -timeout 30s -cover -short ./...
.PHONY: coverage
coverage: ## Generates a test coverage report.
@echo Generating coverage report...
@go test -timeout 30s ./... -coverprofile=coverage.out -covermode=atomic
@go tool cover -func=coverage.out
@go tool cover -html=coverage.out
# Tools setup
$(TOOLS_DIR):
mkdir -p $(TOOLS_DIR)
$(TOOLS_DIR)/goimports: | $(TOOLS_DIR)
@echo "Installing $(GOTOOLS_REPO)/cmd/goimports@$(GOTOOLS_VERSION)"
@GOBIN=$(ROOT_DIR)/$(TOOLS_DIR) go install $(GOTOOLS_REPO)/cmd/goimports@$(GOTOOLS_VERSION)
$(TOOLS_DIR)/golangci-lint: | $(TOOLS_DIR)
@echo "Installing $(GOLANGCI_LINT_REPO)/cmd/golangci-lint@$(GOLANGCI_LINT_VERSION)"
@GOBIN=$(ROOT_DIR)/$(TOOLS_DIR) go install $(GOLANGCI_LINT_REPO)/cmd/golangci-lint@$(GOLANGCI_LINT_VERSION)
$@ version
$@ linters