-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
112 lines (90 loc) · 3.79 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
MODULE = $(shell go list -m)
VERSION ?= $(shell git describe --tags --always --dirty --match=v* 2> /dev/null || echo "1.0.0")
PACKAGES := $(shell go list ./... | grep -v /vendor/)
LDFLAGS := -ldflags "-X main.Version=${VERSION}"
CONFIG_FILE ?= ./config/local.yml
APP_DSN ?= $(shell sed -n 's/^dsn:[[:space:]]*"\(.*\)"/\1/p' $(CONFIG_FILE))
MIGRATE := docker run -v $(shell pwd)/migrations:/migrations --network host migrate/migrate:v4.10.0 -path=/migrations/ -database "$(APP_DSN)"
PID_FILE := './.pid'
FSWATCH_FILE := './fswatch.cfg'
.PHONY: default
default: help
# generate help info from comments: thanks to https://marmelab.com/blog/2016/02/29/auto-documented-makefile.html
.PHONY: help
help: ## help information about make commands
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
.PHONY: test
test: ## run unit tests
@echo "mode: count" > coverage-all.out
@$(foreach pkg,$(PACKAGES), \
go test -p=1 -cover -covermode=count -coverprofile=coverage.out ${pkg}; \
tail -n +2 coverage.out >> coverage-all.out;)
.PHONY: test-cover
test-cover: test ## run unit tests and show test coverage information
go tool cover -html=coverage-all.out
.PHONY: run
run: ## run the API server
go run ${LDFLAGS} cmd/server/main.go
.PHONY: run-restart
run-restart: ## restart the API server
@pkill -P `cat $(PID_FILE)` || true
@printf '%*s\n' "80" '' | tr ' ' -
@echo "Source file changed. Restarting server..."
@go run ${LDFLAGS} cmd/server/main.go & echo $$! > $(PID_FILE)
@printf '%*s\n' "80" '' | tr ' ' -
run-live: ## run the API server with live reload support (requires fswatch)
@go run ${LDFLAGS} cmd/server/main.go & echo $$! > $(PID_FILE)
@fswatch -x -o --event Created --event Updated --event Renamed -r internal pkg cmd config | xargs -n1 -I {} make run-restart
.PHONY: build
build: ## build the API server binary
CGO_ENABLED=0 go build ${LDFLAGS} -a -o server $(MODULE)/cmd/server
.PHONY: build-docker
build-docker: ## build the API server as a docker image
docker build -f cmd/server/Dockerfile -t server .
.PHONY: clean
clean: ## remove temporary files
rm -rf server coverage.out coverage-all.out
.PHONY: version
version: ## display the version of the API server
@echo $(VERSION)
.PHONY: db-start
db-start: ## start the database server
@mkdir -p testdata/postgres
@mkdir -p /tmp/postgres/data
docker run --rm --name postgres -v $(shell pwd)/testdata:/testdata \
-v $(shell pwd)/testdata/postgres:/tmp/postgresql/data \
-e POSTGRES_PASSWORD=postgres -e POSTGRES_DB=go_restful -d -p 5433:5432 postgres
.PHONY: db-stop
db-stop: ## stop the database server
docker stop postgres
.PHONY: testdata
testdata: ## populate the database with test data
@echo "APP_DSN = $(APP_DSN)"
make migrate-reset
@echo "Populating test data..."
# @docker exec -it postgres psql "$(APP_DSN)" -f /testdata/testdata.sql
@docker exec -it postgres psql "postgres://127.0.0.1:5432/go_restful?sslmode=disable&user=postgres&password=postgres" -f /testdata/testdata.sql
.PHONY: lint
lint: ## run golint on all Go package
@golint $(PACKAGES)
.PHONY: fmt
fmt: ## run "go fmt" on all Go packages
@go fmt $(PACKAGES)
.PHONY: migrate
migrate: ## run all new database migrations
@echo "Running all new database migrations..."
@$(MIGRATE) up
.PHONY: migrate-down
migrate-down: ## revert database to the last migration step
@echo "Reverting database to the last migration step..."
@$(MIGRATE) down 1
.PHONY: migrate-new
migrate-new: ## create a new database migration
@read -p "Enter the name of the new migration: " name; \
$(MIGRATE) create -ext sql -dir /migrations/ $${name// /_}
.PHONY: migrate-reset
migrate-reset: ## reset database and re-run all migrations
@echo "Resetting database..."
@$(MIGRATE) drop
@echo "Running all database migrations..."
@$(MIGRATE) up