This repository has been archived by the owner on Aug 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Makefile
140 lines (115 loc) · 4.08 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
include .env
export
SHELL := /bin/bash
# Make all targets phony. Get list of all targets: 'cat Makefile | grep -P -o "^[\w-]+:" | rev | cut -c 2- | rev | sort | uniq'
.PHONY: build check default docker docker-build docker-clear docker-run export-ldflags generate-docs lint run run-pg run-pg-test stop-pg stop-pg-test test test-integ test-unit
default: build run
# build builds a binary file
build: export-ldflags
@ echo "Build Budget Manager..."
@ CGO_ENABLED=1 go build -ldflags "${LDFLAGS}" -mod=vendor -o bin/budget-manager cmd/budget-manager/main.go
# run runs built Budget Manager
run:
@ echo "Run Budget Manager..."
@ ./bin/budget-manager
#
# Docker
#
docker: docker-build docker-run
# docker-build builds a Docker image
docker-build: TAG?=budget-manager:latest
docker-build: export-ldflags
@ echo "Build Docker image for Budget Manager..."
@ docker build -t ${TAG} --build-arg LDFLAGS="${LDFLAGS}" .
# docker-run runs both Budget Manager and PostgreSQL in containers
docker-run:
@ echo "Run Budget Manager in Docker container..."
@ docker-compose up --exit-code-from budget-manager
# docker-clear downs containers and removes volumes
docker-clear:
@ docker-compose down -v || true
#
# Tests
#
test: test-integ
TEST_CMD=CGO_ENABLED=1 go test -v -mod=vendor ${TEST_FLAGS} \
-cover -coverprofile=cover.out -coverpkg=github.com/ShoshinNikita/budget-manager/...\
./cmd/... ./internal/... ./tests/... && \
sed -i '/github.com\/ShoshinNikita\/budget-manager\/tests\//d' cover.out && \
go tool cover -func=cover.out && rm cover.out
# test-unit runs unit tests
test-unit: TEST_FLAGS=-short
test-unit:
@ echo "Run unit tests..."
${TEST_CMD}
# test-integ runs both unit and integration tests
#
# Disable parallel tests for packages (with '-p 1') to avoid DB errors.
# Same situation: https://medium.com/@xcoulon/how-to-avoid-parallel-execution-of-tests-in-golang-763d32d88eec)
#
test-integ: TEST_FLAGS=-p=1
test-integ:
@ echo "Run integration tests..."
${TEST_CMD}
#
# PostgreSQL
#
PG_ENV=-e POSTGRES_USER=postgres -e POSTGRES_DB=postgres -e POSTGRES_HOST_AUTH_METHOD=trust
PG_CONAINER_NAME=budget-manager_pg
# run-pg runs develop PostgreSQL instance with mounted '_var/pg_data' directory
run-pg: stop-pg
@ echo "Run develop PostgreSQL instance..."
@ docker run --rm -d \
--name ${PG_CONAINER_NAME} \
-p "5432:5432" \
-v $(shell pwd)/_var/pg_data:/var/lib/postgresql/data \
${PG_ENV} \
postgres:12-alpine -c "log_statement=all"
# stop-pg stops develop PostgreSQL instance
stop-pg:
@ echo "Stop develop PostgreSQL instance..."
@ docker stop ${PG_CONAINER_NAME} > /dev/null 2>&1 || true
#
# Configuration
#
# export-ldflags exports LDFLAGS env variable. It is used during the build process to set version
# and git hash. It can be used as a dependency target
#
# For example, we have target 'build':
#
# build: export-ldflags
# go build -ldflags "${LDFLAGS}" main.go
#
# We can use it as 'make build VERSION=v1.0.0'. Then, next command will be executed:
#
# go build -ldflags "-s -w -X 'main.version=v1.0.0' -X 'main.gitHash=some_hash'" main.go
#
export-ldflags: GIT_HASH=$(shell git log -1 --pretty="format:%h")
export-ldflags: VERSION?=unknown
export-ldflags:
$(eval export LDFLAGS=-s -w -X 'main.version=${VERSION}' -X 'main.gitHash=${GIT_HASH}')
@ echo Use this ldflags: ${LDFLAGS}
#
# Other
#
# lint runs golangci-lint - https://github.com/golangci/golangci-lint
#
# Use go cache to speed up execution: https://github.com/golangci/golangci-lint/issues/1004
#
lint:
@ echo "Run golangci-lint..."
@ docker run --rm -it --network=none \
-v $(shell go env GOCACHE):/cache/go \
-e GOCACHE=/cache/go \
-e GOLANGCI_LINT_CACHE=/cache/go \
-v $(shell go env GOPATH)/pkg:/go/pkg \
-v $(shell pwd):/app \
-w /app \
golangci/golangci-lint:v1.42-alpine golangci-lint run --config .golangci.yml
check: build lint test
# generate-docs generates Swagger API documentation with swag - https://github.com/swaggo/swag
generate-docs:
@ echo "Clear Swagger API docs..."
@ swag init --generalInfo cmd/budget-manager/main.go --output docs
@ echo "Generate Swagger API docs..."
@ rm ./docs/swagger.json ./docs/docs.go