.PHONY: setup build test coverage lint vendor generate show-coverage service-container pull-build-image push-image integrationtest build-image artifact _artifact

GIT_COMMIT=$(shell git rev-parse HEAD)
WORKER_IMAGE=docker-registry.internal.justin.tv/web/users-service-worker:${GIT_COMMIT}
SERVER_IMAGE=docker-registry.internal.justin.tv/web/users-service-server:${GIT_COMMIT}
BUILD_IMAGE=docker-registry.internal.justin.tv/web/users-service-build:latest

setup:
	which go || (echo "install go" && exit 1)
	which glide || (echo "install glide" && exit 1)
	go get github.com/sgotti/glide-vc
	go get github.com/vektra/mockery/...
	go get -u github.com/jstemmer/go-junit-report

build:
	go build

coverage:
	./coverage.sh

integrationtest: artifact
	./build/integrate.sh

test: lint coverage

artifact:
	docker run --rm -v ${PWD}:/go/src/code.justin.tv/web/users-service ${BUILD_IMAGE} make _artifact

_artifact:
	mkdir -p ./artifact/
	cp -R deploy/. artifact/
	cp -R courier artifact/
	go build -o ./artifact/users-service

test-in-ci: test-in-container integrationtest push-image

test-in-container: pull-build-image
	docker run --rm -v ${PWD}:/go/src/code.justin.tv/web/users-service ${BUILD_IMAGE} make test

pull-build-image:
	docker pull ${BUILD_IMAGE}

build-image:
	docker build --build-arg STARTUP=server -f run.Dockerfile -t ${SERVER_IMAGE} .
	docker build --build-arg STARTUP=worker -f run.Dockerfile -t ${WORKER_IMAGE} .

push-image: build-image
	docker push ${SERVER_IMAGE}
	docker push ${WORKER_IMAGE}

lint:
	go vet ./...
	errcheck -blank ./...
	golint ./...

vendor:
	glide update --strip-vendor
	glide-vc --only-code --no-tests --use-lock-file
	# Remove vendored main.go's
	find ./__vendor -type f -name main.go | xargs rm

generate:
	go generate ./...

show-coverage:
	./coverage.sh
	go tool cover -html=coverage.out
