version: '3'

vars:
  DEV_REPO: us-west2-docker.pkg.dev/flowy-dev-440017/deployments
  PROD_REPO: us-west2-docker.pkg.dev/flowy-prod-440017/deployments

tasks:
  generate:
    desc: Generate protobuf code and run go generate
    cmds:
      - task: genproto
      - go generate ./...

  format:
    desc: Format go files
    cmds:
      - go fmt ./...

  genproto:
    desc: Generate Go code from .proto files via docker
    vars:
      PROTO_DIR: '{{.PROTO_DIR | default "./protocol"}}'
      OUT_DIR: '{{.OUT_DIR | default "./genproto"}}'
    cmds:
      - 'echo "Proto directory: {{.PROTO_DIR}}"'
      - 'echo "Output directory: {{.OUT_DIR}}"'
      - rm -rf {{.OUT_DIR}}
      - mkdir -p {{.OUT_DIR}}
      - |
        docker run --rm \
          -v "{{.PROTO_DIR}}/":/protocol \
          -v "{{.OUT_DIR}}":/genproto \
          --workdir / \
          talksik/golang-protoc:latest \
          sh -c 'protoc --proto_path=./protocol \
                        --go_out=./genproto \
                        --go_opt=paths=source_relative \
                        --go-grpc_out=./genproto \
                        --go-grpc_opt=paths=source_relative \
                        $(find ./protocol -name "*.proto")'

  test:
    desc: Run tests with pretty output via gotestfmt
    cmds:
      - go test -json ./... | docker run -i ghcr.io/gotesttools/gotestfmt:latest

  create-migration:
    desc: 'Create new migration (usage: task create-migration ARG=create_users_table)'
    cmds:
      - 'echo "Creating migration with name {{.ARG}}"'
      - docker run -v ./migrations:/migrations --network host migrate/migrate create -ext sql -seq -dir /migrations {{.ARG}}

  dev-database:
    desc: Open psql shell against the dev database
    interactive: true
    cmds:
      - |
        kubectl run postgres-client \
          --rm -it --image=postgres:latest \
          --env="LLINK_POSTGRES_CONNECTION_URL=$(kubectl get secret shared-secrets -o jsonpath='{.data.LLINK_POSTGRES_CONNECTION_URL}' --context dev | base64 --decode)" \
          --context dev \
          --command -- /bin/bash -c "psql \$LLINK_POSTGRES_CONNECTION_URL"

  prod-database:
    desc: Open psql shell against the prod database
    interactive: true
    cmds:
      - |
        kubectl run postgres-client \
          --rm -it --image=postgres:latest \
          --env="LLINK_POSTGRES_CONNECTION_URL=$(kubectl get secret shared-secrets -o jsonpath='{.data.LLINK_POSTGRES_CONNECTION_URL}' --context prod | base64 --decode)" \
          --context prod \
          --command -- /bin/bash -c "psql \$LLINK_POSTGRES_CONNECTION_URL"

  migrate-dev:
    desc: Run migrations against dev cluster
    cmds:
      - kubectl delete job migrations --ignore-not-found --context=dev
      - SKAFFOLD_DEFAULT_REPO={{.DEV_REPO}} skaffold run -p migrations --kube-context dev --tail

  migrate-prod:
    desc: Run migrations against prod cluster
    cmds:
      - kubectl delete job migrations --ignore-not-found --context=prod
      - SKAFFOLD_DEFAULT_REPO={{.PROD_REPO}} skaffold run -p migrations --kube-context prod --tail

  deploy-dev:
    desc: 'Deploy to dev (optionally MODULE=orion to deploy a single service)'
    cmds:
      - task: generate
      - task: test
      - SKAFFOLD_DEFAULT_REPO={{.DEV_REPO}} skaffold run -p dev {{if .MODULE}}-m {{.MODULE}}{{end}} --kube-context dev --port-forward --tail

  deploy-prod:
    desc: 'Deploy to prod (optionally MODULE=orion to deploy a single service)'
    cmds:
      - task: generate
      - task: test
      - SKAFFOLD_DEFAULT_REPO={{.PROD_REPO}} skaffold run -p prod {{if .MODULE}}-m {{.MODULE}}{{end}} --kube-context prod --port-forward --tail
