-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
123 lines (104 loc) · 2.84 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
# TOOLS VERSIONS
export GO_VERSION=1.21.5
export GOLANGCI_LINT_VERSION=v1.55.2
devimage=newsletter-dev
gopkg=$(devimage)-gopkg
gocache=$(devimage)-gocache
devrun=docker-compose run --rm newsletter
image=perebaj
version=$(shell git rev-parse --short HEAD)
## run all tests. Usage `make test` or `make test testcase="TestFunctionName"` to run an isolated tests
.PHONY: test
test:
if [ -n "$(testcase)" ]; then \
go test ./... -timeout 10s -race -run="^$(testcase)$$" -v; \
else \
go test ./... -timeout 10s -race; \
fi
## Show the tests coverage
.PHONY: coverage
coverage:
go test -coverprofile=c.out
go tool cover -html=c.out
## Run all tests including the integration tests (requires docker up and running). Usage `make integration-test` or `make integration-test testcase="TestFunctionName"` to run an isolated tests
.PHONY: integration-test
integration-test:
if [ -n "$(testcase)" ]; then \
go test ./... -timeout 5s -tags integration -v -run="^$(testcase)$$" ; \
else \
go test ./... -timeout 5s -tags integration; \
fi
## builds the service
.PHONY: service
service:
go build -o ./cmd/newsletter/newsletter ./cmd/newsletter
## runs the service locally
.PHONY: run
run: service
./cmd/newsletter/newsletter
## lint the whole project
.PHONY: lint
lint:
go run github.com/golangci/golangci-lint/cmd/golangci-lint@$(GOLANGCI_LINT_VERSION) run ./...
go run golang.org/x/vuln/cmd/govulncheck@latest ./...
## Build the service image
.PHONY: image
image:
docker build . \
--build-arg GO_VERSION=$(GO_VERSION) \
-t $(image)
## Publish the service image
.PHONY: image/publish
image/publish: image
docker push $(image)
.PHONY: dev
dev: dev/image
$(devrun)
## Create the dev container image
.PHONY: dev/image
dev/image:
docker build \
--build-arg GO_VERSION=$(GO_VERSION) \
--build-arg GOLANGCI_LINT_VERSION=$(GOLANGCI_LINT_VERSION) \
-t $(devimage) \
-f Dockerfile.dev \
.
##run a make target inside the dev container
dev/%: dev/image
$(devrun) make ${*}
## Start containers, additionaly you can provide rebuild=true to force rebuild
.PHONY: dev/start
dev/start:
@echo "Starting development server..."
@if [ "$(rebuild)" = "true" ]; then \
docker-compose up -d --build; \
else \
docker-compose up -d; \
fi
## Dev container logs
.PHONY: dev/logs
dev/logs:
docker-compose logs -f newsletter
## Dev container stop
.PHONY: dev/stop
dev/stop:
docker-compose stop
## Dev container cleanup (remove volumes and images)
.PHONY: dev/cleanup
dev/cleanup:
docker-compose down -v --remove-orphans --rmi all
## Access the container
dev:
@$(devrun) bash
## Display help for all targets
.PHONY: help
help:
@awk '/^.PHONY: / { \
msg = match(lastLine, /^## /); \
if (msg) { \
cmd = substr($$0, 9, 100); \
msg = substr(lastLine, 4, 1000); \
printf " ${GREEN}%-30s${RESET} %s\n", cmd, msg; \
} \
} \
{ lastLine = $$0 }' $(MAKEFILE_LIST)