-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMakefile
More file actions
124 lines (95 loc) · 3.1 KB
/
Makefile
File metadata and controls
124 lines (95 loc) · 3.1 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
name ?= you-didnt-define-migration-name
GO ?= go
DOCKER ?= docker
DOCKER_BUILDKIT ?= 1
VERSION ?= $(shell git log --pretty=format:%h -n 1)
BUILD_TIME ?= $(shell date)
# -s removes symbol table and -ldflags -w debugging symbols
LDFLAGS ?= -asmflags -trimpath -ldflags \
"-s -w -X 'main.Version=${VERSION}' -X 'main.BuildTime=${BUILD_TIME}'"
GOARCH ?=
GOOS ?=
# CGO_ENABLED=0 == static by default
CGO_ENABLED ?= 0
PSQL_CLIENT ?= psql
PG_DUMP ?= pg_dump
DB_HOST ?= $(shell awk -F '=' '/^DB_HOST/ { print $$NF }' .env)
DB_PORT ?= $(shell awk -F '=' '/^DB_PORT/ { print $$NF }' .env)
DB_NAME ?= $(shell awk -F '=' '/^DB_NAME/ { print $$NF }' .env)
DB_USERNAME ?= $(shell awk -F '=' '/^DB_USERNAME/ { print $$NF }' .env)
DB_PASSWORD ?= $(shell awk -F '=' '/^DB_PASSWORD/ { print $$NF }' .env)
COMPOSE_FILE ?= docker-compose.yml
all: test-unit lint build-all
_build: dist/$(APP_NAME)
build-all:
make -C cmd/dbmigrate
make -C cmd/webserver
dist/$(APP_NAME):
CGO_ENABLED=$(CGO_ENABLED) GOOS=$(GOOS) GOARCH=$(GOARCH) \
$(GO) build $(LDFLAGS) \
-o dist/$(APP_NAME) \
main.go
.PHONY: clean
clean:
rm -rf dist/
install-dependencies:
@go get -v ./...
@go get -tool -modfile=golangci-lint.mod \
github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.1.6
lint:
-rm -rf cmd/dbmigrate/schemas
cp -R sql/schemas cmd/dbmigrate/
go tool -modfile=golangci-lint.mod \
github.com/golangci/golangci-lint/v2/cmd/golangci-lint \
run ./...
vulncheck:
@govulncheck ./...
escape-analysis:
$(GO) build -gcflags="-m" 2>&1
# Easiest way to get proper profiler files:
# make -B LDFLAGS=-cover build-all
launch-profiler:
$(GO) tool pprof -http=: cpu.prof
docker-build:
@DOCKER_BUILDKIT=$(DOCKER_BUILDKIT) $(DOCKER) \
build --rm --target app -t $(APP_NAME)-build .
docker-get-artifact:
-mkdir -p dist/webserver
$(DOCKER) create -ti --name tmp $(APP_NAME)-builder /bin/bash
$(DOCKER) cp tmp:/go/src/app/dist/webserver dist/webserver/main
$(DOCKER) rm -f tmp
build-artifact: docker-build docker-get-artifact
migrate-add:
@echo "Creating a new database migration"
@goose -dir sql/schemas/ create $(name) sql
build-dbmigrate:
make -C cmd/dbmigrate build
migrate-all: build-dbmigrate
@echo "Performing all database migrations"
@./cmd/dbmigrate/dist/dbmigrate
db-dump:
$(PG_DUMP) postgresql://$(DB_USERNAME):$(DB_PASSWORD)@$(DB_HOST):$(DB_PORT)/$(DB_NAME) \
> $(DB_NAME)_dump_$(shell date "+%Y-%m-%d_%H:%M:%S").sql
db-restore:
$(PSQL_CLIENT) postgresql://$(DB_USERNAME):$(DB_PASSWORD)@$(DB_HOST):$(DB_PORT)/$(DB_NAME) \
-q -f $(RESTORE_FILE)
postgresql:
@$(DOCKER) compose -f $(COMPOSE_FILE) up -d
echo "Waiting database to start up..."
@sleep 1
start-db: postgresql migrate-all
stop-db:
@$(DOCKER) compose down
test-coverage:
go test -failfast -race -coverprofile=coverage.out ./...
go tool cover -func=coverage.out
test-unit:
go tool gotest.tools/gotestsum --junitfile=junit.xml -- \
-short -failfast -race -covermode=atomic ./...
# This runs all tests, including integration tests
test-integration: start-db
-go test -failfast -race -tags=integration ./...
docker compose down
.PHONY: sqlc
sqlc:
sqlc generate