forked from ConSol-Monitoring/check_nsc_web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
211 lines (181 loc) · 5.8 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
#!/usr/bin/make -f
MAKE:=make
SHELL:=bash
GOVERSION:=$(shell \
go version | \
awk -F'go| ' '{ split($$5, a, /\./); printf ("%04d%04d", a[1], a[2]); exit; }' \
)
MINGOVERSION:=00010019
MINGOVERSIONSTR:=1.19
BUILD:=$(shell git rev-parse --short HEAD)
REVISION:=$(shell printf "%04d" $$( git rev-list --all --count))
# see https://github.com/go-modules-by-example/index/blob/master/010_tools/README.md
# and https://github.com/golang/go/wiki/Modules#how-can-i-track-tool-dependencies-for-a-module
TOOLSFOLDER=$(shell pwd)/tools
export GOBIN := $(TOOLSFOLDER)
export PATH := $(GOBIN):$(PATH)
VERSION ?= $(shell ./buildtools/get_version)
all: build
CMDS = $(shell cd ./cmd && ls -1)
tools: versioncheck vendor go.work
go mod download
set -e; for DEP in $(shell grep "_ " buildtools/tools.go | awk '{ print $$2 }'); do \
go install $$DEP; \
done
go mod tidy
go mod vendor
updatedeps: versioncheck
$(MAKE) clean
go mod download
set -e; for DEP in $(shell grep "_ " buildtools/tools.go | awk '{ print $$2 }'); do \
go get $$DEP; \
done
go get -u ./...
go get -t -u ./...
go mod tidy
vendor: go.work
go mod download
go mod tidy
go mod vendor
go.work: pkg/*
echo "go $(MINGOVERSIONSTR)" > go.work
go work use . pkg/*
build: vendor go.work
set -e; for CMD in $(CMDS); do \
( cd ./cmd/$$CMD && CGO_ENABLED=0 go build -ldflags "-s -w -X main.Build=$(BUILD) -X main.Revision=$(REVISION)" -o ../../$$CMD ) ; \
done
# run build watch, ex. with tracing: make build-watch -- -vv
build-watch: vendor
ls cmd/*/*.go pkg/*/*.go | entr -sr "$(MAKE) && ./check_nsc_web $(filter-out $@,$(MAKECMDGOALS)) $(shell echo $(filter-out --,$(MAKEFLAGS)) | tac -s " ")"
build-linux-amd64: vendor
set -e; for CMD in $(CMDS); do \
( cd ./cmd/$$CMD && GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -ldflags "-s -w -X main.Build=$(BUILD) -X main.Revision=$(REVISION)" -o ../../$$CMD.linux.amd64 ) ; \
done
build-windows-i386: vendor
set -e; for CMD in $(CMDS); do \
( cd ./cmd/$$CMD && GOOS=windows GOARCH=386 CGO_ENABLED=0 go build -ldflags "-s -w -X main.Build=$(BUILD) -X main.Revision=$(REVISION)" -o ../../$$CMD.windows.i386.exe ) ; \
done
build-windows-amd64: vendor
set -e; for CMD in $(CMDS); do \
( cd ./cmd/$$CMD && GOOS=windows GOARCH=amd64 CGO_ENABLED=0 go build -ldflags "-s -w -X main.Build=$(BUILD) -X main.Revision=$(REVISION)" -o ../../$$CMD.windows.amd64.exe ) ; \
done
build-freebsd-i386: vendor
set -e; for CMD in $(CMDS); do \
( cd ./cmd/$$CMD && GOOS=freebsd GOARCH=386 CGO_ENABLED=0 go build -ldflags "-s -w -X main.Build=$(BUILD) -X main.Revision=$(REVISION)" -o ../../$$CMD.freebsd.i386 ) ; \
done
build-darwin-aarch64: vendor
set -e; for CMD in $(CMDS); do \
( cd ./cmd/$$CMD && GOOS=darwin GOARCH=arm64 CGO_ENABLED=0 go build -ldflags "-s -w -X main.Build=$(BUILD) -X main.Revision=$(REVISION)" -o ../../$$CMD.darwin.aarch64 ) ; \
done
test: vendor
go test -short -v -timeout=1m pkg/*
if grep -rn TODO: ./cmd/ ./pkg/ ; then exit 1; fi
longtest: vendor
go test -v -timeout=1m pkg/*
citest: vendor
#
# Checking gofmt errors
#
if [ $$(gofmt -s -l ./cmd/ ./pkg/ | wc -l) -gt 0 ]; then \
echo "found format errors in these files:"; \
gofmt -s -l ./cmd/ ./pkg/ ; \
exit 1; \
fi
#
# Checking TODO items
#
if grep -rn TODO: ./cmd/ ./pkg/ ; then exit 1; fi
#
# Run other subtests
#
$(MAKE) golangci
-$(MAKE) govulncheck
$(MAKE) fmt
#
# Normal test cases
#
$(MAKE) test
#
# Benchmark tests
#
$(MAKE) benchmark
#
# Race rondition tests
#
$(MAKE) racetest
#
# Test cross compilation
#
$(MAKE) build-linux-amd64
$(MAKE) build-windows-amd64
$(MAKE) build-windows-i386
$(MAKE) build-freebsd-i386
$(MAKE) build-darwin-aarch64
#
# All CI tests successful
#
benchmark:
go test -timeout=1m -ldflags "-s -w -X main.Build=$(BUILD)" -v -bench=B\* -run=^$$ -benchmem ./pkg/*
racetest:
go test -race -timeout=3m -coverprofile=coverage.txt -covermode=atomic ./pkg/*
covertest:
go test -v -coverprofile=cover.out -timeout=1m ./pkg/*
go tool cover -func=cover.out
go tool cover -html=cover.out -o coverage.html
coverweb:
go test -v -coverprofile=cover.out -timeout=1m ./pkg/*
go tool cover -html=cover.out
clean:
set -e; for CMD in $(CMDS); do \
rm -f ./cmd/$$CMD/$$CMD; \
done
rm -f $(CMDS)
rm -f *.windows.*.exe
rm -f *.linux.*
rm -f *.darwin.*
rm -f *.freebsd.*
rm -f cover.out
rm -f coverage.html
rm -f coverage.txt
rm -rf vendor/
rm -rf $(TOOLSFOLDER)
GOVET=go vet -all
fmt: tools
set -e; for CMD in $(CMDS); do \
$(GOVET) ./cmd/$$CMD; \
done
set -e; for dir in $(shell ls -d1 pkg/*); do \
$(GOVET) ./$$dir; \
done
gofmt -w -s ./cmd/ ./pkg/
./tools/gofumpt -w ./cmd/ ./pkg/
./tools/gci write ./cmd/. ./pkg/. --skip-generated
goimports -w ./cmd/ ./pkg/
versioncheck:
@[ $$( printf '%s\n' $(GOVERSION) $(MINGOVERSION) | sort | head -n 1 ) = $(MINGOVERSION) ] || { \
echo "**** ERROR:"; \
echo "**** check_nsc_web requires at least golang version $(MINGOVERSIONSTR) or higher"; \
echo "**** this is: $$(go version)"; \
exit 1; \
}
golangci: tools
#
# golangci combines a few static code analyzer
# See https://github.com/golangci/golangci-lint
#
set -e; for dir in $$(ls -1d pkg/*); do \
echo $$dir; \
( cd $$dir && golangci-lint run ./... ); \
done
govulncheck: tools
govulncheck ./...
version:
OLDVERSION="$(shell grep "VERSION =" ./pkg/checknscweb/check.go | awk '{print $$4}' | tr -d '"')"; \
NEWVERSION=$$(dialog --stdout --inputbox "New Version:" 0 0 "v$$OLDVERSION") && \
NEWVERSION=$$(echo $$NEWVERSION | sed "s/^v//g"); \
if [ "v$$OLDVERSION" = "v$$NEWVERSION" -o "x$$NEWVERSION" = "x" ]; then echo "no changes"; exit 1; fi; \
sed -i -e 's/VERSION =.*/VERSION = "'$$NEWVERSION'"/g' cmd/*/*.go pkg/checknscweb/*.go
check_nsc_web: build
docker:
docker build -t dockerbuilder .
docker run -it --rm -e CGO_ENABLED=1 -v $(shell pwd):/go/src/app dockerbuilder make $(target)