# Maintained in https://github.com/coreos/repo-templates
# Do not edit downstream.

name: Go
on:
  push:
    branches: [main]
  pull_request:
    branches: [main]
permissions:
  contents: read

# don't waste job slots on superseded code
concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  test:
    name: Test
    strategy:
      matrix:
        go-version: [1.23.x, 1.24.x]
    runs-on: ubuntu-latest
    steps:
    - name: Set up Go 1.x
      uses: actions/setup-go@v6
      with:
        go-version: ${{ matrix.go-version }}
    - name: Check out repository
      uses: actions/checkout@v5
    - name: Install Linux dependencies
      if: runner.os == 'Linux'
      run: |
        sudo apt-get update
        sudo apt-get install libblkid-dev
    - name: Check modules
      run: go mod verify
    - name: Build
      run: ./build
    - name: Test
      run: ./test
    - name: Check Go formatting (gofmt)
      shell: bash
      run: |
        GO_FILES=$(find . -name '*.go' -not -path "./vendor/*")
        UNFORMATTED_FILES=$(gofmt -l $GO_FILES)
        if [ -n "$UNFORMATTED_FILES" ]; then
          echo "Go files are not formatted. Please run 'gofmt -w .' on your code."
          gofmt -d $UNFORMATTED_FILES
          exit 1
        fi
        echo "All Go files are correctly formatted."
    - name: Run linter
      uses: golangci/golangci-lint-action@v8
      with:
        version: v2.1.6
  regenerate:
    name: Regenerate
    runs-on: ubuntu-latest
    steps:
    - name: Check out repository
      uses: actions/checkout@v5
    - name: Set up Go 1.x
      uses: actions/setup-go@v6
      with:
        go-version: 1.24.x
    - name: Install schematyper
      run: |
        # "go install github.com/idubinskiy/schematyper:latest" fails with
        # current Go.  Use fix from fork.  We can't "go install" directly from
        # the fork; it complains about mismatched package paths.
        # https://github.com/idubinskiy/schematyper/pull/22
        git clone -b gomod https://github.com/bgilbert/schematyper
        cd schematyper
        go install .
    - name: Regenerate
      run: ./generate
    - name: Check whether generated output is current
      run: |
        if [ -n "$(git status --porcelain config docs)" ]; then
          echo "Found local changes after regenerating:"
          git --no-pager diff --color=always config docs
          echo "Rerun './generate'."
          exit 1
        fi
