# 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@de0fac2e4500dabe0009e67214ff5f5447ce83dd  # v6.0.2
        with:
          fetch-depth: 1

      - if: ${{ inputs.canary }}
        name: "Init (canary): retrieve GO_VERSION"
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        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@4b73464bb391d4059bd26b0524d20df3927bd417  # v6.3.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
