# The old school Makefile, following are required targets. The Makefile is written
# to allow building multiple binaries. You are free to add more targets or change
# existing implementations, as long as the semantics are preserved.
#
#   make              - default to 'build' target
#   make lint         - code analysis
#   make test         - run unit test (or plus integration test)
#   make build        - alias to build-local target
#   make build-local  - build local binary targets
#   make container    - build containers
#   $ docker login registry -u username -p xxxxx
#   make push         - push containers
#   make clean        - clean up targets
#
# The makefile is also responsible to populate project version information.
#

#
# Tweak the variables based on your project.
#

# This repo's root import path (under GOPATH).
ROOT := code.byted.org/infcp/vecps-cli

# Module name.
NAME ?= vecps

# Container image prefix and suffix added to targets.
# The final built images are:
#   $[REGISTRY]/$[IMAGE_PREFIX]$[TARGET]$[IMAGE_SUFFIX]:$[VERSION]
# $[REGISTRY] is an item from $[REGISTRIES], $[TARGET] is an item from $[TARGETS].
IMAGE_PREFIX ?= $(strip )
IMAGE_SUFFIX ?= $(strip )

# Container registries.
REGISTRY ?= hub.byted.org/infcprelease

# Container registry for base images.
BASE_REGISTRY ?= hub.byted.org/infcplibrary

# Helm chart repo
CHART_REPO ?= charts

#
# These variables should not need tweaking.
#

# It's necessary to set this because some environments don't link sh -> bash.
export SHELL := /bin/bash

# It's necessary to set the errexit flags for the bash shell.
export SHELLOPTS := errexit

# Project main package location.
CMD_DIR := ./cmd

# Project output directory.
OUTPUT_DIR := ./bin

# Build directory.
BUILD_DIR := ./build

TAGS ?=

IMAGE_NAME := $(IMAGE_PREFIX)$(NAME)$(IMAGE_SUFFIX)

# Current version of the project.
VERSION      ?= $(shell git describe --tags --always --dirty)
BRANCH       ?= $(shell git branch | grep \* | cut -d ' ' -f2)
GITCOMMIT    ?= $(shell git rev-parse HEAD)
GITTREESTATE ?= $(if $(shell git status --porcelain),dirty,clean)
BUILDDATE    ?= $(shell date +"%Y-%m-%dT%H:%M:%SZ")
appVersion   ?= $(VERSION)

# Track code version with Docker Label.
DOCKER_LABELS ?= git-describe="$(shell date -u +v%Y%m%d)-$(shell git describe --tags --always --dirty)"

# Golang standard bin directory.
GOPATH ?= $(shell go env GOPATH)
BIN_DIR := $(GOPATH)/bin

# Default golang flags used in build and test
# -mod=vendor: force go to use the vendor files instead of using the `$GOPATH/pkg/mod`
# -p: the number of programs that can be run in parallel
# -count: run each test and benchmark 1 times. Set this flag to disable test cache
export GOFLAGS ?= -mod=vendor -count=1

.PHONY: build
build: build-local

# more info about `GOGC` env: https://github.com/golangci/golangci-lint#memory-usage-of-golangci-lint
.PHONY: lint
lint: $(HELM_LINT)
	golangci-lint run

.PHONY: gen
gen:
	go generate ./...

.PHONY: snapshot
snapshot:
	goreleaser release --clean --snapshot -p 1

.PHONY: release
release:
	goreleaser release --clean --auto-snapshot -p 1

.PHONY: build-local
build-local:
	@go build -v -o $(OUTPUT_DIR)/$(NAME) -tags="$(TAGS)"                  \
	  -ldflags "-s -w -X $(ROOT)/pkg/version.module=$(NAME)                \
	    -X $(ROOT)/pkg/version.version=$(VERSION)                          \
	    -X $(ROOT)/pkg/version.branch=$(BRANCH)                            \
	    -X $(ROOT)/pkg/version.gitCommit=$(GITCOMMIT)                      \
	    -X $(ROOT)/pkg/version.gitTreeState=$(GITTREESTATE)                \
	    -X $(ROOT)/pkg/version.buildDate=$(BUILDDATE)"                     \
	  $(CMD_DIR);

.PHONY: container
container:
	@docker build -t $(REGISTRY)/$(IMAGE_NAME):$(VERSION)                  \
	  --label $(DOCKER_LABELS)                                             \
	  -f $(BUILD_DIR)/Dockerfile .;

.PHONY: push
push: container
	@docker push $(REGISTRY)/$(IMAGE_NAME):$(VERSION);

.PHONY: clean
clean:
	@-rm -vrf ${OUTPUT_DIR}
