forked from mongodb/mongodb-atlas-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
191 lines (151 loc) · 6.82 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
# A Self-Documenting Makefile: http://marmelab.com/blog/2016/02/29/auto-documented-makefile.html
GOLANGCI_VERSION=v1.50.1
COVERAGE=coverage.out
MCLI_SOURCE_FILES?=./cmd/mongocli
MCLI_BINARY_NAME=mongocli
MCLI_VERSION?=$(shell git tag --list 'mongocli/v*' --sort=taggerdate | tail -1 | cut -d "v" -f 2 )
MCLI_GIT_SHA?=$(shell git rev-parse HEAD)
MCLI_DESTINATION=./bin/$(MCLI_BINARY_NAME)
MCLI_INSTALL_PATH="${GOPATH}/bin/$(MCLI_BINARY_NAME)"
MCLI_E2E_BINARY?=../../../bin/${MCLI_BINARY_NAME}
ATLAS_SOURCE_FILES?=./cmd/atlas
ATLAS_BINARY_NAME=atlas
ATLAS_VERSION?=$(shell git tag --list 'atlascli/v*' --sort=taggerdate | tail -1 | cut -d "v" -f 2 )
ATLAS_DESTINATION=./bin/$(ATLAS_BINARY_NAME)
ATLAS_INSTALL_PATH="${GOPATH}/bin/$(ATLAS_BINARY_NAME)"
REF=$(shell git describe --always --tags | grep '-')
ifneq (,$(findstring -,$(REF))) # if the tag doesn't point to the commit, we add -next to the version
MCLI_VERSION:="$(MCLI_VERSION)-next"
ATLAS_VERSION:="$(ATLAS_VERSION)-next"
endif
LINKER_FLAGS=-s -w -X github.com/mongodb/mongodb-atlas-cli/internal/version.GitCommit=${MCLI_GIT_SHA}
MCLI_LINKER_FLAGS=${LINKER_FLAGS} -X github.com/mongodb/mongodb-atlas-cli/internal/config.ToolName=$(MCLI_BINARY_NAME) -X github.com/mongodb/mongodb-atlas-cli/internal/version.Version=${MCLI_VERSION}
ATLAS_LINKER_FLAGS=${LINKER_FLAGS} -X github.com/mongodb/mongodb-atlas-cli/internal/config.ToolName=atlascli -X github.com/mongodb/mongodb-atlas-cli/internal/version.Version=${ATLAS_VERSION}
ATLAS_E2E_BINARY?=../../../bin/${ATLAS_BINARY_NAME}
DEBUG_FLAGS=all=-N -l
TEST_CMD?=go test
UNIT_TAGS?=unit
INTEGRATION_TAGS?=integration
E2E_TAGS?=e2e
E2E_TIMEOUT?=60m
export PATH := $(shell go env GOPATH)/bin:$(PATH)
export PATH := ./bin:$(PATH)
ifneq ($(OS),Windows_NT)
export SHELL := env PATH=$(PATH) /bin/bash
endif
export TERM := linux-m
export GO111MODULE := on
export MCLI_E2E_BINARY
export ATLAS_E2E_BINARY
.PHONY: deps
deps: ## Download go module dependencies
@echo "==> Installing go.mod dependencies..."
go mod download
go mod tidy
.PHONY: devtools
devtools: ## Install dev tools
@echo "==> Installing dev tools..."
go install github.com/google/addlicense@latest
go install github.com/golang/mock/mockgen@latest
go install golang.org/x/tools/cmd/goimports@latest
go install github.com/google/go-licenses@latest
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(shell go env GOPATH)/bin $(GOLANGCI_VERSION)
.PHONY: setup
setup: deps devtools ## Set up dev env
.PHONY: link-git-hooks
link-git-hooks: ## Install git hooks
@echo "==> Installing all git hooks..."
find .git/hooks -type l -exec rm {} \;
find .githooks -type f -exec ln -sf ../../{} .git/hooks/ \;
.PHONY: fmt
fmt: ## Format changed go
@scripts/fmt.sh
.PHONY: fmt-all
fmt-all: ### Format all go files with goimports and gofmt
find . -name "*.go" -not -path "./vendor/*" -not -path "./internal/mocks" -exec gofmt -w "{}" \;
find . -name "*.go" -not -path "./vendor/*" -not -path "./internal/mocks" -exec goimports -l -w "{}" \;
.PHONY: test
test: unit-test integration-test
.PHONY: lint
lint: ## Run linter
golangci-lint run
.PHONY: fix-lint
fix-lint: ## Fix linting errors
golangci-lint run --fix
.PHONY: check
check: test fix-lint ## Run tests and linters
.PHONY: addcopy
addcopy:
@scripts/add-copy.sh
.PHONY: gen-mocks
gen-mocks: ## Generate mocks
@echo "==> Generating mocks"
go generate ./internal...
.PHONY: gen-docs
gen-docs: gen-docs-mongocli gen-docs-atlascli ## Generate docs for commands
.PHONY: gen-docs-mongocli
gen-docs-mongocli: ## Generate docs for mongocli commands
@echo "==> Generating docs for mongocli"
go run ./tools/mongoclidocs/main.go
.PHONY: gen-docs-atlascli
gen-docs-atlascli: ## Generate docs for atlascli commands
@echo "==> Generating docs for atlascli"
go run ./tools/atlasclidocs/main.go
.PHONY: build
build: build-mongocli ## Generate a binary for mongocli
.PHONY: build-all
build-all: build-mongocli build-atlascli ## Generate a binary for both CLIs
.PHONY: build-mongocli
build-mongocli: ## Generate a mongocli binary in ./bin
@echo "==> Building $(MCLI_BINARY_NAME) binary"
go build -ldflags "$(MCLI_LINKER_FLAGS)" -o $(MCLI_DESTINATION) $(MCLI_SOURCE_FILES)
.PHONY: build-atlascli
build-atlascli: ## Generate a atlascli binary in ./bin
@echo "==> Building $(ATLAS_BINARY_NAME) binary"
go build -ldflags "$(ATLAS_LINKER_FLAGS)" -o $(ATLAS_DESTINATION) $(ATLAS_SOURCE_FILES)
.PHONY: build-debug
build-debug: build-mongocli-debug build-atlascli-debug ## Generate binaries in ./bin for debugging both CLIs
.PHONY: build-mongocli-debug
build-mongocli-debug: ## Generate a binary in ./bin for debugging mongocli
@echo "==> Building $(MCLI_BINARY_NAME) binary for debugging"
go build -gcflags="$(DEBUG_FLAGS)" -ldflags "$(MCLI_LINKER_FLAGS)" -o $(MCLI_DESTINATION) $(MCLI_SOURCE_FILES)
.PHONY: build-atlascli-debug
build-atlascli-debug: ## Generate a binary in ./bin for debugging atlascli
@echo "==> Building $(ATLAS_BINARY_NAME) binary for debugging"
go build -gcflags="$(DEBUG_FLAGS)" -ldflags "$(ATLAS_LINKER_FLAGS)" -o $(ATLAS_DESTINATION) $(ATLAS_SOURCE_FILES)
.PHONY: e2e-test
e2e-test: build-all ## Run E2E tests
@echo "==> Running E2E tests..."
# the target assumes the MCLI_* environment variables are exported
$(TEST_CMD) -v -p 1 -parallel 1 -timeout $(E2E_TIMEOUT) -tags="$(E2E_TAGS)" ./test/e2e...
.PHONY: integration-test
integration-test: ## Run integration tests
@echo "==> Running integration tests..."
$(TEST_CMD) --tags="$(INTEGRATION_TAGS)" -count=1 ./internal...
.PHONY: unit-test
unit-test: ## Run unit-tests
@echo "==> Running unit tests..."
$(TEST_CMD) --tags="$(UNIT_TAGS)" -race -cover -count=1 -coverprofile $(COVERAGE) ./...
.PHONY: install
install: install-mongocli install-atlascli ## Install binaries in $GOPATH/bin for both CLIs
.PHONY: install-mongocli
install-mongocli: ## Install mongocli binary in $GOPATH/bin
@echo "==> Installing $(MCLI_BINARY_NAME) to $(MCLI_INSTALL_PATH)"
go install -ldflags "$(MCLI_LINKER_FLAGS)" $(MCLI_SOURCE_FILES)
@echo "==> Done..."
.PHONY: install-atlascli
install-atlascli: ## Install atlascli binary in $GOPATH/bin
@echo "==> Installing $(ATLAS_BINARY_NAME) to $(ATLAS_INSTALL_PATH)"
go install -ldflags "$(ATLAS_LINKER_FLAGS)" $(ATLAS_SOURCE_FILES)
@echo "==> Done..."
.PHONY: list
list: ## List all make targets
@${MAKE} -pRrn : -f $(MAKEFILE_LIST) 2>/dev/null | awk -v RS= -F: '/^# File/,/^# Finished Make data base/ {if ($$1 !~ "^[#.]") {print $$1}}' | egrep -v -e '^[^[:alnum:]]' -e '^$@$$' | sort
.PHONY: check-library-owners
check-library-owners: ## Check that all the dependencies in go.mod has a owner in library_owners.json
@echo "==> Check library_owners.json"
go run ./tools/libraryowners/main.go
.PHONY: help
.DEFAULT_GOAL := help
help:
@grep -h -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'