-
Notifications
You must be signed in to change notification settings - Fork 1
/
Makefile
192 lines (159 loc) · 6.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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
.DEFAULT_GOAL := help
MAKEFLAGS += --silent --no-print-directory
BIN_DIR := ./bin
# renovate datasource=github-releases depName=abice/go-enum
GO_ENUM_VERSION := v0.6.0
# renovate datasource=github-releases depName=securego/gosec
GOSEC_VERSION := v2.20.0
# renovate datasource=github-releases depName=golangci/golangci-lint
GOLANGCI_LINT_VERSION := v1.60.1
# renovate datasource=go depName=golang.org/x/vuln/cmd/govulncheck
GOVULNCHECK_VERSION := v1.1.3
# renovate datasource=go depName=golang.org/x/tools/cmd/goimports
GOIMPORTS_VERSION := v0.28.0
# renovate datasource=go depName=github.com/vburenin/ifacemaker
IFACEMAKER_VERSION := v1.2.1
# Check if the program is present in $PATH and install otherwise.
# ${1} - oneOf{binary,yarn}
# ${2} - program name
define _ensure_installed
LOCAL_BIN_DIR=$(BIN_DIR) ./scripts/ensure_installed.sh "${1}" "${2}"
endef
# Install Go binary using 'go install' with an output directory set via $GOBIN.
# ${1} - repository url
define _install_go_binary
GOBIN=$(realpath $(BIN_DIR)) go install "${1}"
endef
# Print Makefile target step description for check.
# Only print 'check' steps this way, and not dependent steps, like 'install'.
# ${1} - step description
define _print_check_step
printf -- '------\n%s...\n' "${1}"
endef
.PHONY: test test/e2e test/record
## Run all unit tests.
test:
go test -race -cover ./... ./docs/mock_example
## Run all end-to-end tests (requires Nobl9 platform credentials).
test/e2e:
# The '-count=1' flag disables tests results caching, as per https://go.dev/doc/go1.10#test.
@if [ "$(GITHUB_ACTIONS)" != "true" ]; then \
export NOBL9_SDK_TEST_RUN_SEQUENTIAL_APPLY_AND_DELETE=true; \
fi; \
go test -count=1 -race -test.v -timeout=5m -tags=e2e_test ./tests
## Record tests and save them in ./bin/recorded-tests.json.
test/record:
RECORD_FILE="$(abspath $(dir .))/$(BIN_DIR)/recorded-tests" ; \
NOBL9_SDK_TEST_RECORD_FILE="$$RECORD_FILE" go test ./... ; \
jq -s < "$$RECORD_FILE" > "$$RECORD_FILE.json"
.PHONY: check check/vet check/lint check/gosec check/spell check/trailing check/markdown check/format check/generate check/vulns
## Run all checks.
check: check/vet check/lint check/gosec check/spell check/trailing check/markdown check/format check/generate check/vulns
## Run 'go vet' on the whole project.
check/vet:
$(call _print_check_step,Running go vet)
go vet -tags=e2e_test ./...
## Run golangci-lint all-in-one linter with configuration defined inside .golangci.yml.
check/lint:
$(call _print_check_step,Running golangci-lint)
$(call _ensure_installed,binary,golangci-lint)
$(BIN_DIR)/golangci-lint run
## Check for security problems using gosec, which inspects the Go code by scanning the AST.
check/gosec:
$(call _print_check_step,Running gosec)
$(call _ensure_installed,binary,gosec)
$(BIN_DIR)/gosec -exclude-generated -quiet ./...
## Check spelling, rules are defined in cspell.json.
check/spell:
$(call _print_check_step,Verifying spelling)
$(call _ensure_installed,yarn,cspell)
yarn --silent cspell --no-progress '**/**'
## Check for trailing whitespaces in any of the projects' files.
check/trailing:
$(call _print_check_step,Looking for trailing whitespaces)
yarn --silent check-trailing-whitespaces
## Check markdown files for potential issues with markdownlint.
check/markdown:
$(call _print_check_step,Verifying Markdown files)
$(call _ensure_installed,yarn,markdownlint)
yarn --silent markdownlint '**/*.md' --ignore node_modules
## Check for potential vulnerabilities across all Go dependencies.
check/vulns:
$(call _print_check_step,Running govulncheck)
$(call _ensure_installed,binary,govulncheck)
$(BIN_DIR)/govulncheck ./...
## Verify if the auto generated code has been committed.
check/generate:
$(call _print_check_step,Checking if generated code matches the provided definitions)
./scripts/check-generate.sh
## Verify if the files are formatted.
## You must first commit the changes, otherwise it won't detect the diffs.
check/format:
$(call _print_check_step,Checking if files are formatted)
./scripts/check-formatting.sh
.PHONY: generate generate/code generate/examples
## Auto generate files.
generate: generate/code generate/examples
## Generate Golang code.
generate/code:
echo "Generating Go code..."
$(call _ensure_installed,binary,go-enum)
$(call _ensure_installed,binary,ifacemaker)
go generate -tags=e2e_test ./... ./docs/mock_example
${MAKE} format/go
## Generate examples from code.
generate/examples:
echo "Generating examples..."
go run internal/cmd/examplegen/main.go
.PHONY: format format/go format/cspell
## Format files.
format: format/go format/cspell
## Format Go files.
format/go:
echo "Formatting Go files..."
$(call _ensure_installed,binary,goimports)
gofmt -w -l -s .
$(BIN_DIR)/goimports -local=github.com/nobl9/nobl9-go -w .
## Format cspell config file.
format/cspell:
echo "Formatting cspell.yaml configuration (words list)..."
$(call _ensure_installed,yarn,yaml)
yarn --silent format-cspell-config
.PHONY: install install/yarn install/go-enum install/golangci-lint install/gosec install/govulncheck install/goimports install/ifacemaker
## Install all dev dependencies.
install: install/yarn install/go-enum install/golangci-lint install/gosec install/govulncheck install/goimports install/ifacemaker
## Install JS dependencies with yarn.
install/yarn:
echo "Installing yarn dependencies..."
yarn --silent install
## Install go-enum, an enum generator for Go (https://github.com/abice/go-enum).
install/go-enum:
echo "Downloading go-enum..."
curl -fsSL "https://github.com/abice/go-enum/releases/download/$(GO_ENUM_VERSION)/go-enum_$$(uname -s)_$$(uname -m)" \
-o $(BIN_DIR)/go-enum && chmod +x $(BIN_DIR)/go-enum
## Install golangci-lint (https://golangci-lint.run).
install/golangci-lint:
echo "Installing golangci-lint..."
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh |\
sh -s -- -b $(BIN_DIR) $(GOLANGCI_LINT_VERSION)
## Install gosec (https://github.com/securego/gosec).
install/gosec:
echo "Installing gosec..."
curl -sfL https://raw.githubusercontent.com/securego/gosec/master/install.sh |\
sh -s -- -b $(BIN_DIR) $(GOSEC_VERSION)
## Install govulncheck (https://pkg.go.dev/golang.org/x/vuln/cmd/govulncheck).
install/govulncheck:
echo "Installing govulncheck..."
$(call _install_go_binary,golang.org/x/vuln/cmd/govulncheck@$(GOVULNCHECK_VERSION))
## Install goimports (https://pkg.go.dev/golang.org/x/tools/cmd/goimports).
install/goimports:
echo "Installing goimports..."
$(call _install_go_binary,golang.org/x/tools/cmd/goimports@$(GOIMPORTS_VERSION))
## Install ifacemaker (https://github.com/vburenin/ifacemaker).
install/ifacemaker:
echo "Installing ifacemaker..."
$(call _install_go_binary,github.com/vburenin/ifacemaker@$(IFACEMAKER_VERSION))
.PHONY: help
## Print this help message.
help:
./scripts/makefile-help.awk $(MAKEFILE_LIST)