GO := CGO_ENABLED=0 go

.PHONY: build local-build fmt

IMG ?= hub.pingcap.net/qa/tcctl:latest

build:
	$(GO) mod tidy
	$(GO) build -o bin/tcctl

publish:
	scripts/publish.sh

fmt: groupimports
	go fmt ./...

groupimports: install-goimports
	goimports -w -l -local github.com/pingcap/test-infra .

install-goimports:
ifeq (, $(shell which goimports))
	@{ \
	set -e ;\
	TMP_DIR=$$(mktemp -d) ;\
	cd $$TMP_DIR ;\
	GO111MODULE=on go get golang.org/x/tools/cmd/goimports ;\
	rm -rf $$TMP_DIR ;\
	}
endif

local-build:
	cp go.mod go.mod.bak
	go mod edit -replace github.com/pingcap/test-infra=../..
	$(GO) mod tidy
	$(GO) build -o bin/tcctl
	mv go.mod.bak go.mod

local-build-linux:
	cp go.mod go.mod.bak
	go mod edit -replace github.com/pingcap/test-infra=../..
	$(GO) mod tidy
	GOOS=linux GOARCH=amd64 $(GO) build -o bin/tcctl
	mv go.mod.bak go.mod

image: local-build-linux
	docker build --platform linux/amd64 . -f Dockerfile -t $(IMG)

install-linter:
ifeq (, $(shell which golangci-lint))
	go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
endif


lint: install-linter
# only show issues that not exist in main
	golangci-lint run --new-from-rev=origin/main
