-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
93 lines (74 loc) · 2.76 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# Project directories
ROOT_DIR := $(CURDIR)
BUILD_DIR := $(ROOT_DIR)/build
# Ensure everything works even if GOPATH is not set, which is often the case.
GOPATH ?= $(shell go env GOPATH)
# Set $(GOBIN) to project directory for keeping everything in one place
GOBIN = $(ROOT_DIR)/bin
# All go files belong to project
GOFILES = $(shell find . -type f -name '*.go' -not -path './vendor/*')
# Commands used in Makefile
GOCMD := GOBIN=$(GOBIN) go
GOBUILD := $(GOCMD) build
GOTEST := $(GOCMD) test
GOMOD := $(GOCMD) mod
GOINSTALL := $(GOCMD) install
GOCLEAN := $(GOCMD) clean
GOFMT := gofmt
GOLANGCILINT := $(GOBIN)/golangci-lint
MODULE := $(shell $(GOCMD) list -m)
VERSION ?= 0.1.0+dev
COMMIT := $(strip $(shell [ -d .git ] && git describe --always --tags --dirty))
BUILD_TIMESTAMP := $(shell date -u +"%Y-%m-%dT%H:%M:%S%Z")
# Build variables
BUILD_LDFLAGS := '-s -w -X main.version=$(VERSION) -X main.commit=$(COMMIT) -X main.date=$(BUILD_TIMESTAMP)'
# Helper variabless
V = 0
Q = $(if $(filter 1,$V),,@)
M = $(shell printf "\033[34;1m▶\033[0m")
.PHONY: help
default: help
.PHONY: gar
gar: ## Builds gar binary
gar: vendor cmd/main.go $(wildcard *.go) $(wildcard */*.go) $(BUILD_DIR) ; $(info $(M) building binary)
$(Q) CGO_ENABLED=0 $(GOBUILD) -a -tags netgo -ldflags $(BUILD_LDFLAGS) -o $(BUILD_DIR)/$@ cmd/main.go
.PHONY: vendor
vendor: ## Updates vendored copy of dependencies
vendor: ; $(info $(M) running go mod vendor)
$(Q) $(GOMOD) tidy
$(Q) $(GOMOD) vendor
.PHONY: fix
fix: ## Fix found issues (if it's supported by the $(GOLANGCILINT))
fix: install-tools ; $(info $(M) runing golangci-lint run --fix)
$(Q) $(GOLANGCILINT) run --fix --enable-all -c .golangci.yml
.PHONY: fmt
fmt: ## Runs gofmt
fmt: ; $(info $(M) runnig gofmt )
$(Q) $(GOFMT) -d -s $(GOFILES)
.PHONY: lint
lint: ## Runs golangci-lint analysis
lint: install-tools vendor fmt ; $(info $(M) runnig golangci-lint analysis)
$(Q) $(GOLANGCILINT) run
.PHONY: clean
clean: ## Cleanup everything
clean: ; $(info $(M) cleaning )
$(Q) $(GOCLEAN)
$(Q) $(shell rm -rf $(GOBIN) $(BUILD_DIR))
.PHONY: test
test: ## Runs go test
test: ; $(info $(M) runnig tests)
$(Q) $(GOTEST) -race -cover -v ./...
.PHONY: install-tools
install-tools: ## Install tools
install-tools: vendor ; $(info $(M) installing tools)
$(Q) $(GOINSTALL) $(shell cat tools.go | grep _ | awk -F'"' '{print $$2}')
.PHONY: help
help: ## Shows this help message
$(Q) echo 'usage: make [target] ...'
$(Q) echo
$(Q) echo 'targets : '
$(Q) echo
$(Q) fgrep -h "##" $(MAKEFILE_LIST) | fgrep -v fgrep | sed -e 's/\\$$//' | sed -e 's/##//'| column -s: -t
$(Q) echo
$(BUILD_DIR): ; $(info $(M) creating build directory)
$(Q) $(shell mkdir -p $@)