# This job runs golangci-lint
# Note that technically, `make lint-go-all` would run the linter for all targets, and could be called once, on a single instance.
# The point of running it on a matrix instead, each GOOS separately, is to verify that the tooling itself is working on the target OS.
name: job-lint-go

on:
  workflow_call:
    inputs:
      timeout:
        required: true
        type: number
      go-version:
        required: true
        type: string
      runner:
        required: true
        type: string
      canary:
        required: false
        default: false
        type: boolean
      goos:
        required: true
        type: string

env:
  GOTOOLCHAIN: local

jobs:
  lint-go:
    name: ${{ format('{0}{1}', inputs.goos, inputs.canary && ' (go canary)' || '') }}
    timeout-minutes: ${{ inputs.timeout }}
    runs-on: "${{ inputs.runner }}"
    defaults:
      run:
        shell: bash
    env:
      GO_VERSION: ${{ inputs.go-version }}

    steps:
      - name: "Init: checkout"
        uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683  # v4.2.2
        with:
          fetch-depth: 1

      - if: ${{ inputs.canary }}
        name: "Init (canary): retrieve GO_VERSION"
        run: |
          latest_go="$(. ./hack/provisioning/version/fetch.sh; go::canary::for::go-setup)"
          printf "GO_VERSION=%s\n" "$latest_go" >> "$GITHUB_ENV"
          [ "$latest_go" != "" ] || \
            echo "::warning title=No canary go::There is currently no canary go version to test. Steps will not run."

      - if: ${{ env.GO_VERSION != '' }}
        name: "Init: install go"
        uses: actions/setup-go@d35c59abb061a4a6fb18e82ac0862c26744d6ab5  # v5.5.0
        with:
          go-version: ${{ env.GO_VERSION }}
          check-latest: true

      - if: ${{ env.GO_VERSION != '' }}
        name: "Init: install dev-tools"
        run: |
          echo "::group:: make install-dev-tools"
          make install-dev-tools
          echo "::endgroup::"

      - if: ${{ env.GO_VERSION != '' }}
        name: "Run"
        run: |
          # On canary, lint for all supported targets
          if [ "${{ inputs.canary }}" == "true" ]; then
            NO_COLOR=true make lint-go-all
          else
            NO_COLOR=true GOOS="${{ inputs.goos }}" make lint-go
          fi
