|
| 1 | +MODULE = $(shell go list -m) |
| 2 | +VERSION ?= $(shell git describe --tags --always --dirty --match=v* 2> /dev/null || echo "1.0.0") |
| 3 | +PACKAGES := $(shell go list ./... | grep -v /vendor/) |
| 4 | +LDFLAGS := -ldflags "-X main.Version=${VERSION}" |
| 5 | + |
| 6 | +CONFIG_FILE ?= ./config/local.yml |
| 7 | +APP_DSN ?= $(shell sed -n 's/^dsn:[[:space:]]*"\(.*\)"/\1/p' $(CONFIG_FILE)) |
| 8 | +MIGRATE := docker run -v $(shell pwd)/migrations:/migrations --network host migrate/migrate -path=/migrations/ -database "$(APP_DSN)" |
| 9 | + |
| 10 | +PID_FILE := './.pid' |
| 11 | +FSWATCH_FILE := './fswatch.cfg' |
| 12 | + |
| 13 | +.PHONY: default |
| 14 | +default: help |
| 15 | + |
| 16 | +# generate help info from comments: thanks to https://marmelab.com/blog/2016/02/29/auto-documented-makefile.html |
| 17 | +.PHONY: help |
| 18 | +help: ## help information about make commands |
| 19 | + @grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' |
| 20 | + |
| 21 | +.PHONY: test |
| 22 | +test: ## run unit tests |
| 23 | + @echo "mode: count" > coverage-all.out |
| 24 | + @$(foreach pkg,$(PACKAGES), \ |
| 25 | + go test -p=1 -cover -covermode=count -coverprofile=coverage.out ${pkg}; \ |
| 26 | + tail -n +2 coverage.out >> coverage-all.out;) |
| 27 | + |
| 28 | +.PHONY: test-cover |
| 29 | +test-cover: test ## run unit tests and show test coverage information |
| 30 | + go tool cover -html=coverage-all.out |
| 31 | + |
| 32 | +.PHONY: run |
| 33 | +run: ## run the API server |
| 34 | + go run ${LDFLAGS} cmd/server/main.go & echo $$! > $(PID_FILE) |
| 35 | + |
| 36 | +.PHONY: run-stop |
| 37 | +run-stop: ## stop the API server |
| 38 | + @pkill -P `cat $(PID_FILE)` || true |
| 39 | + |
| 40 | +.PHONY: run-restart |
| 41 | +run-restart: ## restart the API server |
| 42 | + @make run-stop |
| 43 | + @printf '%*s\n' "80" '' | tr ' ' - |
| 44 | + @echo "Source file changed. Restarting server..." |
| 45 | + @make run |
| 46 | + @printf '%*s\n' "80" '' | tr ' ' - |
| 47 | + |
| 48 | +run-live: run ## run the API server with live reload support (requires fswatch) |
| 49 | + @fswatch -x -o --event Created --event Updated --event Renamed -r internal pkg cmd config | xargs -n1 -I {} make run-restart |
| 50 | + |
| 51 | +.PHONY: build |
| 52 | +build: ## build the API server binary |
| 53 | + CGO_ENABLED=0 go build ${LDFLAGS} -a -o server $(MODULE)/cmd/server |
| 54 | + |
| 55 | +.PHONY: build-docker |
| 56 | +build-docker: ## build the API server as a docker image |
| 57 | + docker build -f cmd/server/Dockerfile -t server . |
| 58 | + |
| 59 | +.PHONY: clean |
| 60 | +clean: ## remove temporary files |
| 61 | + rm -rf server coverage.out coverage-all.out |
| 62 | + |
| 63 | +.PHONY: version |
| 64 | +version: ## display the version of the API server |
| 65 | + @echo $(VERSION) |
| 66 | + |
| 67 | +.PHONY: db-start |
| 68 | +db-start: ## start the database server |
| 69 | + @mkdir -p testdata/postgres |
| 70 | + docker run --rm --name postgres -v $(shell pwd)/testdata:/testdata \ |
| 71 | + -v $(shell pwd)/testdata/postgres:/var/lib/postgresql/data \ |
| 72 | + -e POSTGRES_PASSWORD=postgres -e POSTGRES_DB=go_restful -d -p 5432:5432 postgres |
| 73 | + |
| 74 | +.PHONY: db-stop |
| 75 | +db-stop: ## stop the database server |
| 76 | + docker stop postgres |
| 77 | + |
| 78 | +.PHONY: testdata |
| 79 | +testdata: ## populate the database with test data |
| 80 | + make migrate-reset |
| 81 | + @echo "Populating test data..." |
| 82 | + @docker exec -it postgres psql "$(APP_DSN)" -f /testdata/testdata.sql |
| 83 | + |
| 84 | +.PHONY: lint |
| 85 | +lint: ## run golint on all Go package |
| 86 | + @golint $(PACKAGES) |
| 87 | + |
| 88 | +.PHONY: fmt |
| 89 | +fmt: ## run "go fmt" on all Go packages |
| 90 | + @go fmt $(PACKAGES) |
| 91 | + |
| 92 | +.PHONY: migrate |
| 93 | +migrate: ## run all new database migrations |
| 94 | + @echo "Running all new database migrations..." |
| 95 | + @$(MIGRATE) up |
| 96 | + |
| 97 | +.PHONY: migrate-down |
| 98 | +migrate-down: ## revert database to the last migration step |
| 99 | + @echo "Reverting database to the last migration step..." |
| 100 | + @$(MIGRATE) down 1 |
| 101 | + |
| 102 | +.PHONY: migrate-new |
| 103 | +migrate-new: ## create a new database migration |
| 104 | + @read -p "Enter the name of the new migration: " name; \ |
| 105 | + $(MIGRATE) create -ext sql -dir /migrations/ $${name// /_} |
| 106 | + |
| 107 | +.PHONY: migrate-reset |
| 108 | +migrate-reset: ## reset database and re-run all migrations |
| 109 | + @echo "Resetting database..." |
| 110 | + @$(MIGRATE) drop |
| 111 | + @echo "Running all database migrations..." |
| 112 | + @$(MIGRATE) up |
0 commit comments