SHELL := /bin/bash

novendor = $(shell go list ./... | grep -v vendor/ | grep -v integration_test)

.PHONY: test mocks

all: release

# Runs all tasks that need to be done as part of a release. Think 'bb release"...
release: verify install

# Run all static checks, must run dev-setup first  
lint:
	golangci-lint run -v --timeout 5m --out-format checkstyle > golangci-report.xml

# Run tests
test:
	go test $(novendor)

verify: format lint test

# This replaces retool - run this command to set up developer tools. 
dev-setup: get-tools install-tools 

get-tools:
	cat tools/tools.go | grep _ | awk -F'"' '{print $$2}' | xargs -tI % go get %

install-tools:
	cat tools/tools.go | grep _ | awk -F'"' '{print $$2}' | xargs -tI % go install %

integration-test-dev:
	AWS_PROFILE=rex-staging go test ./integration_test -testEndpoint=http://localhost:8000

integration-test:
	go test -run W ./integration_test -testEndpoint=http://staging-rex.us-west-2.elasticbeanstalk.com/

format: 
	golangci-lint run --fix

test-coverage:
	CGO_ENABLED=1 gocov test -race -v $(novendor) | gocov-xml > coverage.xml

# Build the executable
install:
	go build -mod=vendor -o bin/rex cmd/rex/*.go

dev:
	AWS_PROFILE=rex-staging go run cmd/rex/main.go

mod:
	go mod vendor

# TWIRP
twirp: mod generate build-twirp

generate:
	cd rpc; \
	protoc --proto_path=$$GOPATH/src:. --twirp_out=. --go_out=. rex_service.proto

build-twirp:
	go build code.justin.tv/samus/rex/rpc

clean:
	rm twirp/*.pb.go
	rm twirp/*.twirp.go

### MANTA TASKS ###

# Kick off the full manta build
manta_bootstrap: manta_cleanup
	manta -v -f build.json

# Task that is run inside of the manta build to actually build the service
manta: install prune migrate

# Remove all build artifacts that we don't care about to minimize distro size
prune:
	find . -mindepth 1 -maxdepth 1 -not -name bin -not -name config -not -name courier -not -name campaign -exec rm -rf {} \;

# Remove manta artifacts
manta_cleanup:
	rm -rf .manta/bin
	rm -rf .manta/courier
	rm -rf .manta/config
	rm -rf .manta/campaign

# Move files that need to be manually included in the manta build
migrate:
	mv -f bin/rex .

manta_integration_tests: integration-test

### MOCKS ###

# When adding a new mock, the name input has to be equivalent to the name of the interface you want mocked
mocks:
	mockery --dir=metrics/ --name IMetricLogger
	mockery --dir=vendor/github.com/aws/aws-sdk-go/service/cloudwatch/cloudwatchiface --name CloudWatchAPI
	mockery --dir=internal/app/status/ --name OfferStatus
	mockery --dir=internal/app/gifting/ --name Gifting
	mockery --dir=internal/clients/status/ --name IStatusClient
	mockery --dir=internal/clients/gifting/ --name IGiftingClient
	mockery --dir=internal/clients/nitro/ --name INitroClient
	mockery --dir=internal/clients/notifications/ --name INotificationsClient
	mockery --dir=vendor/code.justin.tv/samus/gateway/client --output ./mocks/samus_gateway/ --name Client

	go generate $(novendor)
