GOVER_MAJOR := $(shell go version | sed -E -e "s/.*go([0-9]+)[.]([0-9]+).*/\1/")
GOVER_MINOR := $(shell go version | sed -E -e "s/.*go([0-9]+)[.]([0-9]+).*/\2/")
GO111 := $(shell [ $(GOVER_MAJOR) -gt 1 ] || [ $(GOVER_MAJOR) -eq 1 ] && [ $(GOVER_MINOR) -ge 11 ]; echo $$?)
ifeq ($(GO111), 1)
$(error Please upgrade your Go compiler to 1.11 or higher version)
endif

GOOS   := $(if $(OS_TYPE),$(OS_TYPE),linux)
GOENV  := GO15VENDOREXPERIMENT="1" CGO_ENABLED=0 GOOS=$(GOOS) GOARCH=amd64
GO     := $(GOENV) GO111MODULE=on go build -mod=vendor
GOTEST := CGO_ENABLED=0 GO111MODULE=on go test -v -mod=vendor -cover

LDFLAGS += -X "github.com/pingcap/tidb-enterprise/installer/tep-operator/version.BuildTS=$(shell date -u '+%Y-%m-%d %I:%M:%S')"
LDFLAGS += -X "github.com/pingcap/tidb-enterprise/installer/tep-operator/version.GitSHA=$(shell git rev-parse HEAD)"
LDFLAGS += -X "github.com/pingcap/tidb-enterprise/installer/tep-operator/version.GitBranch=$(shell git rev-parse --abbrev-ref HEAD)"
LDFLAGS += -X "github.com/pingcap/tidb-enterprise/installer/tep-operator/version.GoVersion=$(shell go version)"

PACKAGE_LIST := go list ./... | grep -vE "vendor" | grep -vE "pkg/client" | grep -vE "zz_generated"
PACKAGES := $$($(PACKAGE_LIST))
PACKAGE_DIRECTORIES := $(PACKAGE_LIST) | sed 's|github.com/pingcap/tidb-enterprise/installer/tep-operator/||'
FILES := $$(find $$($(PACKAGE_DIRECTORIES)) -name "*.go")
FAIL_ON_STDOUT := awk '{ print } END { if (NR > 0) { exit 1 } }'

default: build

build: tep-operator

tep-operator:
	$(GO) -ldflags '$(LDFLAGS)' -o bin/tep-controller-manager cmd/controller-manager/main.go

tidy:
	@GO111MODULE=on go mod tidy

test:
	@echo "Run unit tests"
	@$(GOTEST) ./pkg/... && echo "\nUnit tests run successfully!"

update:
	@echo "Update go vendor"
	@GO111MODULE=on go mod vendor -v

check-all: lint check-static check-shadow check-gosec megacheck errcheck

check-setup:
	@GO111MODULE=on CGO_ENABLED=0 go get github.com/dnephin/govet@4a96d43e39d340b63daa8bc5576985aa599885f6
	@GO111MODULE=off CGO_ENABLED=0 go get gopkg.in/alecthomas/gometalinter.v2
	@GO111MODULE=on CGO_ENABLED=0 go get github.com/client9/misspell/cmd/misspell@7888c6b6ce89353cd98e196bce3c3f9e4cdf31f6
	@GO111MODULE=on CGO_ENABLED=0 go get github.com/gordonklaus/ineffassign@7bae11eba15a3285c75e388f77eb6357a2d73ee2
	@GO111MODULE=on CGO_ENABLED=0 go get honnef.co/go/tools/cmd/megacheck@88497007e8588ea5b6baee991f74a1607e809487
	@GO111MODULE=on CGO_ENABLED=0 go get github.com/kisielk/errcheck@55d8f507faff4d6eddd0c41a3e713e2567fca4e5
	@GO111MODULE=on CGO_ENABLED=0 go get github.com/mgechev/revive@7773f47324c2bf1c8f7a5500aff2b6c01d3ed73b
	@GO111MODULE=on CGO_ENABLED=0 go get github.com/securego/gosec/cmd/gosec@5fb530cda357c16175f2c049577d2030de735b28


check: lint check-static

check-static:
	@ # Not running vet and fmt through metalinter becauase it ends up looking at vendor
	@echo "gofmt checking"
	gofmt -s -l -w $(FILES) 2>&1| $(FAIL_ON_STDOUT)
	@echo "govet check"
	govet -all $$($(PACKAGE_DIRECTORIES)) 2>&1
	@echo "mispell and ineffassign checking"
	CGO_ENABLED=0 gometalinter.v2 --disable-all \
	  --enable misspell \
	  --enable ineffassign \
	  $$($(PACKAGE_DIRECTORIES))

megacheck:
	@echo "gometalinter megacheck"
	CGO_ENABLED=0 gometalinter.v2 --disable-all --deadline 120s \
	  --enable megacheck \
	  $$($(PACKAGE_DIRECTORIES))

errcheck:
	@echo "gometalinter errcheck"
	CGO_ENABLED=0 gometalinter.v2 --disable-all --deadline 120s \
	  --enable errcheck \
	  $$($(PACKAGE_DIRECTORIES))

check-shadow:
	@echo "govet shadow checking"
	govet -shadow $$($(PACKAGE_DIRECTORIES))

lint:
	@echo "linting"
	CGO_ENABLED=0 revive -formatter friendly -config revive.toml $$($(PACKAGE_DIRECTORIES))

check-gosec:
	@echo "security checking"
	CGO_ENABLED=0 gosec $$($(PACKAGE_DIRECTORIES))

.PHONY: check check-all build
