name: Lint

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

jobs:
  lint:
    name: Lint
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Set up Go
        uses: actions/setup-go@v5
        with:
          go-version: '1.26.4'

      - name: Check goimports formatting
        run: |
          go install golang.org/x/tools/cmd/goimports@latest
          goimports_output=$(goimports -local github.com/dynatrace-oss/dtctl -l .)
          if [ -n "$goimports_output" ]; then
            echo "The following files are not properly formatted:"
            echo "$goimports_output"
            echo ""
            echo "Run 'make fmt' to fix formatting."
            exit 1
          fi

      - name: Check go mod tidy
        run: |
          go mod tidy
          if ! git diff --exit-code go.mod go.sum; then
            echo ""
            echo "go.mod or go.sum is not tidy. Run 'go mod tidy' and commit the changes."
            exit 1
          fi

      - name: Run golangci-lint
        uses: golangci/golangci-lint-action@v7
        with:
          version: v2.11.1
