1.PHONY: all-release build clean fmt fmt-go fmt-terraform lint lint-go lint-terraform release test vendor vendor-status vet
2
3ARCH ?= amd64
4PLATFORM ?= linux
5ALL_PLATFORMS := darwin linux windows
6BIN := terraform-provider-vultr
7PKG := github.com/squat/$(BIN)
8BUILD_IMAGE ?= golang:1.10.0-alpine
9TEST ?= $$(go list ./... | grep -v 'vendor/')
10GOFMT_FILES ?= $$(find . -name '*.go' | grep -v vendor)
11SRC := $(shell find . -type f -name '*.go' -not -path "./vendor/*")
12TERRAFORMFMT_FILES ?= examples
13TESTARGS ?=
14TAG := $(shell git describe --abbrev=0 --tags HEAD 2>/dev/null)
15COMMIT := $(shell git rev-parse HEAD)
16VERSION := $(COMMIT)
17ifneq ($(TAG),)
18    ifeq ($(COMMIT), $(shell git rev-list -n1 $(TAG)))
19        VERSION := $(TAG)
20    endif
21endif
22DIRTY := $(shell test -z "$$(git diff --shortstat 2>/dev/null)" || echo -dirty)
23VERSION := $(VERSION)$(DIRTY)
24
25default: build
26
27build:
28	go install
29
30all-release: $(addprefix release-, $(ALL_PLATFORMS))
31
32release-%:
33	@$(MAKE) --no-print-directory ARCH=$(ARCH) PLATFORM=$* release
34
35release: bin/$(BIN)_$(VERSION)_$(PLATFORM)_$(ARCH).tar.gz.asc
36
37bin/$(PLATFORM)/$(ARCH):
38	@mkdir -p bin/$(PLATFORM)/$(ARCH)
39
40bin/$(BIN)_$(VERSION)_$(PLATFORM)_$(ARCH).tar.gz.asc: bin/$(BIN)_$(VERSION)_$(PLATFORM)_$(ARCH).tar.gz
41	@cd bin && gpg --armor --detach-sign $(<F)
42
43bin/$(BIN)_$(VERSION)_$(PLATFORM)_$(ARCH).tar.gz: bin/$(PLATFORM)/$(ARCH)/$(BIN)_$(VERSION)
44	@tar -czf $@ -C $(<D) $(<F)
45
46bin/$(PLATFORM)/$(ARCH)/$(BIN)_$(VERSION): $(SRC) glide.yaml bin/$(PLATFORM)/$(ARCH)
47	@echo "building: $@"
48	@docker run --rm \
49	    -u $$(id -u):$$(id -g) \
50	    -v $$(pwd):/go/src/$(PKG) \
51	    -v $$(pwd)/bin/$(PLATFORM)/$(ARCH):/go/bin \
52	    -w /go/src/$(PKG) \
53	    $(BUILD_IMAGE) \
54	    /bin/sh -c " \
55	        GOARCH=$(ARCH) \
56	        GOOS=$(PLATFORM) \
57		CGO_ENABLED=0 \
58		go build -o /go/bin/$(BIN)_$(VERSION) \
59	    "
60
61fmt: fmt-go fmt-terraform
62
63fmt-go:
64	gofmt -w -s $(GOFMT_FILES)
65
66fmt-terraform:
67	terraform fmt $(TERRAFORMFMT_FILES)
68
69lint: lint-go lint-terraform
70
71lint-go:
72	@echo 'golint $(TEST)'
73	@lint_res=$$(golint $(TEST)); if [ -n "$$lint_res" ]; then \
74		echo ""; \
75		echo "Golint found style issues. Please check the reported issues"; \
76		echo "and fix them if necessary before submitting the code for review:"; \
77		echo "$$lint_res"; \
78		exit 1; \
79	fi
80	@echo 'gofmt -d -s $(GOFMT_FILES)'
81	@fmt_res=$$(gofmt -d -s $(GOFMT_FILES)); if [ -n "$$fmt_res" ]; then \
82		echo ""; \
83		echo "Gofmt found style issues. Please check the reported issues"; \
84		echo "and fix them if necessary before submitting the code for review:"; \
85		echo "$$fmt_res"; \
86		exit 1; \
87	fi
88
89lint-terraform:
90	@echo "terraform fmt --check=true $(TERRAFORMFMT_FILES)"
91	@lint_res=$$(terraform fmt --check=true $(TERRAFORMFMT_FILES)); if [ -n "$$lint_res" ]; then \
92		echo ""; \
93		echo "Terraform fmt found style issues. Please check the reported issues"; \
94		echo "and fix them if necessary before submitting the code for review:"; \
95		echo "$$lint_res"; \
96		exit 1; \
97	fi
98
99
100test: vet lint
101	go test -i $(TEST) || exit 1
102	go test $(TESTARGS) -timeout=30s -parallel=4 $(TEST)
103
104vendor:
105	@glide install -v
106	@glide-vc --only-code --no-tests
107
108vendor-status:
109	@glide list
110
111vet:
112	@echo 'go vet $(TEST)'
113	@go vet $(TEST); if [ $$? -eq 1 ]; then \
114		echo ""; \
115		echo "Vet found suspicious constructs. Please check the reported constructs"; \
116		echo "and fix them if necessary before submitting the code for review."; \
117		exit 1; \
118	fi
119
120clean:
121	@rm -rf bin
122