version: '3'

vars:
  EXE: sumitroajiprabowo/go-snmp-olt-zte-c320{{exeExt}}

tasks:
  default:
    cmds:
      - task: dev

  dev:
    desc: Start the local environment
    cmds:
      - docker compose -f docker-compose.local.yaml up -d && air -c .air.toml

  go-install:
    cmds:
      - go install {{.REPO}}

  dl-deps:
    desc: Install tools required to run/build this app
    cmds:
      - task: go-install
        vars: { REPO: github.com/cosmtrek/air@latest }
      - task: tidy

  init:
    desc: Initialize the environment
    deps: [ dl-deps ]

  tidy:
    desc: Clean up dependencies
    cmds:
      - go mod tidy

  app-build:
    desc: Build the app binary
    cmds:
      - CGO_ENABLED=0 go build -o {{.EXE}} ./cmd/api/main.go
    sources:
      - ./**/*.go
    generates:
      - ./{{.EXE}}

  build-image:
    desc: Build the docker image
    cmds:
      - docker build -t {{.EXE}} .

  push-image:
    desc: Push docker image with tag latest with multi-arch support (linux/amd64, linux/arm64) to docker hub
    cmds:
      - docker buildx build --push --platform linux/amd64,linux/arm64,linux/arm/v7 -t {{.EXE}}:latest -f Dockerfile .

  pull-image:
    desc: Pull docker image with tag latest with multi-arch support (linux/amd64, linux/arm64) from docker hub
    cmds:
      - docker pull {{.EXE}}:latest

  docker-run:
    desc: Run docker image with tag latest
    cmds:
      - docker network create local-dev
      - docker run -d --name redis-container --network local-dev -p 6379:6379 redis:7.2
      - docker run -d -p 8081:8081 --name go-snmp-olt-zte-c320 --network local-dev -e REDIS_HOST=redis-container -e REDIS_PORT=6379 -e REDIS_DB=0 -e REDIS_MIN_IDLE_CONNECTIONS=200 -e REDIS_POOL_SIZE=12000 -e REDIS_POOL_TIMEOUT=240 -e SNMP_HOST=192.168.213.174 -e SNMP_PORT=161 -e SNMP_COMMUNITY=homenetro {{.EXE}}:latest

  docker-stop:
    desc: Stop docker image with tag latest
    cmds:
      - docker stop go-snmp-olt-zte-c320 && docker stop redis-container

  docker-remove:
    desc: Remove docker image with tag latest
    cmds:
      - docker network rm local-dev && docker rm go-snmp-olt-zte-c320 && docker rm redis-container

  up:
    desc: Start the docker containers in the background
    cmds:
      - docker compose up -d

  up-rebuild:
    desc: Rebuild the docker containers
    cmds:
      - docker compose up -d --build

  down:
    desc: Stop and remove the docker containers
    cmds:
      - docker compose down

  restart:
    desc: Restart the docker containers
    cmds:
      - task: down
      - task: up

  rebuild:
    desc: Rebuild the docker image and up with detached mode
    cmds:
      - task: down
      - task: up-rebuild