-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathMakefile
122 lines (111 loc) · 3.48 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
override BUILD_DIR := build
override DOCKER_TAG := ziyan/gatewaysshd:latest
override GOFMTARGS := $(shell find . -mindepth 1 -maxdepth 1 -type d -not \( -path ./.git -or -path ./vendor -or -path ./build \)) $(shell find . -mindepth 1 -maxdepth 1 -type f -iname '*.go')
.PHONY: all
all: format build test
# format code
.PHONY: format
format:
gofmt -l -w ${GOFMTARGS}
# check that code has been formatted
.PHONY: check
check:
@if [ ! -z "$$(gofmt -l -e -d ${GOFMTARGS})" ]; then \
gofmt -l -e -d ${GOFMTARGS}; \
echo "ERROR: Please format your code before you commit and push!" >&2; \
exit 1; \
fi
# compile
.PHONY: generate
generate:
@CGO_ENABLED=0 go generate -mod=vendor ./...
.PHONY: build
build: gatewaysshd
gatewaysshd: $(shell find . -iname '*.go') generate
mkdir -p ${BUILD_DIR}
CGO_ENABLED=0 go build -mod=vendor -o ${BUILD_DIR}/gatewaysshd -ldflags="-X main.Commit=$$(git describe --match=NeVeRmAtCh --always --abbrev=40 --dirty)" .
objcopy --strip-all ${BUILD_DIR}/gatewaysshd
# run lint
.PHONY: lint
lint:
@set -e; \
if ! hash golangci-lint >/dev/null 2>&1; then \
go install github.com/golangci/golangci-lint/cmd/[email protected]; \
fi; \
golangci-lint run
# run tests
.PHONY: test
test: generate
@set -e; \
if ! hash gotestsum >/dev/null 2>&1; then \
go install gotest.tools/[email protected]; \
fi; \
if hash docker >/dev/null 2>&1; then \
CONTAINER="$$(docker run \
-d \
--rm \
-e POSTGRES_DB=gatewaysshd \
-e POSTGRES_USER=gatewaysshd \
-e POSTGRES_PASSWORD=gatewaysshd \
-e POSTGRES_HOST_AUTH_METHOD=trust \
postgres)"; \
trap "docker kill $${CONTAINER} >/dev/null 2>&1" EXIT; \
until docker exec $${CONTAINER} pg_isready >/dev/null 2>&1; do \
sleep 1; \
done; \
IP="$$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' $${CONTAINER})"; \
export GATEWAYSSHD_TEST_DATABASE_HOST="$${IP}"; \
fi; \
gotestsum --format testname -- -mod=vendor -cover -coverprofile=${BUILD_DIR}/coverage.out ./...; \
go tool cover -html=${BUILD_DIR}/coverage.out -o ${BUILD_DIR}/coverage.html; \
go tool cover -func=${BUILD_DIR}/coverage.out
# benchmark tests
.PHONY: benchmark
benchmark:
@go test -mod=vendor -bench=. ./...
# watch for code change and compile
.PHONY: watch
watch:
@set -e; \
while true; do \
inotifywait --quiet --recursive --event modify --event delete --event move .; \
$(MAKE) format lint build || true; \
done
# make docker image
.PHONY: docker
docker: build
@mkdir -p ${BUILD_DIR}
@cp Dockerfile ${BUILD_DIR}
@docker build -t ${DOCKER_TAG} ${BUILD_DIR}
# generate static documents
.PHONY: docs
docs:
@set -e; \
rm -rf ${BUILD_DIR}/docs; \
mkdir -p ${BUILD_DIR}/docs; \
SERVERPKG="$$(grep ^module go.mod | awk '{print $$2}')"; \
SERVERPORT="$$(shuf -i 62000-65000 -n 1)"; \
SERVERURL="http://127.0.0.1:$${SERVERPORT}"; \
godoc -v -http=127.0.0.1:$${SERVERPORT}& SERVERPID="$$!"; \
trap "kill $${SERVERPID}" EXIT; \
while ! wget --quiet --output-document=/dev/null $${SERVERURL}/pkg/$${SERVERPKG}/; do \
sleep 0.1; \
done; \
cd ${BUILD_DIR}/docs; \
wget \
--execute robots=off \
--mirror \
--quiet \
--no-host-directories \
--no-use-server-timestamps \
--no-parent \
--convert-links \
--adjust-extension \
--page-requisites \
--span-hosts \
--domains 127.0.0.1 \
--exclude-directories src/$${SERVERPKG}/${BUILD_DIR}/,src/$${SERVERPKG}/vendor/ \
$${SERVERURL}/lib/godoc/ \
$${SERVERURL}/pkg/$${SERVERPKG}/ \
$${SERVERURL}/src/$${SERVERPKG}/ || true; \
find -type f -exec sed -i -- "s,$${SERVERURL},,g" {} \;