/
Makefile
142 lines (118 loc) · 4.59 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
SOURCES := $(wildcard *.go cmd/*/*.go pkg/*/*/*.go)
# ==================================================================================== #
# HELPERS
# ==================================================================================== #
## help: print this help message
.PHONY: help
help:
@echo 'Usage:'
@sed -n 's/^##//p' ${MAKEFILE_LIST} | column -t -s ':' | sed -e 's/^/ /'
.PHONY: confirm
confirm:
@echo -n 'Are you sure? [y/N] ' && read ans && [ $${ans:-N} = y ]
# ==================================================================================== #
# DEVELOPMENT
# ==================================================================================== #
## startdb: run a postgres db container
.PHONY: startdb
startdb:
docker run \
--name greenlight_db \
-p 5432:5432 \
-v greenlight_db-data:/var/lib/postgresql/data \
-e POSTGRES_USER=postgres \
-e POSTGRES_PASSWORD=654321 \
-e POSTGRES_DB=greenlight \
-d postgres:15-alpine
## createdb: create a database with the username postgres, owner postgres and database name greenlight in the greenlight_db container
.PHONY: createdb
createdb:
docker exec -it greenlight_db createdb --username=postgres --owner=postgres greenlight
## dropdb: drop a database with the username postgres, owner postgres and database name greenlight in the greenlight_db container
.PHONY: dropdb
dropdb:
docker exec -it greenlight_db dropdb greenlight
## migrateup: apply all up database migrations
.PHONY: migrateup
migrateup: confirm
@echo 'Running up migrations...'
migrate -path=./migrations -database=${GREENLIGHT_DB_DSN} -verbose up
## migratedown: apply all down database migrations
.PHONY: migratedown
migratedown:
@echo 'Running down migrations...'
migrate -path=./migrations -database=${GREENLIGHT_DB_DSN} -verbose down
## run: run the cmd/api application
.PHONY: run
run:
@echo 'starting greenlight application'
@go run ./cmd/api
## docker/compose/up: start containers in greenlight.yaml file
.PHONY: docker/compose/up
docker/compose/up:
@echo 'starting greenlight containers'
docker-compose -f greenlight.yaml up -d
## docker/compose/down: stop and remove all running containers in greenlight.yaml file
.PHONY: docker/compose/down
docker/compose/down:
@echo 'stop and remove greenlight containers'
docker-compose -f greenlight.yaml down
## migration name=$1: create a new database migration file
.PHONY: migration
migration:
@echo 'Creating migration files for ${name}...'
migrate create -seq -ext=.sql -dir=./migrations ${name}
## tests: runs test code coverage
.PHONY: tests
tests:
@echo 'running tests cover'
go test ./cmd/api -cover
# ==================================================================================== #
# QUALITY CONTROL
# ==================================================================================== #
## audit: tidy and vendor dependencies and format, vet and test all code
.PHONY: audit
audit: vendor
@echo 'Formatting code...'
go fmt ./...
@echo 'Vetting code...'
go vet ./...
staticcheck ./...
@echo 'Running tests...'
go test -race -vet=off ./...
## vendor: tidy and vendor dependencies
.PHONY: vendor
vendor:
@echo 'Tidying and verifying module dependencies...'
go mod tidy
go mod verify
@echo 'Vendoring dependencies...'
go mod vendor
# ==================================================================================== #
# BUILD
# ==================================================================================== #
current_time = $(shell date --iso-8601=seconds)
git_description = $(shell git describe --tags --long --dirty --always 2>/dev/null)
ifeq ($(git_description),)
git_description = UNKNOWN
endif
linker_flags = '-s -X main.buildTime=${current_time} -X main.version=${git_description}'
## build/api: build the cmd/api application
.PHONY: build/api
build/api:
@echo 'Building cmd/api...'
go build -ldflags=${linker_flags} -o=./bin/api ./cmd/api
GOOS=linux GOARCH=amd64 go build -ldflags=${linker_flags} -o=./bin/linux_amd64/api ./cmd/api
# ==================================================================================== #
# Docker
# ==================================================================================== #
## docker: build the docker image
.PHONY: docker
docker: $(SOURCES) Dockerfile
@echo 'Building docker image...'
docker build -t greenlight:latest -f Dockerfile --build-arg VERSION=$(git_description) --build-arg BUILDTIME=$(current_time) .
.PHONY: publish
publish:
docker tag greenlight:latest ifedayoawe/greenlight:latest
docker push ifedayoawe/greenlight:latest
.PHONY: help startdb createdb dropdb migrateup migratedown confirm run docker/compose/up docker/compose/down migration vendor build/api tests