-
Notifications
You must be signed in to change notification settings - Fork 2
/
Makefile
366 lines (301 loc) · 15.1 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#-------------------------------------------------------------------------------
# Running `make` will show the list of subcommands that will run.
include ./standard.mk
# go install golang.org/dl/go{VERSION}@latest
# go{VERSION} download
GO=$(shell which go)
GO_VER=1.23
GOBIN=$(shell ./find-go-bin.sh)
BINARY_NAME=terraform-provider-corefunc
#-------------------------------------------------------------------------------
# Global stuff.
HOMEBREW_PACKAGES=bash bats-core coreutils editorconfig-checker findutils git git-cliff git-lfs go grep jq librsvg nodejs poetry pre-commit python@3.11 shellcheck tfschema trivy trufflesecurity/trufflehog/trufflehog
NEXT_VERSION ?= $(shell git cliff --bump --unreleased --context | jq -r .[0].version)
#-------------------------------------------------------------------------------
# Installation
.PHONY: install-tools-go
## install-tools-go: [tools]* Install/upgrade the required Go packages.
install-tools-go:
@ $(HEADER) "=====> Installing Go packages..."
$(GO) install github.com/antham/gommit@latest
$(GO) install github.com/charmbracelet/gum@latest
$(GO) install github.com/evilmartians/lefthook@latest
$(GO) install github.com/google/osv-scanner/cmd/osv-scanner@v1
$(GO) install github.com/goph/licensei/cmd/licensei@latest
$(GO) install github.com/hashicorp/terraform-plugin-docs/cmd/tfplugindocs@latest
$(GO) install github.com/mdempsky/unconvert@latest
$(GO) install github.com/nikolaydubina/go-binsize-treemap@latest
$(GO) install github.com/nikolaydubina/go-cover-treemap@latest
$(GO) install github.com/nikolaydubina/smrcptr@latest
$(GO) install github.com/orlangure/gocovsh@latest
$(GO) install github.com/pelletier/go-toml/v2/cmd/tomljson@latest
$(GO) install github.com/quasilyte/go-consistent@latest
$(GO) install github.com/rhysd/actionlint/cmd/actionlint@latest
$(GO) install github.com/securego/gosec/v2/cmd/gosec@latest
$(GO) install github.com/trufflesecurity/driftwood@latest
$(GO) install golang.org/x/perf/cmd/benchstat@latest
$(GO) install golang.org/x/tools/cmd/godoc@latest
$(GO) install golang.org/x/vuln/cmd/govulncheck@latest
$(GO) install gotest.tools/gotestsum@latest
$(GO) install mvdan.cc/gofumpt@latest
.PHONY: install-tools-mac
## install-tools-mac: [tools]* Install/upgrade the required tools for macOS, including Go packages.
install-tools-mac: install-tools-go
@ $(HEADER) "=====> Installing required packages for macOS (Homebrew)..."
brew update && brew install $(HOMEBREW_PACKAGES) && brew upgrade $(HOMEBREW_PACKAGES)
curl -sSLf https://raw.githubusercontent.com/mtdowling/chag/master/install.sh | sudo bash
@ $(BORDER) "To update to the latest versions, run:" " brew update && brew upgrade"
.PHONY: install-hooks
## install-hooks: [tools]* Install/upgrade the Git hooks used for ensuring consistency.
install-hooks:
@ $(HEADER) "=====> Installing Git hooks..."
cp -vf .githooks/commit-msg.sh .git/hooks/commit-msg
chmod +x .git/hooks/*
pre-commit install
@ $(BORDER) "Learn more about 'pre-commit' at:" " https://pre-commit.com" " " "Learn more about 'gommit' at:" " https://github.com/antham/gommit"
#-------------------------------------------------------------------------------
# Compile
.PHONY: tidy
## tidy: [build] Updates go.mod and downloads dependencies.
tidy:
@ $(HEADER) "=====> Tidy and download the Go dependencies..."
$(GO) mod tidy -go=$(GO_VER) -v
.PHONY: godeps
## godeps: [build] Updates go.mod and downloads dependencies.
godeps:
@ $(HEADER) "=====> Upgrade the minor versions of Go dependencies..."
find . -type f -name "go.mod" | xargs -I% dirname "%" | xargs -I@ bash -c 'cd "@" && $(GO) mod tidy -go=$(GO_VER) && $(GO) get -u -t -v ./...'
@ echo ""
@ $(YELLOW) "Run 'make tidy' to clean up the go.mod file."
.PHONY: build
## build: [build]* Builds and installs the Terraform provider locally.
build: tidy
@ $(HEADER) "=====> Building and installing the provider..."
$(GO) install -a -ldflags="-s -w" .
@ ls -lahF $(GOBIN)/$(BINARY_NAME)
#-------------------------------------------------------------------------------
# Clean
.PHONY: clean-go
## clean-go: [clean] Clean Go's module cache.
clean-go:
@ $(HEADER) "=====> Cleaning Go cache..."
$(GO) clean -i -r -x -testcache -modcache -cache
.PHONY: clean-tests
## clean-tests: [clean] Cleans all test artifacts.
clean-tests:
@ $(HEADER) "=====> Cleaning artifacts from tests..."
- find . -type d -name ".terraform" | xargs -I% rm -fv "%"
- find . -type d -name "terratest-*" | xargs -I% rm -fv "%"
- find . -type f -name "terraform.tfstate*" | xargs -I% rm -fv "%"
- find ./examples -type d -name "\.*" | xargs -I% rm -fv "%"
.PHONY: clean-bench
## clean-bench: [clean] Cleans all benchmarking-related files.
clean-bench:
@ $(HEADER) "=====> Cleaning artifacts from benchmarks..."
- find . -type f -name "__*.out" | xargs -I% rm -fv "%"
- find . -type f -name "*.test" | xargs -I% rm -fv "%"
.PHONY: clean-tf
## clean-tf: [clean] Clean Terra/Fu leftovers.
clean-tf:
@ $(HEADER) "=====> Cleaning Terra/Fu artifacts..."
find . -type d -name "terraform.d" | xargs -I% rm -Rfv "%"
find . -type d -name ".terraform" | xargs -I% rm -Rfv "%"
find . -type f -name ".terraform.lock.hcl" | xargs -I% rm -fv "%"
.PHONY: clean
## clean: [clean]* Runs ALL cleaning tasks (except the Go cache).
clean: clean-bench clean-tf clean-tests clean-ds
#-------------------------------------------------------------------------------
# Documentation
.PHONY: docs
## docs: [docs]* Runs primary documentation tasks.
docs: docs-provider docs-cli
.PHONY: docs-provider
## docs-provider: [docs] Generate Terraform Registry documentation.
docs-provider: clean-ds
@ $(HEADER) "=====> Generating Terraform provider documentation..."
$(GO) get -u github.com/hashicorp/terraform-plugin-docs/cmd/tfplugindocs
$(GO) generate -v ./...
@ $(HEADER) "=====> Remove tfplugindocs comments..."
find docs/ -type f -name "*.md" | xargs -I% sed -i 's|<!-- arguments generated by tfplugindocs -->||g' "%"
find docs/ -type f -name "*.md" | xargs -I% sed -i 's|<!-- variadic argument generated by tfplugindocs -->||g' "%"
@ echo " "
# If this fails, there's a bigger issue.
lefthook run pre-commit --commands markdownlint
.PHONY: docs-cli
## docs-cli: [docs] Preview the Go library documentation on the CLI.
docs-cli:
@ $(HEADER) "=====> Displaying Go CLI documentation..."
$(GO) doc -C corefunc/ -all
.PHONY: docs-serve
## docs-serve: [docs] Preview the Go library documentation as displayed on pkg.go.dev.
docs-serve:
@ $(HEADER) "=====> Displaying Go HTTP documentation..."
open http://localhost:6060/pkg/github.com/northwood-labs/terraform-provider-corefunc/corefunc/
godoc -index -links
.PHONY: binsize
## binsize: [docs] Analyze the size of the binary by Go package.
binsize:
@ $(HEADER) "=====> Displaying Go HTTP documentation..."
$(GO) tool nm -size "$(GOBIN)/$(BINARY_NAME)" | go-binsize-treemap > binsize.svg
rsvg-convert --width=2000 --format=png --output="binsize.png" "binsize.svg"
#-------------------------------------------------------------------------------
# Linting
.PHONY: pre-commit
## pre-commit: [lint]* Runs `pre-commit` against all files.
pre-commit:
@ $(HEADER) "=====> Running pre-commit..."
pre-commit run --all-files
.PHONY: license
## license: [lint]* Checks the licenses of all files and dependencies.
license:
@ $(HEADER) "=====> Checking license usage..."
@ - trivy fs --config trivy-license.yaml --format json . 2>/dev/null > .licenses.cache.json
@ cat .licenses.cache.json | jq -Mr '[.Results[] | select(.Packages) | .Packages[] | select(.Licenses) | .Licenses[]] | to_entries | group_by(.value)[] | {Name: .[0].value, Count: length} | "\(.Name): \(.Count)"'
@ $(HEADER) "=====> Checking license headers..."
@ echo "Missing/outdated:"
@ - licensei header
.PHONY: lint
## lint: [lint]* Runs ALL linting/validation tasks.
lint: license pre-commit
#-------------------------------------------------------------------------------
# Testing
# https://github.com/golang/go/wiki/TableDrivenTests
# https://go.dev/doc/tutorial/fuzz
# https://pkg.go.dev/testing
# https://pkg.go.dev/golang.org/x/perf/cmd/benchstat
.PHONY: test
## test: [test]* Runs ALL tests.
test: unit examples acc mutate terratest bats
.PHONY: list-tests
## list-tests: [test] Lists all of the tests that are available to run.
list-tests:
@ $(HEADER) "=====> Unit tests..."
@ echo "make unit"
@ cat ./corefunc/*_test.go | ggrep "func Test" | gsed 's/func\s//g' | gsed -r 's/\(.*//g' | gsed -r 's/Test/make unit NAME=/g'
@ $(HEADER) "=====> Terraform acceptance tests..."
@ echo "make acc"
@ cat ./corefuncprovider/*_test.go | ggrep "func TestAcc" | gsed 's/func\s//g' | gsed -r 's/\(.*//g' | gsed -r 's/TestAcc/make acc NAME=/g'
@ $(HEADER) "=====> Example tests..."
@ echo "make examples"
@ cat ./corefunc/*_test.go | ggrep "func Example" | gsed 's/func\s//g' | gsed -r 's/\(.*//g' | gsed -r 's/Example/make examples NAME=/g'
@ $(HEADER) "=====> Fuzzing tests..."
@ echo "make fuzz"
@ $(HEADER) "=====> Terratest tests..."
@ echo "make terratest"
@ $(HEADER) "=====> BATS tests..."
@ echo "make bats"
.PHONY: bats
## bats: [test] Tests the output of the provider using tfschema and BATS.
bats: build clean
@ $(HEADER) "=====> Running BATS/tfschema tests..."
@ # cd ./bats && terraform init && bats *.bats.sh
cd ./bats && bats *.bats.sh
.PHONY: acc
## acc: [test] Runs Terraform provider acceptance tests. Set NAME= (without 'TestAcc') to run a specific test by name.
acc:
@ $(HEADER) "=====> Running acceptance tests..."
ifeq ($(DEBUG), true)
PROVIDER_DEBUG=1 TF_ACC=1 go test -run=TestAcc$(NAME) -count=1 -parallel=$(shell nproc) -timeout 30m -coverpkg=./corefuncprovider/... -coverprofile=__coverage.out -v ./corefuncprovider/...
else ifeq ($(TOFU), true)
TF_ACC_PROVIDER_NAMESPACE=northwood-labs TF_ACC_PROVIDER_HOST=registry.opentofu.org TF_ACC_TERRAFORM_PATH=$(shell which tofu) TF_ACC=1 gotestsum --format testname -- -run=TestAcc$(NAME) -count=1 -parallel=$(shell nproc) -timeout 30m -coverpkg=./corefuncprovider/... -coverprofile=__coverage.out -v ./corefuncprovider/...
else
TF_ACC=1 gotestsum --format testname -- -run=TestAcc$(NAME) -count=1 -parallel=$(shell nproc) -timeout 30m -coverpkg=./corefuncprovider/... -coverprofile=__coverage.out -v ./corefuncprovider/...
endif
@ go-cover-treemap -coverprofile __coverage.out > acc-coverage.svg
@ rsvg-convert --width=2000 --format=png --output="acc-coverage.png" "acc-coverage.svg"
.PHONY: unit
## unit: [test] Runs unit tests. Set NAME= (without 'Test') to run a specific test by name.
unit:
@ $(HEADER) "=====> Running unit tests..."
gotestsum --format testname -- -run=Test$(NAME) -count=1 -parallel=$(shell nproc) -timeout 30s -coverpkg=./corefunc/... -coverprofile=__coverage.out -v ./corefunc/...
@ go-cover-treemap -coverprofile __coverage.out > unit-coverage.svg
@ rsvg-convert --width=2000 --format=png --output="unit-coverage.png" "unit-coverage.svg"
.PHONY: mutate
## mutate: [test] Runs mutation tests.
mutate:
@ $(HEADER) "=====> Running mutation tests..."
cd ./corefunc && $(GO) test -tags=mutation -count=1 -parallel=$(shell nproc) -timeout 30s -ooze.v=true | ggrep -v "^[[:lower:]]" | ggrep -v "^)"
.PHONY: terratest
## terratest: [test] Runs Terratest tests.
terratest:
@ $(HEADER) "=====> Running Terratest tests..."
cd ./terratest/data-sources && $(GO) test -count 1
cd ./terratest/functions && $(GO) test -count 1
.PHONY: examples
## examples: [test] Runs tests for examples. Set NAME= (without 'Example') to run a specific test by name.
examples:
@ $(HEADER) "=====> Running tests for examples..."
gotestsum --format testname -- -run=Example$(NAME) -count=1 -parallel=$(shell nproc) -timeout 30s -coverpkg=./corefunc/... -coverprofile=__coverage.out -v ./corefunc/...
.PHONY: fuzz
## fuzz: [test]* Runs the fuzzer for 1 minute per test.
fuzz:
@ $(HEADER) "=====> Running the fuzzer (https://go.dev/doc/tutorial/fuzz)..."
$(GO) test -list=Fuzz ./corefunc/... | grep ^Fuzz | xargs -I% $(GO) test -run='^$$' -fuzz=% -fuzztime 1m -v ./corefunc
.PHONY: quickbench
## quickbench: [test]* Runs the benchmarks with minimal data for a quick check.
quickbench:
@ $(HEADER) "=====> Running "quick" benchmark..."
$(GO) test -bench=. -run=^Benchmark$(NAME) -timeout 60m ./corefunc
.PHONY: bench
## bench: [test]* Runs the benchmarks with enough data for analysis with benchstat.
bench:
@ $(HEADER) "=====> Running "full" benchmark..."
$(GO) test -bench=. -run=^Benchmark$(NAME) -count=6 -timeout 60m -benchmem -cpuprofile=__cpu.out -memprofile=__mem.out -trace=__trace.out ./corefunc | tee __bench-$(shell date --utc "+%Y%m%dT%H%M%SZ").out
.PHONY: pgo
## pgo: [test] Runs the benchmarks with enough data for use with Profile-Guided Optimization.
pgo:
@ $(HEADER) "=====> Running benchmark for PGO data..."
TF_ACC=1 go test -run=^TestAcc -count=6 -timeout 60m -cpuprofile=default.pgo -parallel=$(shell nproc) ./corefuncprovider/...
.PHONY: view-cov-cli
## view-cov-cli: [test] After running test or unittest, this will view the coverage report on the CLI.
view-cov-cli:
@ $(HEADER) "=====> Viewing code coverage on the CLI..."
gocovsh --profile=__coverage.out
.PHONY: view-cov-html
## view-cov-html: [test] After running test or unittest, this will launch a browser to view the coverage report.
view-cov-html:
@ $(HEADER) "=====> Viewing code coverage as HTML..."
$(GO) tool cover -html=__coverage.out
.PHONY: view-cpupprof
## view-cpupprof: [test] After running bench, this will launch a browser to view the CPU profiler results.
view-cpupprof:
@ $(HEADER) "=====> Viewing CPU profiling in the browser..."
$(GO) tool pprof -http :8080 __cpu.out
.PHONY: view-mempprof
## view-mempprof: [test] After running bench, this will launch a browser to view the memory profiler results.
view-mempprof:
@ $(HEADER) "=====> Viewing memory profiling in the browser..."
$(GO) tool pprof -http :8080 __mem.out
.PHONY: view-trace
## view-trace: [test] After running bench, this will launch a browser to view the trace results.
view-trace:
@ $(HEADER) "=====> Viewing trace profiling in the browser..."
$(GO) tool trace __trace.out
#-------------------------------------------------------------------------------
# Git Tasks
.PHONY: changelog
## changelog: [release]* Generates the CHANGELOG for the release.
changelog:
@ $(HEADER) "=====> Updating the CHANGELOG..."
git cliff -o CHANGELOG.md
.PHONY: tag
## tag: [release]* Signs and tags the release.
tag:
@ $(HEADER) "=====> Signing and tagging the release..."
@ if [ $$(git status -s -uall | wc -l) != 1 ]; then $(ERROR) "Git workspace must be clean."; exit 1; fi;
@ $(WHITE) "This release will be tagged as: $(NEXT_VERSION)"
@ echo "---------------------------------------------------------------------"
@ read -p "Press any key to continue, or press Control+C to cancel. " x;
@ echo " "
@ chag update $(NEXT_VERSION)
@ echo " "
@ $(HEADER) "These are the contents of the CHANGELOG for this release. Are these correct?"
@ echo "---------------------------------------------------------------------"
@ chag contents
@ echo "---------------------------------------------------------------------"
@ echo "Are these release notes correct? If not, cancel and update CHANGELOG.md."
@ read -p "Press any key to continue, or press Control+C to cancel. " x;
@ echo " "
git add .
git commit -a -m "relprep: Preparing the $(NEXT_VERSION) release." --no-verify
chag tag --sign