# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

version: "3"

dotenv: [".env"]

tasks:
  update-gitmodules:
    cmds:
      - git submodule update --recursive --remote ../pcap-cli

  docker-build:
    requires:
      vars: [ENV]
    vars:
      RUNTIME_ENVIRONMENT: '{{default "cloud_run_gen2" .ENV}}'
    dotenv:
      - "{{.USER_WORKING_DIR}}/../env/{{.RUNTIME_ENVIRONMENT}}.env"
    cmds:
      - task: update-gitmodules
      - >-
        docker buildx build
        --platform=linux/amd64
        --no-cache --file=Dockerfile
        --build-arg="BIN_NAME=$TCPDUMPW_BIN_NAME"
        --build-arg="LIBPCAP_VERSION=$LIBPCAP_VERSION"
        --build-arg="TCPDUMP_VERSION=$TCPDUMP_VERSION"
        --output={{.USER_WORKING_DIR}}/bin
        --target=releaser {{.USER_WORKING_DIR}}
    sources:
      - go.mod
      - go.sum
      - main.go
      - pkk/**/*.go

  go-fmt:
    cmds:
      - gofumpt -l -w .

  go-gen:
    cmds:
      - go generate ./...

  local-build:
    dotenv: [".env"]
    cmds:
      - task: update-gitmodules
      - task: go-fmt
      - task: go-gen
      - go mod tidy -compat=$GOLANG_VERSION
      - >-
        go build
        -tags json
        -o bin/$TCPDUMPW_BIN_NAME
        {{if .VERBOSE}}-v -a{{end}}
        main.go
    sources:
      - go.mod
      - go.sum
      - main.go
      - pkk/**/*.go

  local-dist:
    cmds:
      - task: build
        vars:
          VERBOSE: true
