OUT_DIR = bin
BINARY = reporter
IMAGE_NAME="billing-reporter:local"

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

GITSHASYM="tidbcloud/billing-reporter/pkg/version.GitHash"
GIT_SHA=$(shell git rev-parse HEAD)
GO_LDFLAGS ?= "-w -s -X=$(GITSHASYM)=$(GIT_SHA)"

.PHONY: build
build: build-bin docker-build

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

### build-bin:		Build the binary file.
.PHONY: build-bin
build-bin: cmd/main.go
	GO111MODULE=on CGO_ENABLED=0 GOOS=linux GOARCH=amd64 $(GO_BUILD) -ldflags $(GO_LDFLAGS) -o $(OUT_DIR)/$(BINARY) cmd/main.go
	# $(MAKE) -C collector apiserver
### docker-build:	Build the docker image.
.PHONY: docker-build
docker-build: build-bin
	docker build -t ${IMAGE_NAME} .
	# $(MAKE) -C collector build

### 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
	
### lint:		lint the code.
.PHONY: lint
lint: tools
	$(GO_LINT) run -v --timeout 5m ./...

### fix:		fix lint issues.
.PHONY: fix
fix: tools
	$(GO_LINT) run -v --fix ./...

### unit:		Run unit tests.
.PHONY: unit
unit:
	$(GO_TEST) ./... -coverprofile=coverage.txt -covermode=atomic
