-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
185 lines (158 loc) · 5.72 KB
/
Copy pathMakefile
File metadata and controls
185 lines (158 loc) · 5.72 KB
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
-include .env
# ==================================================================================== #
# HELPERS
# ==================================================================================== #
## help: print this help message
.PHONY: help
help:
@echo 'Usage:'
@sed -n 's/^##//p' ${MAKEFILE_LIST} | column -t -s ':' | sed -e 's/^/ /'
# ==================================================================================== #
# DEVELOPMENT
# ==================================================================================== #
## run: builds the binary and runs the program
.PHONY: run
run: build
@./bin/api
## build: builds the binary
.PHONY: build
build:
@echo 'Building cmd/api...'
GIT_COMMIT=$(shell git rev-parse HEAD)
GIT_DIRTY=$(shell test -n "`git status --porcelain`" && echo "+CHANGES" || true)
BUILD_DATE=$(shell TZ=UTC0 git show --quiet --date='format-local:%Y-%m-%dT%T%z' --format="%cd")
go build -ldflags="-X github.com/hunttraitor/dialed-in-backend/internal/vcs.revision=${GIT_COMMIT}${GIT_DIRTY} -X github.com/hunttraitor/dialed-in-backend/internal/vcs.time=${BUILD_DATE}" -o=./bin/api ./cmd/api
GOOS=linux GOARCH=amd64 go build -ldflags='-s' -o=./bin/linux/_amd64/api ./cmd/api
## seed: seeds the database
.PHONY: seed
seed:
@docker compose exec -T db psql -U ${POSTGRES_USER} -d ${POSTGRES_DB} -f /tmp/seeds.sql
## db-stats: checks the status of the database to see if you are connected
.PHONY: db-status
db-status:
@docker compose exec -T app goose -dir=db/migrations postgres "$(MIGRATION_URL)" status
## up: runs the up migrations
.PHONY: up
up:
@docker compose exec -T app goose -dir=db/migrations postgres "$(MIGRATION_URL)" up
## down: runs the down migrations
.PHONY: down
down:
@docker compose exec -T app goose -dir=db/migrations postgres "$(MIGRATION_URL)" down
## reset: resets the migrations
.PHONY: reset
reset:
@docker compose exec -T app goose -dir=db/migrations postgres "$(MIGRATION_URL)" reset
## test-all: runs all unit tests sequentially with gotestdox output
.PHONY: test-all
test-all:
docker compose exec app gotestsum \
--format testdox \
--format-icons unicode \
--hide-summary=skipped \
-- -p=1 -count=1 -timeout=180s -buildvcs=false ./...
## test-api: runs all API endpoint tests against a mock database
.PHONY: test-api
test-api:
@docker compose exec -T app go test -v -buildvcs=false ./cmd/api/...
## test-internal: runs all internal business logic such as SQL queries against a test database
.PHONY: test-internal
test-internal:
@docker compose exec -T app go test -v -buildvcs=false ./internal/...
## test-e2e: spins up a real version of the application and runs tests against a test database
.PHONY: test-e2e
test-e2e:
@docker compose exec -T app go test -v -buildvcs=false ./e2e/...
## test-one: run one top-level test or one exact subtest path
##
## examples:
## make test-one TEST="TestPostCoffee2" PKG=./e2e
## make test-one TEST="TestPostCoffee2/coffee origin type too long" PKG=./e2e
##
.PHONY: test-one
test-one:
@test -n "$(TEST)" || (echo 'usage: make test-one TEST="TestName or TestName/subtest words" [PKG=./path]'; exit 1)
@INPUT='$(TEST)'; \
case "$$INPUT" in \
Test*/*) \
PARENT=$$(printf '%s' "$$INPUT" | cut -d/ -f1); \
CHILD=$$(printf '%s' "$$INPUT" | cut -d/ -f2-); \
PARENT_ESCAPED=$$(printf '%s' "$$PARENT" | sed 's/[][(){}.^$$*+?|\\-]/\\&/g'); \
CHILD_REGEX=$$(printf '%s' "$$CHILD" \
| sed 's/[][(){}.^$$*+?|\\-]/\\&/g' \
| tr '[:upper:]' '[:lower:]' \
| sed 's/[[:space:]]\\+/.*/g'); \
RUN_PATTERN="^$$PARENT_ESCAPED$$/(?i)$$CHILD_REGEX"; \
;; \
Test*) \
PARENT_ESCAPED=$$(printf '%s' "$$INPUT" | sed 's/[][(){}.^$$*+?|\\-]/\\&/g'); \
RUN_PATTERN="^$$PARENT_ESCAPED$$"; \
;; \
*) \
echo 'error: use TEST="ParentTest/subtest words" for subtests'; \
exit 1; \
;; \
esac; \
echo "Running pattern: $$RUN_PATTERN"; \
docker compose exec app gotestsum \
--format testdox \
--format-icons unicode \
--hide-summary=skipped \
-- -p=1 -count=1 -timeout=60s -buildvcs=false -run "$$RUN_PATTERN" $(if $(PKG),$(PKG),./...)
## docker-up: runs the docker container
.PHONY: docker-up
docker-up:
docker compose up --build
## docker-down: shuts down the docker container
.PHONY: docker-down
docker-down:
docker compose down
# ==================================================================================== #
# QUALITY CONTROL
# ==================================================================================== #
## tidy: format all .go files
.PHONY: tidy
tidy:
@echo 'Formatting .go files...'
go fmt ./...
@echo 'Tidying module dependencies...'
go mod tidy
go mod verify
go mod vendor
go fix ./...
## audit: run quality control checks
audit:
@echo 'Checking module dependencies...'
go mod tidy -diff
go mod verify
@echo 'Verifying code...'
go vet ./...
go tool staticcheck ./...
@echo 'Running tests...'
make test-all
# ==================================================================================== #
# PRODUCTION
# ==================================================================================== #
production_host_ip = $(PROD_HOST)
## prod/deploy: deploy to production
.PHONY: prod/deploy
prod/deploy:
ssh -o StrictHostKeyChecking=no \
-o ServerAliveInterval=30 \
-o ServerAliveCountMax=10 \
$(PROD_USER)@$(production_host_ip) '\
cd /root/app/Dialed-In-Backend && \
git pull && \
chmod +x scripts/migrate.sh && \
./scripts/migrate.sh && \
docker compose -f production-compose.yml pull && \
docker compose -f production-compose.yml up -d \
'
## prod/connect: connect to the production server
.PHONY: prod/connect
prod/connect:
ssh root@${production_host_ip}
## prod/logs: opens a tunnel to view grafana stats in prod
.PHONY: prod/logs
prod/logs:
ssh -N -L 9999:127.0.0.1:9999 root@${production_host_ip}