OUT_DIR = bin
BINARY = sli-reporter
IMAGE_NAME="sli-reporter:local"
LOCAL_REGISTRY="localhost:5000"

GO = go
GO_PATH = $$($(GO) env GOPATH)
GO_BUILD = $(GO) build
GO_GET = $(GO) get
GO_INSTALL = $(GO) install
GO_TEST = $(GO) test
GO_LINT = $(GO_PATH)/bin/golangci-lint

ifeq ($(shell uname -m),aarch64)
	ARCH=arm64
else
	ARCH=amd64
endif

.PHONY: build
build: build-reporter tidy
	docker build . -t ${IMAGE_NAME}

.PHONY: build-reporter
build-reporter:  cmd/main.go
	GO111MODULE=on CGO_ENABLED=0 GOOS=linux GOARCH=$(ARCH) $(GO_BUILD) -ldflags="-s -w" -o $(OUT_DIR)/$(BINARY) cmd/main.go

.PHONY: tidy
tidy:
	go mod tidy

setup: build
	./scripts/setup.sh

.PHONY: deps
deps: install-lint
	$(GO_GET) -v -t -d ./...

.PHONY: test
test: 
	go test -v -cover ./...

.PHONY: install-lint
install-lint:
	$(GO_INSTALL) github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.0.2

.PHONY: lint
lint: install-lint
	$(GO_LINT) run -v --timeout 5m ./...

.PHONY: lint-fix
lint-fix: install-lint
	$(GO_LINT) run -v --fix ./...

### update-local:
.PHONY: update-local
update-local: kind-up build
	docker tag $(IMAGE_NAME) $(LOCAL_REGISTRY)/$(IMAGE_NAME)
	docker push $(LOCAL_REGISTRY)/$(IMAGE_NAME)

### kind-up:		Launch a Kubernetes cluster with a image registry by Kind.
.PHONY: kind-up
kind-up:
	./scripts/kind-with-registry.sh

### kind-down:		Delete the Kubernetes cluster created by "make kind-up".
.PHONY: kind-down
kind-down:
	kind delete cluster --name reporter

### help:			Show Makefile commands.
.PHONY: help
help:
	@grep -E '^### [-A-Za-z0-9_]+:' Makefile | sed 's/###//'
