github.com/shoshinnikita/budget-manager@v0.7.1-0.20220131195411-8c46ff1c6778/Makefile (about) 1 include .env 2 export 3 4 SHELL := /bin/bash 5 6 # Make all targets phony. Get list of all targets: 'cat Makefile | grep -P -o "^[\w-]+:" | rev | cut -c 2- | rev | sort | uniq' 7 .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 8 9 default: build run 10 11 # build builds a binary file 12 build: export-ldflags 13 @ echo "Build Budget Manager..." 14 @ CGO_ENABLED=1 go build -ldflags "${LDFLAGS}" -mod=vendor -o bin/budget-manager cmd/budget-manager/main.go 15 16 # run runs built Budget Manager 17 run: 18 @ echo "Run Budget Manager..." 19 @ ./bin/budget-manager 20 21 # 22 # Docker 23 # 24 25 docker: docker-build docker-run 26 27 # docker-build builds a Docker image 28 docker-build: TAG?=budget-manager:latest 29 docker-build: export-ldflags 30 @ echo "Build Docker image for Budget Manager..." 31 @ docker build -t ${TAG} --build-arg LDFLAGS="${LDFLAGS}" . 32 33 # docker-run runs both Budget Manager and PostgreSQL in containers 34 docker-run: 35 @ echo "Run Budget Manager in Docker container..." 36 @ docker-compose up --exit-code-from budget-manager 37 38 # docker-clear downs containers and removes volumes 39 docker-clear: 40 @ docker-compose down -v || true 41 42 # 43 # Tests 44 # 45 46 test: test-integ 47 48 TEST_CMD=CGO_ENABLED=1 go test -v -mod=vendor ${TEST_FLAGS} \ 49 -cover -coverprofile=cover.out -coverpkg=github.com/ShoshinNikita/budget-manager/...\ 50 ./cmd/... ./internal/... ./tests/... && \ 51 sed -i '/github.com\/ShoshinNikita\/budget-manager\/tests\//d' cover.out && \ 52 go tool cover -func=cover.out && rm cover.out 53 54 # test-unit runs unit tests 55 test-unit: TEST_FLAGS=-short 56 test-unit: 57 @ echo "Run unit tests..." 58 ${TEST_CMD} 59 60 # test-integ runs both unit and integration tests 61 # 62 # Disable parallel tests for packages (with '-p 1') to avoid DB errors. 63 # Same situation: https://medium.com/@xcoulon/how-to-avoid-parallel-execution-of-tests-in-golang-763d32d88eec) 64 # 65 test-integ: TEST_FLAGS=-p=1 66 test-integ: 67 @ echo "Run integration tests..." 68 ${TEST_CMD} 69 70 # 71 # PostgreSQL 72 # 73 74 PG_ENV=-e POSTGRES_USER=postgres -e POSTGRES_DB=postgres -e POSTGRES_HOST_AUTH_METHOD=trust 75 PG_CONAINER_NAME=budget-manager_pg 76 77 # run-pg runs develop PostgreSQL instance with mounted '_var/pg_data' directory 78 run-pg: stop-pg 79 @ echo "Run develop PostgreSQL instance..." 80 @ docker run --rm -d \ 81 --name ${PG_CONAINER_NAME} \ 82 -p "5432:5432" \ 83 -v $(shell pwd)/_var/pg_data:/var/lib/postgresql/data \ 84 ${PG_ENV} \ 85 postgres:12-alpine -c "log_statement=all" 86 87 # stop-pg stops develop PostgreSQL instance 88 stop-pg: 89 @ echo "Stop develop PostgreSQL instance..." 90 @ docker stop ${PG_CONAINER_NAME} > /dev/null 2>&1 || true 91 92 # 93 # Configuration 94 # 95 96 # export-ldflags exports LDFLAGS env variable. It is used during the build process to set version 97 # and git hash. It can be used as a dependency target 98 # 99 # For example, we have target 'build': 100 # 101 # build: export-ldflags 102 # go build -ldflags "${LDFLAGS}" main.go 103 # 104 # We can use it as 'make build VERSION=v1.0.0'. Then, next command will be executed: 105 # 106 # go build -ldflags "-s -w -X 'main.version=v1.0.0' -X 'main.gitHash=some_hash'" main.go 107 # 108 export-ldflags: GIT_HASH=$(shell git log -1 --pretty="format:%h") 109 export-ldflags: VERSION?=unknown 110 export-ldflags: 111 $(eval export LDFLAGS=-s -w -X 'main.version=${VERSION}' -X 'main.gitHash=${GIT_HASH}') 112 @ echo Use this ldflags: ${LDFLAGS} 113 114 # 115 # Other 116 # 117 118 # lint runs golangci-lint - https://github.com/golangci/golangci-lint 119 # 120 # Use go cache to speed up execution: https://github.com/golangci/golangci-lint/issues/1004 121 # 122 lint: 123 @ echo "Run golangci-lint..." 124 @ docker run --rm -it --network=none \ 125 -v $(shell go env GOCACHE):/cache/go \ 126 -e GOCACHE=/cache/go \ 127 -e GOLANGCI_LINT_CACHE=/cache/go \ 128 -v $(shell go env GOPATH)/pkg:/go/pkg \ 129 -v $(shell pwd):/app \ 130 -w /app \ 131 golangci/golangci-lint:v1.42-alpine golangci-lint run --config .golangci.yml 132 133 check: build lint test 134 135 # generate-docs generates Swagger API documentation with swag - https://github.com/swaggo/swag 136 generate-docs: 137 @ echo "Clear Swagger API docs..." 138 @ swag init --generalInfo cmd/budget-manager/main.go --output docs 139 @ echo "Generate Swagger API docs..." 140 @ rm ./docs/swagger.json ./docs/docs.go