-
Notifications
You must be signed in to change notification settings - Fork 1
/
Makefile
127 lines (94 loc) · 2.76 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
# repo
NAME=ddns
# version
VERSION=$(shell git describe --tags --always --dirty)
BUILD_DATE=$(shell date -u --iso-8601=seconds)
# system
SHELL := /usr/bin/env bash
# go
GO := go
GOHOSTOS := $(shell $(GO) env GOHOSTOS)
GOPATH := $(shell $(GO) env GOPATH)
GO_VERSION := $(shell $(GO) version | awk '{print $$3}')
GOOS := $(if $(GOOS),$(GOOS),$(shell $(GO) env GOOS))
GOARCH := $(if $(GOARCH),$(GOARCH),$(shell $(GO) env GOARCH))
GO_MODULE := $(shell awk '/module/{print $$2}' go.mod)
GIT_COMMIT := $(shell git rev-parse HEAD)
DIST := dist/$(NAME)/$(GOOS)/$(GOARCH)
# build
LD_FLAGS += -X "main.Version=$(VERSION)"
LD_FLAGS += -X "main.BuildDate=$(BUILD_DATE)"
LD_FLAGS += -X "main.GoVersion=$(GO_VERSION)"
LD_FLAGS += -X "main.GOOS=$(GOOS)"
LD_FLAGS += -X "main.GOARCH=$(GOARCH)"
LD_FLAGS += -X "main.GitCommit=$(shell git rev-parse HEAD)"
# docker
DOCKER = docker
DOCKER_REGISTRY = gitsang
#------------------------------------------------------------------------------#
##@ Debug
#------------------------------------------------------------------------------#
.PHONY: test
## run tests
test:
$(GO) test -race ./...
.PHONY: run
## run
run:
$(GO) run . \
$(filter-out $@, $(MAKECMDGOALS))
#------------------------------------------------------------------------------#
##@ Build
#------------------------------------------------------------------------------#
BUILD_DIRS := $(DIST)
$(BUILD_DIRS):
@mkdir -p $@
.PHONY: build
## build
build:
$(GO) build -o $(DIST)/$(NAME) .
.PHONY: docker
DOCKERFILE ?= Dockerfile
## build docker
docker: $(BUILD_DIRS)
$(DOCKER) build \
--no-cache \
--build-arg VERSION=$(shell git rev-parse HEAD) \
-t $(DOCKER_REGISTRY)/$(NAME):$(VERSION) \
-f $(DOCKERFILE) .
.PHONY: publish
## publish docker
publish:
$(DOCKER) push $(DOCKER_REGISTRY)/$(NAME):$(VERSION)
#------------------------------------------------------------------------------#
##@ Clean
#------------------------------------------------------------------------------#
.PHONY: clean
## clean up git repo
clean:
git clean -xfd
rm -fr $(DIST)
#------------------------------------------------------------------------------#
##@ Help
#------------------------------------------------------------------------------#
## display help
help:
@awk 'BEGIN \
{ \
FS = ":.*##"; \
printf "\nUsage:\n make \033[36m<target>\033[0m\n" \
} \
/^[0-9a-zA-Z\_\-]+:/ \
{ \
helpMessage = match(lastLine, /^## (.*)/); \
if (helpMessage) { \
helpCommand = substr($$1, 0, index($$1, ":")-1); \
helpMessage = substr(lastLine, RSTART + 2, RLENGTH); \
printf " \033[36m%-24s\033[0m %s\n", helpCommand,helpMessage; \
} \
} { lastLine = $$0 } \
/^##@/ \
{ \
printf "\n\033[1m%s\033[0m\n", substr($$0, 5) \
} ' $(MAKEFILE_LIST)
.DEFAULT_GOAL := help