-
Notifications
You must be signed in to change notification settings - Fork 2
/
Makefile
126 lines (95 loc) · 3.98 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
.PHONY: help
help: ## Prints help (only for targets with comments)
@grep -E '^[a-zA-Z._-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
APP=dwarka
SRC_PACKAGES=$(shell go list -mod=vendor ./... | grep -v "vendor" | grep -v "swagger")
VERSION?=2.3
BUILD?=$(shell git describe --always --dirty 2> /dev/null)
GOLINT:=$(shell command -v golint 2> /dev/null)
APP_EXECUTABLE="./out/$(APP)"
RICHGO=$(shell command -v richgo 2> /dev/null)
GOMETA_LINT=$(shell command -v golangci-lint 2> /dev/null)
GOLANGCI_LINT_VERSION=v1.21.0
GO111MODULE=on
SHELL=bash -o pipefail
BUILD_ARGS="-s -w -X main.version=$(VERSION) -X main.build=$(BUILD)"
ifeq ($(GOMETA_LINT),)
GOMETA_LINT=$(shell command -v $(PWD)/bin/golangci-lint 2> /dev/null)
endif
ifeq ($(RICHGO),)
GO_BINARY=go
else
GO_BINARY=richgo
endif
ifeq ($(BUILD),)
BUILD=dev
endif
ifdef CI_COMMIT_SHORT_SHA
BUILD=$(CI_COMMIT_SHORT_SHA)
endif
all: setup build
ci: setup-common build-common
ensure-build-dir:
mkdir -p out
build-deps: ## Install dependencies
go get
go mod tidy
go mod vendor
update-deps: ## Update dependencies
go get -u
compile: compile-app ## Compile dwarka
compile-app: ensure-build-dir
$(GO_BINARY) build -mod=vendor -ldflags $(BUILD_ARGS) -o $(APP_EXECUTABLE) ./main.go
install: ## Install dwarka
go install -mod=vendor -ldflags $(BUILD_ARGS)
compile-linux: ensure-build-dir ## Compile dwarka for linux
GOOS=linux GOARCH=amd64 $(GO_BINARY) build -mod=vendor -ldflags $(BUILD_ARGS) -o $(APP_EXECUTABLE) ./main.go
build: fmt build-common ## Build the application
build-common: vet lint-all test compile
compress: compile ## Compress the binary
upx $(APP_EXECUTABLE)
generate: ## Generate files using go generate
go generate gitlab.com/vedhabhavanam/smarthome/dwarka/...
fmt:
GOFLAGS="-mod=vendor" $(GO_BINARY) fmt $(SRC_PACKAGES)
vet:
$(GO_BINARY) vet -mod=vendor $(SRC_PACKAGES)
setup-common:
ifeq ($(GOMETA_LINT),)
curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s $(GOLANGCI_LINT_VERSION)
endif
ifeq ($(GOLINT),)
GO111MODULE=off $(GO_BINARY) get -u golang.org/x/lint/golint
endif
setup-richgo:
ifeq ($(RICHGO),)
GO111MODULE=off $(GO_BINARY) get -u github.com/kyoh86/richgo
endif
setup: setup-richgo setup-common ensure-build-dir ## Setup environment
lint-all: lint setup-common
$(GOMETA_LINT) run --modules-download-mode=vendor --timeout 2m0s
lint:
./scripts/lint $(SRC_PACKAGES)
test-all: test test.integration
test: ensure-build-dir ## Run tests
ENVIRONMENT=test $(GO_BINARY) test -mod=vendor $(SRC_PACKAGES) -race -coverprofile ./out/coverage -short -v | grep -viE "start|no test files"
test-cover-html: ## Run tests with coverage
mkdir -p ./out
@echo "mode: count" > coverage-all.out
$(foreach pkg, $(SRC_PACKAGES),\
ENVIRONMENT=test $(GO_BINARY) test -mod=vendor -coverprofile=coverage.out -covermode=count $(pkg);\
tail -n +2 coverage.out >> coverage-all.out;)
$(GO_BINARY) tool -mod=vendor cover -html=coverage-all.out -o out/coverage.html
dev-docker-build: ## Build dwarka server docker image with local chartmuseum repo added to it
docker build --build-arg BUILD_MODE="dev" -f docker/dwarka/Dockerfile -t local-dwarka .
@echo "To run:"
@echo "$ docker run --rm -it -p 5443:5443 --name local-dwarka local-dwarka:latest"
dev-docker-run: ## Run dwarka server in local docker container pointing to local(host-machine) chartmuseum
docker run --rm -it -p 5443:5443 --name local-dwarka local-dwarka:latest
generate-test-summary:
ENVIRONMENT=test $(GO_BINARY) test -mod=vendor $(SRC_PACKAGES) -race -coverprofile ./out/coverage -short -v -json | grep -viE "start|no test files" | tee test-summary.json; \
sed -i '' -E "s/^(.+\| {)/{/" test-summary.json; \
passed=`cat test-summary.json | jq | rg '"Action": "pass"' | wc -l`; \
skipped=`cat test-summary.json | jq | rg '"Action": "skip"' | wc -l`; \
failed=`cat test-summary.json | jq | rg '"Action": "fail"' | wc -l`; \
echo "Passed: $$passed | Failed: $$failed | Skipped: $$skipped"