-
Notifications
You must be signed in to change notification settings - Fork 9
/
Makefile
140 lines (104 loc) · 4.18 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
ROOT_DIR := $(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))
TOOLS_DIR := .tools
# Determine OS and ARCH for some tool versions.
OS := linux
ARCH := amd64
UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Darwin)
OS = darwin
endif
UNAME_P := $(shell uname -p)
ifneq ($(filter arm%,$(UNAME_P)),)
ARCH = arm64
endif
# Tool Versions
COCKROACH_VERSION = v22.2.8
OS_VERSION = $(OS)
ifeq ($(OS),darwin)
OS_VERSION = darwin-10.9
ifeq ($(ARCH),arm64)
OS_VERSION = darwin-11.0
endif
endif
COCKROACH_VERSION_FILE = cockroach-$(COCKROACH_VERSION).$(OS_VERSION)-$(ARCH)
COCKROACH_RELEASE_URL = https://binaries.cockroachdb.com/$(COCKROACH_VERSION_FILE).tgz
GCI_REPO = github.com/daixiang0/gci
GCI_VERSION = v0.13.5
OAPI_CODEGEN_REPO = github.com/deepmap/oapi-codegen/v2
OAPI_CODEGEN_VERSION = v2.1.0
GOLANGCI_LINT_REPO = github.com/golangci/golangci-lint
GOLANGCI_LINT_VERSION = v1.62.2
# go files to be checked
GO_FILES=$(shell git ls-files '*.go')
GOOS=linux
APP_NAME?=identity-api
DEV_DB=identity_api_dev
DEV_URI="postgresql://root@cockroachdb:26257/${DEV_DB}?sslmode=disable"
TEST_PRIVKEY_FILE?=tests/data/privkey.pem
CONFIG_FILE?=identity-api.example.yaml
DOCKER_BUILD_TAG?=latest
# targets
.PHONY: help all generate dev-database test unit-test coverage lint golint build clean vendor up
help: Makefile ## Print help.
@grep -h "##" $(MAKEFILE_LIST) | grep -v grep | sed -e 's/:.*##/#/' | column -c 2 -t -s#
all: lint test ## Runs lint checks and tests.
generate: | $(TOOLS_DIR)/oapi-codegen ## Runs all code generation.
@PATH="$(ROOT_DIR)/$(TOOLS_DIR):$$PATH" \
go generate ./...
dev-database: | vendor $(TOOLS_DIR)/cockroach ## Initializes dev database "${DEV_DB}"
@$(TOOLS_DIR)/cockroach sql -e "drop database if exists ${DEV_DB}"
@$(TOOLS_DIR)/cockroach sql -e "create database ${DEV_DB}"
@IDAPI_CRDB_URI="${DEV_URI}" \
go run main.go migrate up
test: | unit-test ## Run unit tests.
unit-test: | generate $(TOOLS_DIR)/cockroach ## Runs unit tests.
@echo Running unit tests...
@COCKROACH_BINARY="$(ROOT_DIR)/$(TOOLS_DIR)/cockroach" \
go test -race -cover -short ./...
coverage: | $(TOOLS_DIR)/cockroach ## Runs tests generating coverage output.
@echo Generating coverage report...
@export COCKROACH_BINARY="$(ROOT_DIR)/$(TOOLS_DIR)/cockroach" && \
go test ./... -race -coverprofile=coverage.out -covermode=atomic -tags testtools -p 1 && \
go tool cover -func=coverage.out && \
go tool cover -html=coverage.out
lint: golint ## Runs go lint checks.
golint: | vendor $(TOOLS_DIR)/golangci-lint ## Runs go lint checks.
@echo Linting Go files...
@$(TOOLS_DIR)/golangci-lint run --build-tags "-tags testtools"
build: vendor generate ## Builds a binary stored at bin/${APP_NAME}
@echo Building image...
@CGO_ENABLED=0 go build -buildvcs=false -mod=readonly -v -o bin/${APP_NAME}
docker: build ## Builds a docker image tagged with $(APP_NAME):$(DOCKER_BUILD_TAG)
@echo Building docker image...
@docker build --file Dockerfile -t $(APP_NAME):$(DOCKER_BUILD_TAG) bin/
clean: ## Cleans up generated files.
@echo Cleaning...
@rm -f app
@rm -rf ./dist/
@rm -rf coverage.out
@rm -rf .tools/
@go clean -testcache
vendor: ## Downloads and tidies go modules.
@go mod download
@go mod tidy
up: build $(TEST_PRIVKEY_FILE) ## Builds and runs the service.
bin/${APP_NAME} serve --config ${CONFIG_FILE}
$(TEST_PRIVKEY_FILE):
openssl genpkey -out $(TEST_PRIVKEY_FILE) -algorithm RSA -pkeyopt rsa_keygen_bits:4096
# Tools setup
$(TOOLS_DIR):
mkdir -p $(TOOLS_DIR)
$(TOOLS_DIR)/cockroach: | $(TOOLS_DIR)
@echo "Downloading cockroach: $(COCKROACH_RELEASE_URL)"
@curl --silent --fail "$(COCKROACH_RELEASE_URL)" \
| tar -xz --strip-components 1 -C $(TOOLS_DIR) $(COCKROACH_VERSION_FILE)/cockroach
$@ version
$(TOOLS_DIR)/golangci-lint: | $(TOOLS_DIR)
@echo "Installing $(GOLANGCI_LINT_REPO)/cmd/golangci-lint@$(GOLANGCI_LINT_VERSION)"
@GOBIN=$(ROOT_DIR)/$(TOOLS_DIR) go install $(GOLANGCI_LINT_REPO)/cmd/golangci-lint@$(GOLANGCI_LINT_VERSION)
$@ version
$@ linters
$(TOOLS_DIR)/oapi-codegen: | $(TOOLS_DIR)
@echo "Installing $(OAPI_CODEGEN_REPO)/cmd/oapi-codegen@$(OAPI_CODEGEN_VERSION)"
@GOBIN=$(ROOT_DIR)/$(TOOLS_DIR) go install $(OAPI_CODEGEN_REPO)/cmd/oapi-codegen@$(OAPI_CODEGEN_VERSION)
$@ -version