forked from scaleway/scaleway-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
199 lines (151 loc) · 7.18 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
# Go parameters
GOENV ?=
GO ?= $(GOENV) go
GODEP ?= $(GOENV) godep
GOBUILD ?= $(GO) build
GOCLEAN ?= $(GO) clean
GOINSTALL ?= $(GO) install
GOTEST ?= $(GO) test $(GOTESTFLAGS)
GOMINORVERSION ?= $(shell $(GO) version | cut -c 14- | cut -d' ' -f1 | cut -d'.' -f2)
GOFMT ?= gofmt -w -s
GODIR ?= github.com/scaleway/scaleway-cli
GOCOVER ?= $(GOTEST) -covermode=count -v
# FPM is a simple way to create debian/rpm/... packages.
FPM_DOCKER = \
-it --rm \
-v $(PWD)/dist/latest:/output \
-w /output \
tenzer/fpm
FPM_ARGS = \
-C /input/ \
-s dir \
--name=scw \
--no-depends \
--license=mit \
-m "Scaleway <opensource@scaleway.com>"
NAME = scw
rwildcard=$(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2) $(filter $(subst *,%,$2),$d))
uniq = $(if $1,$(firstword $1) $(call uniq,$(filter-out $(firstword $1),$1)))
SOURCES := $(call rwildcard,./pkg ./cmd,*.go)
COMMANDS := $(call uniq,$(dir $(call rwildcard,./cmd/,*.go)))
PACKAGES := $(call uniq,$(dir $(call rwildcard,./pkg/,*.go))) .
REV = $(shell git rev-parse --short HEAD 2>/dev/null || echo "nogit")
LDFLAGS = "-X `go list ./pkg/scwversion`.GITCOMMIT=$(REV) -s"
BUILDER = scaleway-cli-builder
BUILD_LIST = $(foreach int, $(COMMANDS), $(int)_build)
CLEAN_LIST = $(foreach int, $(COMMANDS) $(PACKAGES), $(int)_clean)
INSTALL_LIST = $(foreach int, $(COMMANDS), $(int)_install)
TEST_LIST = $(foreach int, $(COMMANDS) $(PACKAGES), $(int)_test)
FMT_LIST = $(foreach int, $(COMMANDS) $(PACKAGES), $(int)_fmt)
COVERPROFILE_LIST = $(foreach int, $(subst $(GODIR),./,$(PACKAGES)), $(int)/profile.out)
.PHONY: $(CLEAN_LIST) $(TEST_LIST) $(FMT_LIST) $(INSTALL_LIST) $(BUILD_LIST) $(IREF_LIST)
all: build
build: $(BUILD_LIST)
clean: $(CLEAN_LIST)
install: $(INSTALL_LIST)
test: $(TEST_LIST)
fmt: $(FMT_LIST)
$(BUILD_LIST): %_build: %_fmt
@# https://golang.org/doc/go1.12#vet
@# vel = go tip version
@if [ $(GOMINORVERSION) = vel ] || [ $(GOMINORVERSION) -gt 11 ]; then \
$(GO) vet ./...; \
else \
$(GO) tool vet --all=true $(shell echo $(SOURCES) | tr " " "\n" | grep -v test.go); \
fi
$(GOBUILD) -ldflags $(LDFLAGS) -o $(NAME) ./cmd/$(NAME)
$(CLEAN_LIST): %_clean:
$(GOCLEAN) $(subst $(GODIR),./,$*)
$(INSTALL_LIST): %_install:
$(GOINSTALL) ./$*
$(TEST_LIST): %_test:
$(GOTEST) -ldflags $(LDFLAGS) -v ./$*
$(FMT_LIST): %_fmt:
@$(GOFMT) $(SOURCES)
prepare-release-dist: build
### Prepare dist/ directory ###
$(eval VERSION := $(shell ./scw version | sed -n 's/Client version: v\(.*\)/\1/p'))
rm -rf dist/$(VERSION)
rm -rf dist/latest
mkdir -p dist/$(VERSION)
ln -s -f $(VERSION) dist/latest
### Cross compile scaleway-cli ###
GOOS=linux GOARCH=386 go build -o dist/latest/scw-linux-i386 github.com/scaleway/scaleway-cli/cmd/scw
GOOS=linux GOARCH=amd64 go build -o dist/latest/scw-linux-amd64 github.com/scaleway/scaleway-cli/cmd/scw
GOOS=linux GOARCH=arm go build -o dist/latest/scw-linux-arm github.com/scaleway/scaleway-cli/cmd/scw
GOOS=linux GOARCH=arm64 go build -o dist/latest/scw-linux-arm64 github.com/scaleway/scaleway-cli/cmd/scw
GOOS=darwin GOARCH=386 go build -o dist/latest/scw-darwin-i386 github.com/scaleway/scaleway-cli/cmd/scw
GOOS=darwin GOARCH=amd64 go build -o dist/latest/scw-darwin-amd64 github.com/scaleway/scaleway-cli/cmd/scw
# Issue with mody and [free|net]bsd for go1.12+ https://github.com/moby/moby/pull/38818
if [ $(GOMINORVERSION) -lt 12 ]; then \
GOOS=freebsd GOARCH=386 go build -o dist/latest/scw-freebsd-i386 github.com/scaleway/scaleway-cli/cmd/scw && \
GOOS=freebsd GOARCH=amd64 go build -o dist/latest/scw-freebsd-amd64 github.com/scaleway/scaleway-cli/cmd/scw && \
GOOS=freebsd GOARCH=arm go build -o dist/latest/scw-freebsd-arm github.com/scaleway/scaleway-cli/cmd/scw && \
\
GOOS=netbsd GOARCH=386 go build -o dist/latest/scw-netbsd-i386 github.com/scaleway/scaleway-cli/cmd/scw && \
GOOS=netbsd GOARCH=amd64 go build -o dist/latest/scw-netbsd-amd64 github.com/scaleway/scaleway-cli/cmd/scw && \
GOOS=netbsd GOARCH=arm go build -o dist/latest/scw-netbsd-arm github.com/scaleway/scaleway-cli/cmd/scw; \
fi
GOOS=windows GOARCH=386 go build -o dist/latest/scw-windows-i386.exe github.com/scaleway/scaleway-cli/cmd/scw
GOOS=windows GOARCH=amd64 go build -o dist/latest/scw-windows-amd64.exe github.com/scaleway/scaleway-cli/cmd/scw
prepare-release-docker-image: dist/latest/scw-linux-i386
@echo ${VERSION} | grep -qv 'v' || ( echo "ERROR: VERSION not set or contains a leading 'v'" >&2 && exit 1 )
### Prepare scaleway-cli Docker image ###
docker run --rm golang tar -cf - /etc/ssl > dist/ssl.tar
docker build -t scaleway/cli dist
docker run scaleway/cli version
docker tag scaleway/cli:latest scaleway/cli:v$(VERSION)
prepare-release-debian-packages: dist/latest/scw-linux-amd64 dist/latest/scw-linux-i386 dist/latest/scw-linux-arm dist/latest/scw-linux-arm64
@echo ${VERSION} | grep -qv 'v' || ( echo "ERROR: VERSION not set or contains a leading 'v'" >&2 && exit 1 )
### Build debian packages ###
docker run -v $(PWD)/dist/latest/scw-linux-amd64:/input/usr/bin/scw $(FPM_DOCKER) $(FPM_ARGS) --version $(VERSION) -t deb -a x86_64 ./
docker run -v $(PWD)/dist/latest/scw-linux-i386:/input/usr/bin/scw $(FPM_DOCKER) $(FPM_ARGS) --version $(VERSION) -t deb -a i386 ./
docker run -v $(PWD)/dist/latest/scw-linux-arm:/input/usr/bin/scw $(FPM_DOCKER) $(FPM_ARGS) --version $(VERSION) -t deb -a arm ./
docker run -v $(PWD)/dist/latest/scw-linux-arm64:/input/usr/bin/scw $(FPM_DOCKER) $(FPM_ARGS) --version $(VERSION) -t deb -a arm64 ./
.PHONY: golint
golint:
@$(GO) get github.com/golang/lint/golint
@for dir in $(shell $(GO) list ./... | grep -v /vendor/); do golint $$dir; done
.PHONY: gocyclo
gocyclo:
go get github.com/fzipp/gocyclo
gocyclo -over 15 $(shell find . -name "*.go" -not -name "*test.go" | grep -v /vendor/)
.PHONY: godep-save
godep-save:
go get github.com/tools/godep
$(GODEP) save $(PACKAGES) $(COMMANDS)
.PHONY: convey
convey:
go get github.com/smartystreets/goconvey
$(GOENV) goconvey -cover -port=9042 -workDir="$(realpath .)/pkg" -depth=-1
.PHONY: travis_login
travis_login:
@if [ "$(TRAVIS_SCALEWAY_TOKEN)" -a "$(TRAVIS_SCALEWAY_ORGANIZATION)" ]; then \
echo '{"organization":"$(TRAVIS_SCALEWAY_ORGANIZATION)","token":"$(TRAVIS_SCALEWAY_TOKEN)"}' > ~/.scwrc && \
chmod 600 ~/.scwrc; \
else \
echo "Cannot login, credentials are missing"; \
fi
.PHONY: cover
cover: profile.out
$(COVERPROFILE_LIST):: $(SOURCES)
rm -f $@
$(GOCOVER) -ldflags $(LDFLAGS) -coverpkg=./pkg/... -coverprofile=$@ ./$(dir $@)
profile.out:: $(COVERPROFILE_LIST)
rm -f $@
echo "mode: set" > $@
cat ./pkg/*/profile.out | grep -v mode: | sort -r | awk '{if($$1 != last) {print $$0;last=$$1}}' >> $@
.PHONY: travis_coveralls
travis_coveralls:
if [ -f ~/.scwrc ]; then goveralls -covermode=count -service=travis-ci -v -coverprofile=profile.out; fi
.PHONY: travis_cleanup
travis_cleanup:
# FIXME: delete only resources created for this project
@if [ "$(TRAVIS_SCALEWAY_TOKEN)" -a "$(TRAVIS_SCALEWAY_ORGANIZATION)" ]; then \
./scw stop -t $(shell ./scw ps -q) || true; \
./scw rm $(shell ./scw ps -aq) || true; \
./scw rmi $(shell ./scw images -a -f organization=me -q) || true; \
fi
.PHONY: show_version
show_version:
./scw version