-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMakefile
More file actions
215 lines (179 loc) · 6.37 KB
/
Makefile
File metadata and controls
215 lines (179 loc) · 6.37 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
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
212
213
214
215
# Use bash so we can use `source`.
SHELL := /bin/bash
# Source docker.env if it exists and export all valid keys
ifneq ("$(wildcard docker.env)", "")
include docker.env
export $(shell sed -n 's/^\([A-Za-z_][A-Za-z0-9_]*\)=.*/\1/p' docker.env)
endif
.PHONY: db-up
db-up:
docker compose up --detach --wait db
.PHONY: db-down
db-down:
docker compose down db
.PHONY: gen
gen: db-up
@# sqlc does not remove generated files on its own when the source file is deleted; remove them manually here. (|| true reports success even when no files were found.)
@rm internal/db/* || true
@date
go generate ./...
@echo "Done."
.PHONY: db-docs-gen
db-docs-gen: db-up
@echo "Generating database docs..."
@go run github.com/k1LoW/tbls@v1.91.2 doc --rm-dist
@echo "Done."
.PHONY: build
build: gen
go build .
.PHONY: test
test: gen
go test ./... -skip TestDB
.PHONY: debug
debug: gen
@dlv debug
.PHONY: debug-test-db
debug-test-db: export PGPORT = 5433
debug-test-db:
@# Equivalent to `make db-up` and `make db-down`, but for the ephemeral database
@docker compose down --volumes test-db-ephemeral
@docker compose up --detach --wait test-db-ephemeral
@# Equivalent to `make db-init`, but for the ephemeral database
@PGDATABASE=postgres \
go tool tern migrate --config sql/init/tern.conf --migrations sql/init
@# Equivalent to `make db-migrate`, but for the ephemeral database.
@# Migrate to the latest migration.
@go tool tern migrate --config sql/migrations/tern.conf --migrations sql/migrations
@# Migrate River schema to latest.
@go tool river migrate-up
@echo "Running database tests (TestDB*)..."
@# Edit and use like: dlv test ./internal/dbx -- -test.run TestDBPostUsage
@dlv test ./internal/dbx -- -test.run TestDBPostUsage
.PHONY: watch-setup
watch-setup:
@if [ -z $(shell which entr) ]; then \
echo -en "\033[0;33m"; \
echo "entr not found, attempting to install via \`brew install entr\`..."; \
brew install entr; \
echo -e "\033[0m"; \
fi;
.PHONY: watchgen
watchgen: watch-setup
@echo "Watching for .sql file changes. Press ctrl+c *twice* to exit, or once to rebuild."
@while true; do \
find . -type f -name '*.sql' | entr -d make gen ; \
sleep 0.5 ; \
done
# Run entr in a while loop because it exits when files are deleted.
# Run targets db-up and db-init before running.
.PHONY: watch
watch: watch-setup
@echo "Watching for .go file changes. Press ctrl+c *twice* to exit, or once to rebuild."
@while true; do \
find . -type f -name '*.go' \
| entr -d -r go run . ; \
sleep 0.5 ; \
done
.PHONY: watchtest
watchtest: watch-setup
@echo "Running unit tests every time .go files change. Press ctrl+c *twice* to exit, or once to rebuild."
@while true; do \
sleep 0.5 ; \
find . -type f -name '*.go' | entr -d go test ./... ; \
done
.PHONY: clean
clean: db-down
go clean
.PHONY: db-init
db-init: export PGDATABASE = postgres
db-init:
@# Initialize the database.
@go tool tern migrate --config sql/init/tern.conf --migrations sql/init
.PHONY: db-drop
db-drop: export PGDATABASE = postgres
db-drop:
@# Drop the database.
@go tool tern migrate --config sql/init/tern.conf --migrations sql/init --destination 0
.PHONY: db-migrate
db-migrate: db-init
@# Migrate to the latest migration.
@go tool tern migrate --config sql/migrations/tern.conf --migrations sql/migrations
@# Migrate River schema to latest.
@go tool river migrate-up
.PHONY: db-remigrate
db-remigrate: db-init
@# Redo the latest migration in Tern.
@go tool tern migrate -d -+1 --config sql/migrations/tern.conf --migrations sql/migrations
.PHONY: db-reset
db-reset: db-drop db-init db-migrate
@echo "Database reset. Restart app to reconnect."
.PHONY: psql
psql:
@psql
.PHONY: psql-testdb
psql-testdb: export PGPORT = 5433
psql-testdb:
psql
.PHONY: riverui
riverui:
@riverui
.PHONY: db-schema
db-schema:
@\
pg_dump --schema-only --exclude-table='river*' --exclude-table="schema_version" --no-owner \
| grep --invert-match "\-\-" \
| cat -s \
> sql/schema/generated.sql
.PHONY: test-db
test-db: export PGPORT = 5433
test-db:
@# Equivalent to `make db-up` and `make db-down`, but for the ephemeral database
@docker compose down --volumes test-db-ephemeral
@docker compose up --detach --wait test-db-ephemeral
@# Equivalent to `make db-init`, but for the ephemeral database
@PGDATABASE=postgres \
go tool tern migrate --config sql/init/tern.conf --migrations sql/init
@# Equivalent to `make db-migrate`, but for the ephemeral database.
@# Migrate to the latest migration.
@go tool tern migrate --config sql/migrations/tern.conf --migrations sql/migrations
@# Migrate River schema to latest.
@go tool river migrate-up
@echo "Running database tests (TestDB*)..."
@# Disable caching with -count=1, since go does not cache bust when .sql files change
go test ./... -run TestDB -count=1
# Run from inside a container.
# Ephemeral database must already be up via docker compose.
.PHONY: test-db-ci
test-db-ci:
@# Equivalent to `make db-init`, but for the ephemeral database
@# Intended to be run inside a container.
PGDATABASE=postgres \
go tool tern migrate --config sql/init/tern.conf --migrations sql/init
@# Equivalent to `make db-migrate`, but for the ephemeral database.
@# Migrate to the latest migration.
go tool tern migrate --config sql/migrations/tern.conf --migrations sql/migrations
@# Migrate River schema to latest.
go tool river migrate-up
@echo "Running database tests (TestDB*)..."
@# Disable caching with -count=1, since go does not cache bust when .sql files change
go test ./... -run TestDB -count=1
.PHONY: jwt
jwt:
@if [ -z $(shell which uaac) ]; then \
echo -en "\033[0;33m"; \
echo "cf-uaac not found, attempting to install via \`gem install cf-uaac\`..."; \
gem install cf-uaac; \
echo -e "\033[0m"; \
fi
uaac target $${OIDC_ISSUER%/oauth/token}; \
uaac token client get $$CF_CLIENT_ID -s $$CF_CLIENT_SECRET --scope "usage.admin"; \
uaac context billing | grep access_token | awk '{print $$2}' > jwt.txt
@# JWTs are encoded with base64url, which `base64 -d` cannot directly parse.
@# Steps:
@# - Split on '.' and get the middle (claims) section of the token
@# - Replace URL-safe chars with base64 equivalents
@# - Add padding as needed
@# - Decode base64url to unicode
@# - Pretty print with fromjson
@echo "Token saved to jwt.txt. Expires at:"
@cat jwt.txt | jq -Rr 'split(".")[1] | gsub("-"; "+") | gsub("_"; "/") | . + (["","===","==","="][length % 4]) | @base64d | fromjson | .exp | todate'