OUT_DIR = bin
BINARY = topsqlagent
IMAGE_NAME="topsql-agent: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

.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: main.go
	GO111MODULE=on CGO_ENABLED=0 GOOS=linux GOARCH=amd64 $(GO_BUILD) -o $(OUT_DIR)/$(BINARY) main.go

### docker-build:	Build the docker image.
.PHONY: docker-build
docker-build: build-bin
	docker build --network host -t ${IMAGE_NAME} .

### 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 ./...

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

### unit:		run unit tests.
.PHONY: unit
unit:
	go test -v ./...

bench:
	@echo "running benchmark test"
	go test -benchmem -bench=".*" ./...
	@echo ""

server:
	@echo "running demo server"
	go run main.go server

