-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
160 lines (128 loc) · 3.78 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
PROJECTNAME = $(shell basename "$(PWD)")
include project.properties
ifndef VERBOSE
.SILENT:
endif
BASE_DIR = $(shell pwd)
BIN_DIR = $(BASE_DIR)/.bin
MOCK_DIR = $(BASE_DIR)/mocks
TMP_DIR = $(BASE_DIR)/.tmp
TMPL_DIR = $(BASE_DIR)/assets/templates
GO_PKGS = $(shell go list ./...)
GO_FILES = $(shell find . -type f -name '*.go')
GO_LDFLAGS = "-s -w \
-X github.com/vontikov/pgcluster/internal/app.Namespace=$(NS) \
-X github.com/vontikov/pgcluster/internal/app.App=$(APP) \
-X github.com/vontikov/pgcluster/internal/app.Version=$(VERSION)"
MOCKFILES = \
internal/concurrent/concurrent.go \
internal/pg/pg.go \
internal/storage/storage.go
MOCK_VENDOR_PREFIX = github.com/vontikov/pgcluster/vendor/
ifeq (test,$(firstword $(MAKECMDGOALS)))
ifneq '$(ALL)' 'true'
TEST_MODE = -short
endif
endif
TEST_OPTS = $(TEST_MODE) -timeout 300s -cover -coverprofile=$(COVERAGE_FILE) -failfast -v
TEST_PKGS = \
./internal/gateway \
./internal/pg \
./internal/sentinel
GODOG_DEFAULT_CONFIG = stoa.yaml
ifeq (check,$(firstword $(MAKECMDGOALS)))
ifeq ($(CONFIG),)
CONFIG := $(GODOG_DEFAULT_CONFIG)
endif
endif
GODOG_OPTS = -timeout 300s -failfast -v
GODOG_PKGS = ./integration
BINARY_FILE = $(BIN_DIR)/$(APP)
BIN_MARKER = $(TMP_DIR)/bin.marker
GODOG_MARKER = $(TMP_DIR)/godog.marker
MOCKS_MARKER = $(TMP_DIR)/mocks.marker
TEST_MARKER = $(TMP_DIR)/test.marker
VET_MARKER = $(TMP_DIR)/vet.marker
.PHONY: all help prepare clean
all: help
## help: Prints help
help: Makefile
@echo "Choose a command in "$(PROJECTNAME)":"
@sed -n 's/^##//p' $< | column -t -s ':' | sed -e 's/^/ /'
prepare:
@mkdir -p $(TMP_DIR)
@mkdir -p $(MOCK_DIR)
## clean: Cleans the project
clean:
@echo "Cleaning the project..."
@rm -rf $(BIN_DIR) $(TMP_DIR)
## vet: Runs Go vet
vet: $(VET_MARKER)
$(VET_MARKER): $(GO_FILES)
@echo "Running Go vet..."
@GOBIN=$(BIN_DIR); go vet $(GO_PKGS)
@gosec ./...
@mkdir -p $(@D)
@touch $@
## test [ALL=true]: Runs tests
test: $(TEST_MARKER)
$(TEST_MARKER): $(GO_FILES)
@echo "Executing tests..."
@mkdir -p $(BIN_DIR)
@GOBIN=$(BIN_DIR); go test $(TEST_OPTS) $(TEST_PKGS)
@mkdir -p $(@D)
@touch $@
## check [CONFIG=(stoa.yaml|etcd.yaml)]: Runs integration tests
check: image $(GODOG_MARKER)
$(GODOG_MARKER): $(GO_FILES)
@echo "Executing godog tests...\n\n"
@mkdir -p $(BIN_DIR)
@GOBIN=$(BIN_DIR); go test $(GODOG_OPTS) $(GODOG_PKGS) --config $(CONFIG)
@mkdir -p $(@D)
@touch $@
## build: Builds the binaries
build: $(BIN_MARKER)
$(BIN_MARKER): prepare $(GO_FILES)
@echo "Building the binaries..."
@mkdir -p $(BIN_DIR)
@GOBIN=$(BIN_DIR) \
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
go build \
-tags=$(BUILD_TAGS) \
-ldflags=$(GO_LDFLAGS) \
-o $(BINARY_FILE) cmd/*.go
@mkdir -p $(@D)
@touch $@
## image: Builds Docker image
image: build
@docker build \
--build-arg VERSION=$(VERSION) \
--build-arg PG_VERSION=$(PG_VERSION) \
-t $(DOCKER_NS)/$(APP):$(VERSION) .
## image-staged: Builds Docker image (staged)
image-staged:
@docker build \
--build-arg APP=$(APP) \
--build-arg NS=$(NS) \
--build-arg VERSION=$(VERSION) \
--build-arg PG_VERSION=$(PG_VERSION) \
-f Dockerfile-staged \
-t $(DOCKER_NS)/$(APP):$(VERSION) .
## mocks: Generates mocks
mocks: $(MOCKS_MARKER)
$(MOCKS_MARKER): $(MOCKFILES)
echo "Generating mocks..."
$(foreach v,$(MOCKFILES),$(call generate-mock,$v))
@mkdir -p $(@D)
@touch $@
define generate-mock
$(eval dest:=`echo "$(MOCK_DIR)/$(1)" | sed -E 's!vendor/|internal/!!'`)
echo "$1 -> ${dest}"
mockgen -source $1 -destination $(dest)
sed -i 's!$(MOCK_VENDOR_PREFIX)!!g' ${dest}
endef
## deps: Installs dependencies
deps:
@go mod tidy
@go install github.com/securego/gosec/v2/cmd/gosec@latest
@go install github.com/cucumber/godog/cmd/godog@v0.11.0