-
Notifications
You must be signed in to change notification settings - Fork 55
/
make
executable file
·210 lines (175 loc) · 5.44 KB
/
make
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
#!/bin/bash
#
# run ./make for help
#
##############################################################################
# helpers
##############################################################################
assert_clean_repo() {
if [[ -n $(git status --porcelain) ]]; then
echo "⛔ Repository has uncommitted changes. Please commit or stash them before running this command."
exit 1
fi
}
confirm() {
read -p "🚨 $1 Are you sure? [y/N] " ans
[[ ${ans:-N} == "y" ]]
}
##############################################################################
# Q&A commands
##############################################################################
## [qa]: Q&A commands
## qa: run all quality control checks
qa() {
qa_mod
qa_fmt
qa_vet
qa_lint
qa_staticcheck
qa_vulncheck
qa_gosec
}
## qa/mod: run go mod tidy and go mod verify
qa_mod() {
echo "✔️ Running go mod tidy and go mod verify..."
go mod tidy -v
go mod verify
}
## qa/fmt: run gofmt to test if all files are formatted
qa_fmt() {
echo "✔️ Running gofmt..."
test -z "$(gofmt -l .)"
}
## qa/vet: run go vet
qa_vet() {
echo "✔️ Running go vet..."
go vet -stdmethods=false $(go list ./...)
}
## qa/staticcheck: run staticcheck
qa_staticcheck() {
echo "✔️ Running staticcheck..."
go run honnef.co/go/tools/cmd/staticcheck@latest -checks=all,-ST1000,-U1000 ./...
}
## qa/vulncheck: run govulncheck
qa_vulncheck() {
echo "✔️ Running govulncheck..."
go run golang.org/x/vuln/cmd/govulncheck@latest ./...
}
## qa/gosec: run gosec
qa_gosec() {
echo "✔️ Running gosec..."
go run github.com/securego/gosec/v2/cmd/gosec@latest -exclude-dir=examples ./...
}
## qa/lint: run golangci-lint
qa_lint() {
echo "✔️ Running golangci-lint..."
go run github.com/golangci/golangci-lint/cmd/[email protected] run
}
## qa/test: run all tests
qa_test() {
echo "✔️ Running go test..."
go test -race -buildvcs .
}
## qa/test/cover: run all tests and create a coverage report
qa_test_cover() {
echo "✔️ Running go test with coverage..."
go test -race -buildvcs -coverprofile=art.coverage.prof .
go tool cover -html=art.coverage.prof -o art.coverage.html
}
## qa/test/update: update golden files
qa_test_update() {
echo "✔️ Running test golden files update..."
go test -v -update-golden .
}
## qa/benchmarks: run all benchmarks
qa_benchmarks() {
echo "✔️ Running go test with benchmarks..."
go test -benchmem -bench=.
}
##############################################################################
# development commands
##############################################################################
## [go]: go commands
## go/tidy: tidy modfiles and format .go files
go_tidy() {
echo "✔️ Running go mod tidy..."
go mod tidy -v
}
## go/fmt: format .go files
go_fmt() {
echo "✔️ Running go fmt..."
go fmt ./...
}
## go/build/asm: build the package and show the assembly output
go_build_asm() {
echo "✔️ Running go build asm..."
go build -a -work -v -gcflags="-S -B -C" .
}
##############################################################################
# operation commands
##############################################################################
## [git]: git commands
## git/push: push changes to the github repository
git_push() {
echo "✔️ Running git push..."
confirm "Pushing changes to the github repository." || exit 1
qa
assert_clean_repo
# git push
}
## git/release: release a new version
git_release() {
echo "✔️ Running git release..."
confirm "Releasing a new version." || exit 1
qa
assert_clean_repo
# Include additional deployment steps here...
}
help() {
# Define a fixed width for alignment
local column_width=15
printf "Usage:\n"
printf " $0 <command>\n"
printf "\nCommands:\n"
printf "\n %-${column_width}s%s\n" "help" "show this help message"
current_group=""
while IFS= read -r line; do
# Detect group lines and capture the description
if [[ "$line" =~ ^##\ \[([^]]+)\]:\ (.*) ]]; then
group="[${BASH_REMATCH[1]}]"
description="${BASH_REMATCH[2]}"
if [[ "$group" != "$current_group" ]]; then
printf "\n%-${column_width}s %s\n" "$group" "$description"
current_group="$group"
fi
# Detect command lines and indent them if within a group
elif [[ "$line" =~ ^##\ ([^:]+):\ (.*) ]]; then
command="${BASH_REMATCH[1]}"
description="${BASH_REMATCH[2]}"
if [[ -n "$current_group" ]]; then
printf " %-${column_width}s%s\n" "$command" "$description"
else
printf "%-${column_width}s%s\n" "$command" "$description"
fi
fi
done < "$0"
}
##############################################################################
# main entrypoint
##############################################################################
# Transform input to match function naming convention
# ie: "git/push" -> "git_push"
cmd="${1//\//_}"
# Check if the function exists and execute it, otherwise show help
if declare -f "$cmd" > /dev/null; then
shift # remove the first argument
"$cmd" "$@" # call the function with any additional arguments
else
if [[ -z "$1" ]]; then
help
exit 0
fi
echo "ERROR: unknown command: '$1'"
help
exit 1
fi