name: Cube CLI CI

on:
  push:
    branches: [master]
    paths:
      - 'rust/cube-cli/**'
      - 'install-cli.sh'
      - 'install-cli.ps1'
      - '.github/workflows/cube-cli.yml'
  pull_request:
    paths:
      - 'rust/cube-cli/**'
      - 'install-cli.sh'
      - 'install-cli.ps1'
      - '.github/workflows/cube-cli.yml'

concurrency:
  group: cube-cli-ci-${{ github.ref }}
  cancel-in-progress: true

permissions:
  contents: read

jobs:
  lint:
    runs-on: ubuntu-22.04
    defaults:
      run:
        working-directory: rust/cube-cli
    steps:
      - uses: actions/checkout@v4
      - name: Setup Rust toolchain
        uses: actions-rust-lang/setup-rust-toolchain@v1
        with:
          components: rustfmt, clippy
          cache-workspaces: rust/cube-cli
      - name: Check formatting
        run: cargo fmt --all --check
      - name: Clippy
        run: cargo clippy --all-targets -- -D warnings

  build-and-test:
    name: Build & test ${{ matrix.target }}
    runs-on: ${{ matrix.os }}
    timeout-minutes: 60
    strategy:
      fail-fast: false
      matrix:
        include:
          # Same targets the release builds in publish.yml, so a broken
          # cross-platform build is caught in the PR rather than at release.
          # Linux — fully static single binaries (musl + rustls, no OpenSSL).
          - os: ubuntu-22.04
            target: x86_64-unknown-linux-musl
          - os: ubuntu-24.04-arm
            target: aarch64-unknown-linux-musl
          # macOS — Intel and Apple Silicon.
          - os: macos-15-intel
            target: x86_64-apple-darwin
          - os: macos-14
            target: aarch64-apple-darwin
          # Windows.
          - os: windows-2022
            target: x86_64-pc-windows-msvc
    defaults:
      run:
        working-directory: rust/cube-cli
    steps:
      - uses: actions/checkout@v4
      - name: Install musl tools
        if: endsWith(matrix.target, '-musl')
        run: sudo apt-get update && sudo apt-get install -y musl-tools
      - name: Setup Rust toolchain
        uses: actions-rust-lang/setup-rust-toolchain@v1
        with:
          target: ${{ matrix.target }}
          cache-workspaces: rust/cube-cli
      - name: Test
        run: cargo test --locked --target ${{ matrix.target }}
      - name: Build (release)
        run: cargo build --release --locked --target ${{ matrix.target }}
