
# Image URL to use all building/pushing image targets
IMG ?= cluster-control-server:latest

GO_MOD_NAME = $(shell go list -m)
GOOS ?= "linux"
GOARCH ?= $(shell go env GOARCH)

##@ Development

.PHONY: lint
lint: golangci-lint #aws-iam-policy-linter## Run linter tests.
	$(GOLANGCI_LINT) run ./...

.PHONY: lint-fix
lint-fix: golangci-lint ## Fix linting issues.
	$(GOLANGCI_LINT) run --fix ./...

.PHONY: unit
unit:
	go test ./...

##@ Build

.PHONY: build
build:
	CGO_ENABLED=0 GOOS=$(GOOS) GOARCH=$(GOARCH) go build -C ./ -ldflags "-s -w" -o bin/server

.PHONY: run
run: ## run cluster-server locally
	go run ./ --kubeconfig=${HOME}/.kube/config

# Should be called after `make docker-build`
.PHONY: docker-build
docker-build: build
	docker build -t ${IMG} ./

# Should be called after `make docker-push`
.PHONY: docker-push
docker-push:
	docker push ${IMG}

.PHONY: reviewable
reviewable: lint-fix test ## Run through basic checks for the test.
	GOWORK=off go mod tidy
	git diff --exit-code

GOLANGCI_LINT = $(shell pwd)/bin/golangci-lint
golangci-lint: ## Download golangci-lint locally if necessary
	$(call go-get-tool,$(GOLANGCI_LINT),github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.9.0)

# go-get-tool will 'go get' any package $2 and install it to $1.
PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST))))
define go-get-tool
@[ -f $(1) ] || { \
set -e ;\
TMP_DIR=$$(mktemp -d) ;\
cd $$TMP_DIR ;\
go mod init tmp ;\
echo "Downloading $(2)" ;\
GOBIN=$(PROJECT_DIR)/bin go install $(2) ;\
rm -rf $$TMP_DIR ;\
}
endef
