name: 'staticcheck'
on:
  pull_request:
    paths: ['**.go', 'go.mod', '.github/workflows/*']
  push:
    branches: ['main', 'master']

jobs:
  staticcheck:
    name:    'staticcheck'
    runs-on: 'ubuntu-latest'
    env:     {cache: 'staticcheck-${{ github.ref }}'}
    steps:
      # Setup
      - uses: 'actions/checkout@v6'
      - id:   'cache-restore'
        uses: 'actions/cache/restore@v5'
        with:
          key: '${{ env.cache }}'
          path: |
            ${{ runner.temp }}/staticcheck
            /home/runner/.cache/go-build
          restore-keys: |
            staticcheck-${{ github.ref }}
            staticcheck-refs/heads/main
      - uses: 'actions/setup-go@v6'
        with: {go-version: 'stable'}
      - uses: 'actions/cache@v5'
        with:
          key: '${{ runner.os }}-staticcheck'
          path: |
            ${{ runner.temp }}/staticcheck
            ${{ steps.install_go.outputs.GOCACHE || '' }}

      # Run
      - run: |
          export STATICCHECK_CACHE="${{ runner.temp }}/staticcheck"
          go install honnef.co/go/tools/cmd/staticcheck@latest

          fail=0
          for a in $(go tool dist list); do
            export GOOS=${a%%/*}
            export GOARCH=${a#*/}

            case "$GOOS" in
              (android|ios)  continue ;; # Requires cgo to link.
            esac

            f=
            echo "==> $a"
            go vet ./...      || fail=1
            staticcheck ./... || fail=1
          done
          exit $fail

      # Store cache
      - name: 'delete existing cache'
        if:   '${{ steps.cache-restore.outputs.cache-hit }}'
        env:  {GH_TOKEN: '${{ secrets.GITHUB_TOKEN }}'}
        continue-on-error: true
        run: |
          gh extension install actions/gh-actions-cache
          gh actions-cache delete "${{ env.cache }}" --confirm
      - uses: 'actions/cache/save@v5'
        with:
          key: '${{ env.cache }}'
          path: |
            ${{ runner.temp }}/staticcheck
            /home/runner/.cache/go-build
