-
Notifications
You must be signed in to change notification settings - Fork 61
/
Makefile
171 lines (142 loc) · 5.64 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
.PHONY: clean
SHELL := /usr/bin/env bash -o pipefail ## Set the shell to use for finding Go files (default: /bin/bash)
# Compile program (implicit default target).
#
# GO_ARGS allows for passing additional arguments.
# e.g. make build GO_ARGS='--ldflags "-s -w"'
.PHONY: build
build: config ## Compile program (CGO disabled)
CGO_ENABLED=0 $(GO_BIN) build $(GO_ARGS) ./cmd/fastly
GO_BIN ?= go ## Allows overriding go executable.
TEST_COMMAND ?= $(GO_BIN) test ## Enables support for tools such as https://github.com/rakyll/gotest
TEST_ARGS ?= -v -timeout 15m ./... ## The compute tests can sometimes exceed the default 10m limit
GOHOSTOS ?= $(shell go env GOHOSTOS || echo unknown)
GOHOSTARCH ?= $(shell go env GOHOSTARCH || echo unknown)
ifeq ($(OS), Windows_NT)
SHELL = cmd.exe
.SHELLFLAGS = /c
GO_FILES = $(shell where /r pkg *.go)
GO_FILES += $(shell where /r cmd *.go)
CONFIG_SCRIPT = scripts\config.sh
CONFIG_FILE = pkg\config\config.toml
else
GO_FILES = $(shell find cmd pkg -type f -name '*.go')
CONFIG_SCRIPT = ./scripts/config.sh
CONFIG_FILE = pkg/config/config.toml
endif
# Build executables using goreleaser (useful for local testing purposes).
#
# You can pass flags to goreleaser via GORELEASER_ARGS
# --clean will save you deleting the dist dir
# --single-target will be quicker and only build for your os & architecture
# --skip=post-hooks which prevents errors such as trying to execute the binary for each OS (e.g. we call scripts/documentation.sh and we can't run Windows exe on a Mac).
# --skip=validate will skip the checks (e.g. git tag checks which result in a 'dirty git state' error)
#
# EXAMPLE:
# make release GORELEASER_ARGS="--clean --skip=post-hooks --skip=validate"
release: dependencies $(GO_FILES) ## Build executables using goreleaser
@GOHOSTOS="${GOHOSTOS}" GOHOSTARCH="${GOHOSTARCH}" goreleaser build ${GORELEASER_ARGS}
# Useful for attaching a debugger such as https://github.com/go-delve/delve
debug:
@$(GO_BIN) build -gcflags="all=-N -l" $(GO_ARGS) -o "fastly" ./cmd/fastly
.PHONY: config
config:
@$(CONFIG_SCRIPT)
.PHONY: all
all: config dependencies tidy fmt vet staticcheck gosec semgrep imports test build install ## Run EVERYTHING!
# Update CI tools used by ./.github/workflows/pr_test.yml
.PHONY: dependencies
dependencies:
@while read -r line || [ -n "$$line" ]; do \
$(GO_BIN) install $$line; \
done < .github/dependencies.txt
@if [ "$$(uname)" = 'Darwin' ]; then \
if ! command -v semgrep &> /dev/null; then \
brew install semgrep; \
fi \
fi
# Clean up Go modules file.
.PHONY: tidy
tidy:
$(GO_BIN) mod tidy
# Run formatter.
.PHONY: fmt
fmt:
@echo gofmt -l ./{cmd,pkg}
@eval "bash -c 'F=\$$(gofmt -l ./{cmd,pkg}) ; if [[ \$$F ]] ; then echo \$$F ; exit 1 ; fi'"
# Run static analysis.
.PHONY: vet
vet: config ## Run vet static analysis
$(GO_BIN) vet ./{cmd,pkg}/...
# Run linter.
.PHONY: revive
revive: ## Run linter (using revive)
revive ./...
# Run security vulnerability checker.
.PHONY: gosec
gosec: ## Run security vulnerability checker
gosec -quiet -exclude=G104 ./{cmd,pkg}/...
nilaway: ## Run nilaway
@nilaway ./...
# Run semgrep checker.
# NOTE: We can only exclude the import-text-template rule via a semgrep CLI flag
.PHONY: semgrep
semgrep: ## Run semgrep
@if [ '$(SEMGREP_SKIP)' != 'true' ]; then \
if command -v semgrep &> /dev/null; then semgrep ci --config auto --exclude-rule go.lang.security.audit.xss.import-text-template.import-text-template $(SEMGREP_ARGS); fi \
fi
# Run third-party static analysis.
# To ignore lines use: //lint:ignore <CODE> <REASON>
.PHONY: staticcheck
staticcheck: ## Run static analysis
staticcheck ./{cmd,pkg}/...
# Run imports formatter.
.PHONY: imports
imports:
@echo goimports ./{cmd,pkg}
@eval "bash -c 'F=\$$(goimports -l ./{cmd,pkg}) ; if [[ \$$F ]] ; then echo \$$F ; exit 1 ; fi'"
.PHONY: golangci
golangci: ## Run golangci-lint
golangci-lint run --verbose
# Run tests
.PHONY: test
test: config ## Run tests (with race detection)
@$(TEST_COMMAND) -race $(TEST_ARGS)
# Compile and install program.
#
# GO_ARGS allows for passing additional arguments.
.PHONY: install
install: config ## Compile and install program
CGO_ENABLED=0 $(GO_BIN) install $(GO_ARGS) ./cmd/fastly
# Scaffold a new CLI command from template files.
.PHONY: scaffold
scaffold:
@$(shell pwd)/scripts/scaffold.sh $(CLI_PACKAGE) $(CLI_COMMAND) $(CLI_API)
# Scaffold a new CLI 'category' command from template files.
.PHONY: scaffold-category
scaffold-category:
@$(shell pwd)/scripts/scaffold-category.sh $(CLI_CATEGORY) $(CLI_CATEGORY_COMMAND) $(CLI_PACKAGE) $(CLI_COMMAND) $(CLI_API)
# Graph generates a call graph that focuses on the specified package.
# Output is callvis.svg
# e.g. make graph PKG_IMPORT_PATH=github.com/fastly/cli/pkg/commands/kvstoreentry
.PHONY: graph
graph: ## Graph generates a call graph that focuses on the specified package
@$(GO_BIN) install github.com/ofabry/go-callvis@latest 2>/dev/null
go-callvis -file "callvis" -focus "$(PKG_IMPORT_PATH)" ./cmd/fastly/
@rm callvis.gv
.PHONY: deps-app-update
deps-app-update: ## Update all application dependencies
$(GO_BIN) get -u -d -t ./...
$(GO_BIN) mod tidy
if [ -d "vendor" ]; then $(GO_BIN) mod vendor; fi
.PHONY: help
help:
@printf "Targets\n"
@(grep -h -E '^[0-9a-zA-Z_.-]+:.*?## .*$$' $(MAKEFILE_LIST) || true) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-22s\033[0m %s\n", $$1, $$2}'
@printf "\nDefault target\n"
@printf "\033[36m%s\033[0m" $(.DEFAULT_GOAL)
@printf "\n\nMake Variables\n"
@(grep -h -E '^[0-9a-zA-Z_.-]+\s[:?]?=.*? ## .*$$' $(MAKEFILE_LIST) || true) | sort | awk 'BEGIN {FS = "[:?]?=.*?## "}; {printf "\033[36m%-25s\033[0m %s\n", $$1, $$2}'
.PHONY: run
run: config
$(GO_BIN) run cmd/fastly/main.go $(GO_ARGS)