GIT_COMMIT ?= $(shell git rev-parse --verify HEAD)
ENVIRONMENT ?= development
MAIN_DIR ?= ./cmd/meepo-util
GOMETALINTER_ARGS ?= --vendor --dupl-threshold=150 --min-confidence=.3 --tests --deadline=90s --disable-all \
	-Egolint -Etest -Eineffassign -Etestify -Eunconvert -Estaticcheck -Egoconst -Eerrcheck \
	-Egofmt -Evet -Edupl -Einterfacer -Estructcheck -Evetshadow -Egosimple -Egoimports -Evarcheck -Emisspell \
	-Emaligned -Etest --exclude=/usr/local/go/src ./...
TEST_PACKAGES ?= $(shell go list ./... | grep -v /vendor/)

build:
	go build $(MAIN_DIR)

# Print info on the current environment.
env:
	go env
	go version
	env

# Format the code.
fix:
	find . -iname '*.go' -not -path '*/vendor/*' -print0 | xargs -0 gofmt -s -w
	find . -iname '*.go' -not -path '*/vendor/*' -print0 | xargs -0 goimports -w

# Run integration tests.
integration_test:
	env ENVIRONMENT=$(ENVIRONMENT) go test -race -tags=integration $(TEST_PACKAGES)

integration_test_cover:
	mkdir -p build/cover
	cd -P . && env ENVIRONMENT=$(ENVIRONMENT) go test -race -tags=integration -coverprofile build/cover/cover.out -coverpkg ./cmd/... $(TEST_PACKAGES)
	@go tool cover -func build/cover/cover.out > build/cover/function_coverage.txt
	@# pipe to cat to swallow the error code
	@grep '\s0.0%' build/cover/function_coverage.txt | grep -v '\smain\s' | cat > build/cover/uncovered_functions.txt
	@if [ -s build/cover/uncovered_functions.txt ]; then (echo "### There are uncovered functions ###" && cat build/cover/uncovered_functions.txt && exit 1) fi;

lint:
	gometalinter $(GOMETALINTER_ARGS)

# Run a build on the build server.
# For meepo-util, we run tests
jenkins: env lint integration_test_cover

# Create a zip file that contains the meepo-util binary, compiled to run on macs.
package: lint integration_test_cover
	GOOS=darwin GOARCH=amd64 go build $(MAIN_DIR)
	zip meepo-util.zip meepo-util

# Useful commands to run before pushing changes for review.
precommit: fix lint integration_test

revendor:
	cd -P . && dep ensure -update

# Run unit tests.
test:
	go test $(TEST_PACKAGES)
