DEV_AWS_PROFILE=twitch-extensions-configuration-dev
DEV_LAMBDA_FUNCTION=DevPdmsLambda-LambdaHandler212865DC-1DY6S9V1ZI944
DEV_S3BUCKET=devs3bucket-s3bucket07682993-wdndr9i7j0hb# as defined by CDK after deploying the s3 stack
DEV_S3CODEFILE=lambda_go_build.zip# as used in CDK to bootstrap the lambda

PROD_AWS_PROFILE=twitch-extensions-configuration-prod
PROD_LAMBDA_FUNCTION=ProdPdmsLambda-LambdaHandler212865DC-1FKHOBUZH5AGT
PROD_S3BUCKET=prods3bucket-s3bucket07682993-1slqfuzzaehhb# as defined by CDK after deploying the s3 stack
PROD_S3CODEFILE=lambda_go_build.zip# as used in CDK to bootstrap the lambda

BUILD_FOLDER=build
BUILD_ZIPFILE=main.zip

# Dockerfile is used for CI server (Jenkins), through all the "make <task>-docker" tasks.
# If you have docker installed in your machine, you can also run those tasks to ensure same binary versions are used.
DOCKER_IMAGE=config_service_pdms_lambda
DOCKER_VOLUME=/build/go/src/code.justin.tv/devhub/config-service-pdms-lambda
define docker_run
	@docker run -v $(PWD):$(DOCKER_VOLUME):delegated -w $(DOCKER_VOLUME) --rm $(DOCKER_IMAGE) /bin/bash -c "echo \"Running in Docker: $(1)\" && $(1)"
endef

docker-image:
	# Build docker image from Dockerfile. Installing required binaries for "make <task>-docker" tasks
	docker image build -t $(DOCKER_IMAGE) .

dep:
	# Generate vendor files from Gopkg and code imports.
	dep ensure

lint:
	# Using default linters (see golangci-lint help linters) (if you need to install: brew install golangci/tap/golangci-lint)
	golangci-lint run

lint-docker: docker-image
	$(call docker_run, make lint)

test:
	go test ./...

test-docker: docker-image
	$(call docker_run, make test)

build:
	echo "Build into $(BUILD_FOLDER)/$(BUILD_ZIPFILE) for Lambda"
	GOOS=linux go build -o $(BUILD_FOLDER)/main main.go
	cd $(BUILD_FOLDER) && zip $(BUILD_ZIPFILE) ./main
.PHONY: build

push-dev:
	# Force push current branch to develop branch, which triggers a deploy in Jenkins
	CURRENT_BRANCH=$$(git rev-parse --abbrev-ref HEAD) ;\
	echo ">> Push $$CURRENT_BRANCH to origin/develop" ;\
	echo ">> Jenkins deploy: https://jenkins.internal.justin.tv/job/devhub/job/config-service-pdms-lambda/job/develop/" ;\
	git push --force origin $$CURRENT_BRANCH:develop

push-prod:
	echo ">> Please make a PR and merge in master. Jenkins will auto-deploy to production when merged in origin/master"

deploy-dev: build
	echo "Upload $(BUILD_FOLDER)/$(BUILD_ZIPFILE) to Lambda"
	aws lambda update-function-code \
		--function-name $(DEV_LAMBDA_FUNCTION) \
		--zip-file fileb://$(BUILD_FOLDER)/$(BUILD_ZIPFILE) \
		--profile=$(DEV_AWS_PROFILE)

deploy-prod: build
	echo "Upload $(BUILD_FOLDER)/$(BUILD_ZIPFILE) to Lambda"
	aws lambda update-function-code \
		--function-name $(PROD_LAMBDA_FUNCTION) \
		--zip-file fileb://$(BUILD_FOLDER)/$(BUILD_ZIPFILE) \
		--profile=$(PROD_AWS_PROFILE)

deploy-dev-jenkins:
	echo "Build from Docker to use the right Golang version"
	$(call docker_run, make build)

	echo "Upload $(BUILD_FOLDER)/$(BUILD_ZIPFILE) to Lambda using profile and region from Jenkinsfile withAWS block"
	aws lambda update-function-code \
		--function-name $(DEV_LAMBDA_FUNCTION) \
		--zip-file fileb://$(BUILD_FOLDER)/$(BUILD_ZIPFILE)

	echo "Upload $(BUILD_FOLDER)/$(BUILD_ZIPFILE) to S3 (in case there is a CDK/CFN infra re-build)"
	aws s3 cp ./$(BUILD_FOLDER)/$(BUILD_ZIPFILE) s3://$(DEV_S3BUCKET)/$(DEV_S3CODEFILE)

deploy-prod-jenkins:
	echo "Build from Docker to use the right Golang version"
	$(call docker_run, make build)

	echo "Upload $(BUILD_FOLDER)/$(BUILD_ZIPFILE) to Lambda using profile and region from Jenkinsfile withAWS block"
	aws lambda update-function-code \
		--function-name $(PROD_LAMBDA_FUNCTION) \
		--zip-file fileb://$(BUILD_FOLDER)/$(BUILD_ZIPFILE)

	echo "Upload $(BUILD_FOLDER)/$(BUILD_ZIPFILE) to S3 (in case there is a CDK/CFN infra re-build)"
	aws s3 cp ./$(BUILD_FOLDER)/$(BUILD_ZIPFILE) s3://$(PROD_S3BUCKET)/$(PROD_S3CODEFILE)

