PACKAGES = `go list ./... | grep -v /vendor/`

.PHONY: setup dev lint mocks test update

ENVIRONMENT ?= "dev"
REDIS_HOST ?= 127.0.0.1:6379
USERS_SERVICE_HOST ?= https://dev.users-service.twitch.a2z.com

STATSD_HOST ?= "go-statsd-proxy-a42e356d705f22a4.elb.us-west-2.amazonaws.com:8125"

DB_HOST ?= localhost
DB_PORT ?= 5432
DB_NAME ?= hallpass
DB_SSLMODE ?= disable
DB_MAX_CONNS ?= 50

PDMS_CALLER_ROLE_ARN ?= "arn:aws:iam::895799599216:role/PDMSLambda-CallerRole-18451FI19HSXT"
PDMS_LAMBDA_ARN ?= "arn:aws:lambda:us-west-2:895799599216:function:PDMSLambda-LambdaFunction-IT8I1PE1YR81:live"

setup:
	createdb hallpass

dev-api:
	ENVIRONMENT=$(ENVIRONMENT) \
	REDIS_HOST=$(REDIS_HOST) \
	USERS_SERVICE_HOST=$(USERS_SERVICE_HOST) \
	STATSD_HOST=$(STATSD_HOST) \
	DB_MASTER_HOST=$(DB_HOST) \
	DB_MASTER_PORT=$(DB_PORT) \
	DB_REPLICA_HOST=$(DB_HOST) \
	DB_REPLICA_PORT=$(DB_PORT) \
	DB_NAME=$(DB_NAME) \
	DB_SSLMODE=$(DB_SSLMODE) \
	DB_MAX_CONNS=$(DB_MAX_CONNS) \
	go run cmd/api/main.go

lint:
	golangci-lint run --enable gosec --out-format checkstyle 2>&1 | tee golangci-report.xml

mocks:
	mockery -all -dir=internal/api/ -case=underscore -output=internal/mocks/

test:
	go test -cover -race -v $(PACKAGES)

update:
	dep ensure

TWITCH_CB_AWS = 989470033077

create_hallpass_rds_snapshot_src_production:
	python scripts/validate_aws.py ${TWITCH_CB_AWS}
	aws cloudformation create-stack --template-body file://cloudformation/database/rds_snapshot_src.yaml --cli-input-json file://cloudformation/database/create_rds_snapshot_src_production.json

update_hallpass_rds_snapshot_src_production:
	python scripts/validate_aws.py ${TWITCH_CB_AWS}
	aws cloudformation update-stack --template-body file://cloudformation/database/rds_snapshot_src.yaml --cli-input-json file://cloudformation/database/update_rds_snapshot_src_production.json

create_hallpass_rds_snapshot_dest_production:
	python scripts/validate_aws.py ${TWITCH_CB_AWS}
	aws cloudformation create-stack --template-body file://cloudformation/database/rds_snapshot_dest.yaml --cli-input-json file://cloudformation/database/create_rds_snapshot_dest_production.json

update_hallpass_rds_snapshot_dest_production:
	python scripts/validate_aws.py ${TWITCH_CB_AWS}
	aws cloudformation update-stack --template-body file://cloudformation/database/rds_snapshot_dest.yaml --cli-input-json file://cloudformation/database/update_rds_snapshot_dest_production.json

validate_template:
	aws cloudformation validate-template --template-body file://cloudformation/database/rds_snapshot_src.yaml
	aws cloudformation validate-template --template-body file://cloudformation/database/rds_snapshot_dest.yaml

setup-jenkins:
	@wget -O - -q https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(go env GOPATH)/bin v1.16.0
