From 5e1df7b29bf6b8185fc638d3940dd5101ae20d63 Mon Sep 17 00:00:00 2001 From: Mike Mason Date: Mon, 17 Jul 2023 19:36:37 +0000 Subject: [PATCH] add makefile --- Makefile | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..dd87810 --- /dev/null +++ b/Makefile @@ -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