-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathMakefile
202 lines (174 loc) · 5 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
PROJECTNAME := $(shell go run app/main.go project)
VERSION := $(shell go run app/main.go version)
RELEASENAME := $(PROJECTNAME)_$(VERSION)
MYSQL_DB ?= $(shell go run app/main.go mysql)
MYSQL_DIR ?= database/migrations
ifndef $(GOPATH)
GOPATH=$(shell go env GOPATH)
export GOPATH
endif
all:
.PHONY: lint-prepare
lint-prepare:
@echo "Preparing Linter"
ifeq (,$(shell which golangci-lint))
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin latest
else
@echo "golangci-lint already installed"
endif
## lint, l: run linter to check quality code
.PHONY: lint l
lint l:
@make lint-prepare
golangci-lint run -v \
--exclude-use-default=false \
--disable-all \
--enable=goimports \
--enable=gocyclo \
--enable=nakedret \
--enable=golint \
--enable=gosimple \
--enable=goconst \
--enable=misspell \
--enable=unconvert \
--enable=varcheck \
--enable=unused \
--enable=deadcode \
--enable=unparam \
--enable=ineffassign \
--enable=gocritic \
--enable=prealloc \
--enable=scopelint \
--enable=staticcheck \
--enable=gosec \
./...
.PHONY: mockery-prepare
mockery-prepare:
@echo "prepare mockery"
ifeq (,$(shell which mockery))
@echo "Installing mockery"
@go get github.com/vektra/mockery/v2/
else
@echo "mockery already installed"
endif
## mockery-generate: generate all mock
.PHONY: mockery-generate
mockery-generate:
@make mockery-prepare
@echo "start mockery all"
@mockery --all
## test-unit, t: run all unit test
.PHONY: test-unit t
test-unit t:
@echo "Start: unit test"
@go test -v -short -race ./...
@echo "End: unit test"
## go-build, b: build to compile the project & swagger docs
.PHONY: go-build b
go-build b:
@echo "Start: build "$(PROJECTNAME)" project"
@make swagger-init
@make swagger-validate
@echo "Run: go build -o bin/"$(PROJECTNAME)" app/main.go"
@go build -o bin/"$(PROJECTNAME)" app/main.go
@echo "Done: "$(PROJECTNAME)" project has build"
## go-run, r: run http rest api project
.PHONY: go-run r
go-run r:
@echo "Run "$(PROJECTNAME)" project"
@make swagger-init
@make swagger-validate
@go run app/main.go
## worker, w: run worker for consumer
.PHONY: worker w
worker w:
@echo "Run "$(PROJECTNAME)" worker"
@go run app/main.go worker
## docker-build: dockerize the project
.PHONY: docker-build
docker-build:
@docker build . -f build/builder/Dockerfile -t "$(PROJECTNAME)":latest
## docker-up: run docker compose up
.PHONY: docker-up
docker-up:
@docker-compose up -d
## docker-down: run docker compose down
.PHONY: docker-down
docker-down:
@docker-compose down
.PHONY: swagger-init-prepare
swagger-init-prepare:
@echo "Prepare swag"
ifeq (,$(shell which swag))
go get github.com/swaggo/swag/cmd/swag
else
@echo "swag already installed"
endif
.PHONY: swagger-validate-prepare
swagger-validate-prepare:
@echo "Prepare go-swagger"
ifeq (,$(shell which swagger))
go get github.com/go-swagger/go-swagger/cmd/swagger
else
@echo "go-swagger already installed"
endif
## swagger-init: initialize swagger to folder ./api/docs
.PHONY: swagger-init
swagger-init:
@make swagger-init-prepare
@echo "Start: Initialize swagger"
swag init -g app/main.go -o ./api/docs
@echo "Done: initialize ./api/docs swagger"
## swagger-validate: validate swagger.yaml in folder ./api/docs
.PHONY: swagger-validate
swagger-validate:
@make swagger-validate-prepare
@echo "Start: Validate swagger"
swagger validate api/docs/swagger.yaml
@echo "Done: Validate swagger"
.PHONY: migrate-prepare
migrate-prepare:
@echo "prepare golang migrate using mysql"
ifeq (,$(wildcard ./bin/migrate))
@go build -a -o ./bin/migrate -tags 'mysql' github.com/golang-migrate/migrate/v4/cli
else
@echo "golang-migrate already installed"
endif
## migrate-create [file_name]: create migration file
.PHONY: migrate-create
migrate-create:
@make migrate-prepare
./bin/migrate create -ext sql -dir $(MYSQL_DIR) -seq $(filter-out $@,$(MAKECMDGOALS))
## migrate-up [N]: Apply all or N up migrations
.PHONY: migrate-up
migrate-up:
@make migrate-prepare
./bin/migrate -database "$(MYSQL_DB)" -path=$(MYSQL_DIR) up $(filter-out $@,$(MAKECMDGOALS))
## migrate-down [N]: Apply all or N down migrations
.PHONY: migrate-down
migrate-down:
@make migrate-prepare
./bin/migrate -database "$(MYSQL_DB)" -path=$(MYSQL_DIR) down $(filter-out $@,$(MAKECMDGOALS))
## release: create release app with version
.PHONY: release
release:
@echo "Start: build release "$(RELEASENAME)""
@make lint
@make test-unit
@make swagger-init
@make swagger-validate
@rm -rf release
@mkdir release
@echo "Build release "$(RELEASENAME)"_linux_amd64"
@GOOS=linux GOARCH=amd64 go build -o release/"$(RELEASENAME)"_linux_amd64 app/main.go
@echo "Build release "$(RELEASENAME)"_darwin_amd64"
@GOOS=darwin GOARCH=amd64 go build -o release/"$(RELEASENAME)"_darwin_amd64 app/main.go
@echo "Done: build release "$(RELEASENAME)""
.PHONY: help
all: help
help: Makefile
@echo
@echo " Choose a command run in "$(PROJECTNAME)":"
@echo
@sed -n 's/^##//p' $< | column -t -s ':' | sed -e 's/^/ /'
@echo