# ENV must be prod or it becomes dev by default.
ifneq ($(ENV),prod)
	ENV=dev
endif

AWS_BUCKET_NAME=twitch-grafana-$(ENV)-annotation-gateway
AWS_PROFILE_REGION=--profile=twitch-grafana-$(ENV) --region=us-west-2
STACK_NAME=grafana-annotations-gateway

all: gateway

gateway: *.go
		GOOS=linux go build -o gateway .

test:
		golangci-lint run --enable-all .
		go test ./... --cover

package: package-$(ENV).yaml

package-$(ENV).yaml: gateway template.yaml
		aws $(AWS_PROFILE_REGION) cloudformation package \
			--template-file template.yaml \
			--s3-bucket $(AWS_BUCKET_NAME) \
			--output-template-file package-$(ENV).yaml

deploy: package
		aws $(AWS_PROFILE_REGION) cloudformation deploy \
			--template-file package-$(ENV).yaml \
			--parameter-overrides `grep -Ev '^\s*(#|$$)' params-$(ENV).json` \
			--capabilities CAPABILITY_NAMED_IAM \
			--stack-name $(STACK_NAME)

describe:
		@aws $(AWS_PROFILE_REGION) cloudformation describe-stacks --stack-name $(STACK_NAME)

outputs:
		@make describe | jq -r '.Stacks[0].Outputs'
