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}

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

reporter:
	GO111MODULE=on  go build -ldflags="-s -w" -o bin/sli-reporter cmd/main.go

tidy:
	go mod tidy

setup: build
	./scripts/setup.sh

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

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

### tools: 		Install CI tools.
.PHONY: tools
tools:
	mkdir -p $(GO_PATH)/bin
	$(GO_LINT) version || curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GO_PATH)/bin

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

.PHONY: fix
fix: tools
	$(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/###//'
