|
| 1 | +GOCMD=go |
| 2 | +GOTEST=$(GOCMD) test |
| 3 | +GOVET=$(GOCMD) vet |
| 4 | +BINARY_NAME=pure-fa-om-exporter |
| 5 | +MODULE_NAME=go mod init purestorage/fa-openmetrics-exporter |
| 6 | +VERSION?=0.2.0 |
| 7 | +SERVICE_PORT?=9491 |
| 8 | +DOCKER_REGISTRY?= quay.io/purestorage/ |
| 9 | +EXPORT_RESULT?=false # for CI please set EXPORT_RESULT to true |
| 10 | + |
| 11 | +GREEN := $(shell tput -Txterm setaf 2) |
| 12 | +YELLOW := $(shell tput -Txterm setaf 3) |
| 13 | +WHITE := $(shell tput -Txterm setaf 7) |
| 14 | +CYAN := $(shell tput -Txterm setaf 6) |
| 15 | +RESET := $(shell tput -Txterm sgr0) |
| 16 | + |
| 17 | +.PHONY: all test build |
| 18 | + |
| 19 | +all: help |
| 20 | + |
| 21 | +## Build: |
| 22 | +init: |
| 23 | + $(GOCMD) init go mod init $(MODULE_NAME) |
| 24 | + $(GOCMD) mod tidy |
| 25 | + |
| 26 | +build: ## Build your project and put the output binary in out/bin/ |
| 27 | + mkdir -p out/bin |
| 28 | + GO111MODULE=on $(GOCMD) build -o out/bin/$(BINARY_NAME) cmd/fa-om-exporter/main.go |
| 29 | + |
| 30 | +clean: ## Remove build related file |
| 31 | + rm -fr ./bin |
| 32 | + rm -fr ./out |
| 33 | + rm -f ./junit-report.xml checkstyle-report.xml ./coverage.xml ./profile.cov yamllint-checkstyle.xml |
| 34 | + |
| 35 | +watch: ## Run the code with cosmtrek/air to have automatic reload on changes |
| 36 | + $(eval PACKAGE_NAME=$(shell head -n 1 go.mod | cut -d ' ' -f2)) |
| 37 | + docker run -it --rm -w /go/src/$(PACKAGE_NAME) -v $(shell pwd):/go/src/$(PACKAGE_NAME) -p $(SERVICE_PORT):$(SERVICE_PORT) cosmtrek/air |
| 38 | + |
| 39 | +## Test: |
| 40 | +test: ## Run the tests of the project |
| 41 | +ifeq ($(EXPORT_RESULT), true) |
| 42 | + GO111MODULE=off go get -u github.com/jstemmer/go-junit-report |
| 43 | + $(eval OUTPUT_OPTIONS = | tee /dev/tty | go-junit-report -set-exit-code > junit-report.xml) |
| 44 | +endif |
| 45 | + $(GOTEST) -v -race ./... $(OUTPUT_OPTIONS) |
| 46 | + |
| 47 | +coverage: ## Run the tests of the project and export the coverage |
| 48 | + $(GOTEST) -cover -covermode=count -coverprofile=profile.cov ./... |
| 49 | + $(GOCMD) tool cover -func profile.cov |
| 50 | +ifeq ($(EXPORT_RESULT), true) |
| 51 | + GO111MODULE=off go get -u github.com/AlekSi/gocov-xml |
| 52 | + GO111MODULE=off go get -u github.com/axw/gocov/gocov |
| 53 | + gocov convert profile.cov | gocov-xml > coverage.xml |
| 54 | +endif |
| 55 | + |
| 56 | +## Lint: |
| 57 | +lint: lint-go lint-dockerfile lint-yaml ## Run all available linters |
| 58 | + |
| 59 | +lint-dockerfile: ## Lint your Dockerfile |
| 60 | +# If dockerfile is present we lint it. |
| 61 | +ifeq ($(shell test -e ./Dockerfile && echo -n yes),yes) |
| 62 | + $(eval CONFIG_OPTION = $(shell [ -e $(shell pwd)/.hadolint.yaml ] && echo "-v $(shell pwd)/.hadolint.yaml:/root/.config/hadolint.yaml" || echo "" )) |
| 63 | + $(eval OUTPUT_OPTIONS = $(shell [ "${EXPORT_RESULT}" == "true" ] && echo "--format checkstyle" || echo "" )) |
| 64 | + $(eval OUTPUT_FILE = $(shell [ "${EXPORT_RESULT}" == "true" ] && echo "| tee /dev/tty > checkstyle-report.xml" || echo "" )) |
| 65 | + docker run --rm -i $(CONFIG_OPTION) hadolint/hadolint hadolint $(OUTPUT_OPTIONS) - < ./Dockerfile $(OUTPUT_FILE) |
| 66 | +endif |
| 67 | + |
| 68 | +lint-go: ## Use golintci-lint on your project |
| 69 | + $(eval OUTPUT_OPTIONS = $(shell [ "${EXPORT_RESULT}" == "true" ] && echo "--out-format checkstyle ./... | tee /dev/tty > checkstyle-report.xml" || echo "" )) |
| 70 | + docker run --rm -v $(shell pwd):/app -w /app golangci/golangci-lint:latest-alpine golangci-lint run --deadline=65s $(OUTPUT_OPTIONS) |
| 71 | + |
| 72 | +lint-yaml: ## Use yamllint on the yaml file of your projects |
| 73 | +ifeq ($(EXPORT_RESULT), true) |
| 74 | + GO111MODULE=off go get -u github.com/thomaspoignant/yamllint-checkstyle |
| 75 | + $(eval OUTPUT_OPTIONS = | tee /dev/tty | yamllint-checkstyle > yamllint-checkstyle.xml) |
| 76 | +endif |
| 77 | + docker run --rm -it -v $(shell pwd):/data cytopia/yamllint -f parsable $(shell git ls-files '*.yml' '*.yaml') $(OUTPUT_OPTIONS) |
| 78 | + |
| 79 | +## Docker: |
| 80 | +docker-build: ## Use the dockerfile to build the container |
| 81 | + docker build --rm --tag $(BINARY_NAME) --file build/docker/Dockerfile . |
| 82 | + |
| 83 | +docker-release: ## Release the container with tag latest and version |
| 84 | + docker tag $(BINARY_NAME) $(DOCKER_REGISTRY)$(BINARY_NAME):latest |
| 85 | + docker tag $(BINARY_NAME) $(DOCKER_REGISTRY)$(BINARY_NAME):$(VERSION) |
| 86 | + # Push the docker images |
| 87 | + docker push $(DOCKER_REGISTRY)$(BINARY_NAME):latest |
| 88 | + docker push $(DOCKER_REGISTRY)$(BINARY_NAME):$(VERSION) |
| 89 | + |
| 90 | +## Help: |
| 91 | +help: ## Show this help. |
| 92 | + @echo '' |
| 93 | + @echo 'Usage:' |
| 94 | + @echo ' ${YELLOW}make${RESET} ${GREEN}<target>${RESET}' |
| 95 | + @echo '' |
| 96 | + @echo 'Targets:' |
| 97 | + @awk 'BEGIN {FS = ":.*?## "} { \ |
| 98 | + if (/^[a-zA-Z_-]+:.*?##.*$$/) {printf " ${YELLOW}%-20s${GREEN}%s${RESET}\n", $$1, $$2} \ |
| 99 | + else if (/^## .*$$/) {printf " ${CYAN}%s${RESET}\n", substr($$1,4)} \ |
| 100 | + }' $(MAKEFILE_LIST) |
0 commit comments