ftdc 0.1.5

Crate to download ftdc data for mongodb clusters.
Documentation
name: Release

on:
  push:
    tags:
      - 'v*'

env:
  CARGO_TERM_COLOR: always

jobs:
  build:
    name: Build ${{ matrix.target }}
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        include:
          - target: x86_64-apple-darwin
            os: macos-latest
            archive: ftdc-${{ github.ref_name }}-darwin-amd64.tar.gz
          - target: aarch64-apple-darwin
            os: macos-latest
            archive: ftdc-${{ github.ref_name }}-darwin-arm64.tar.gz
          - target: x86_64-unknown-linux-gnu
            os: ubuntu-latest
            archive: ftdc-${{ github.ref_name }}-linux-amd64.tar.gz
          - target: aarch64-unknown-linux-gnu
            os: ubuntu-24.04-arm
            archive: ftdc-${{ github.ref_name }}-linux-arm64.tar.gz
          - target: x86_64-pc-windows-msvc
            os: windows-latest
            archive: ftdc-${{ github.ref_name }}-windows-amd64.zip

    steps:
      - uses: actions/checkout@v4

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable
        with:
          targets: ${{ matrix.target }}

      - name: Build release binary
        run: cargo build --release --features build-binary --target ${{ matrix.target }}

      - name: Create archive (Unix)
        if: runner.os != 'Windows'
        run: |
          cd target/${{ matrix.target }}/release
          tar -czvf ../../../${{ matrix.archive }} ftdc
          cd ../../..

      - name: Create archive (Windows)
        if: runner.os == 'Windows'
        shell: pwsh
        run: |
          Compress-Archive -Path "target/${{ matrix.target }}/release/ftdc.exe" -DestinationPath "${{ matrix.archive }}"

      - name: Generate SHA256 checksum (Unix)
        if: runner.os != 'Windows'
        run: |
          if [[ "$RUNNER_OS" == "macOS" ]]; then
            SHA=$(shasum -a 256 ${{ matrix.archive }} | cut -d ' ' -f 1)
          else
            SHA=$(sha256sum ${{ matrix.archive }} | cut -d ' ' -f 1)
          fi
          echo "$SHA  ${{ matrix.archive }}" > ${{ matrix.archive }}.sha256

      - name: Generate SHA256 checksum (Windows)
        if: runner.os == 'Windows'
        shell: pwsh
        run: |
          $hash = (Get-FileHash -Algorithm SHA256 "${{ matrix.archive }}").Hash.ToLower()
          "$hash  ${{ matrix.archive }}" | Out-File -FilePath "${{ matrix.archive }}.sha256" -Encoding utf8

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.archive }}
          path: |
            ${{ matrix.archive }}
            ${{ matrix.archive }}.sha256

  publish-crate:
    name: Publish to crates.io
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable

      - name: Verify version matches tag
        run: |
          CARGO_VERSION=$(cargo metadata --no-deps --format-version 1 | jq -r '.packages[0].version')
          TAG_VERSION=${GITHUB_REF#refs/tags/v}
          if [ "$CARGO_VERSION" != "$TAG_VERSION" ]; then
            echo "Version mismatch: Cargo.toml has $CARGO_VERSION but tag is $TAG_VERSION"
            exit 1
          fi

      - name: Run tests
        run: cargo test --all-features

      - name: Publish to crates.io
        run: cargo publish --token ${{ secrets.CRATES_IO_TOKEN }}

  release:
    name: Create Release
    needs: build
    runs-on: ubuntu-latest
    outputs:
      sha256_darwin_amd64: ${{ steps.checksums.outputs.sha256_darwin_amd64 }}
      sha256_darwin_arm64: ${{ steps.checksums.outputs.sha256_darwin_arm64 }}
      sha256_linux_amd64: ${{ steps.checksums.outputs.sha256_linux_amd64 }}
      sha256_linux_arm64: ${{ steps.checksums.outputs.sha256_linux_arm64 }}
    permissions:
      contents: write

    steps:
      - name: Download all artifacts
        uses: actions/download-artifact@v4
        with:
          path: artifacts
          merge-multiple: true

      - name: List artifacts
        run: ls -la artifacts/

      - name: Extract checksums
        id: checksums
        run: |
          cd artifacts
          echo "sha256_darwin_amd64=$(cat ftdc-${{ github.ref_name }}-darwin-amd64.tar.gz.sha256 | cut -d ' ' -f 1)" >> $GITHUB_OUTPUT
          echo "sha256_darwin_arm64=$(cat ftdc-${{ github.ref_name }}-darwin-arm64.tar.gz.sha256 | cut -d ' ' -f 1)" >> $GITHUB_OUTPUT
          echo "sha256_linux_amd64=$(cat ftdc-${{ github.ref_name }}-linux-amd64.tar.gz.sha256 | cut -d ' ' -f 1)" >> $GITHUB_OUTPUT
          echo "sha256_linux_arm64=$(cat ftdc-${{ github.ref_name }}-linux-arm64.tar.gz.sha256 | cut -d ' ' -f 1)" >> $GITHUB_OUTPUT

      - name: Create checksums file
        run: |
          cd artifacts
          cat *.sha256 > checksums.txt
          echo "=== SHA256 Checksums ==="
          cat checksums.txt

      - name: Create GitHub Release
        uses: softprops/action-gh-release@v2
        with:
          files: |
            artifacts/*.tar.gz
            artifacts/*.zip
            artifacts/checksums.txt
          generate_release_notes: true

  update-homebrew:
    name: Update Homebrew Formula
    needs: release
    runs-on: ubuntu-latest

    steps:
      - name: Checkout homebrew-tap
        uses: actions/checkout@v4
        with:
          repository: maoertel/homebrew-tap
          token: ${{ secrets.HOMEBREW_TAP_TOKEN }}
          path: homebrew-tap

      - name: Update formula
        env:
          VERSION: ${{ github.ref_name }}
          SHA256_DARWIN_AMD64: ${{ needs.release.outputs.sha256_darwin_amd64 }}
          SHA256_DARWIN_ARM64: ${{ needs.release.outputs.sha256_darwin_arm64 }}
          SHA256_LINUX_AMD64: ${{ needs.release.outputs.sha256_linux_amd64 }}
          SHA256_LINUX_ARM64: ${{ needs.release.outputs.sha256_linux_arm64 }}
        run: |
          cd homebrew-tap
          VERSION_NUM=${VERSION#v}  # Remove 'v' prefix for version field

          cat > Formula/ftdc.rb << EOF
          class Ftdc < Formula
            desc "CLI tool to download FTDC data from MongoDB Atlas clusters"
            version "${VERSION_NUM}"
            homepage "https://github.com/maoertel/mongodb-ftdc"
            license "MIT"

            if OS.mac? && Hardware::CPU.intel?
              url "https://github.com/maoertel/mongodb-ftdc/releases/download/${VERSION}/ftdc-${VERSION}-darwin-amd64.tar.gz"
              sha256 "${SHA256_DARWIN_AMD64}"
            end
            if OS.mac? && Hardware::CPU.arm?
              url "https://github.com/maoertel/mongodb-ftdc/releases/download/${VERSION}/ftdc-${VERSION}-darwin-arm64.tar.gz"
              sha256 "${SHA256_DARWIN_ARM64}"
            end
            if OS.linux? && Hardware::CPU.intel?
              url "https://github.com/maoertel/mongodb-ftdc/releases/download/${VERSION}/ftdc-${VERSION}-linux-amd64.tar.gz"
              sha256 "${SHA256_LINUX_AMD64}"
            end
            if OS.linux? && Hardware::CPU.arm?
              url "https://github.com/maoertel/mongodb-ftdc/releases/download/${VERSION}/ftdc-${VERSION}-linux-arm64.tar.gz"
              sha256 "${SHA256_LINUX_ARM64}"
            end

            def install
              bin.install "ftdc"
            end

            test do
              system "#{bin}/ftdc --version"
            end
          end
          EOF

      - name: Create Pull Request
        uses: peter-evans/create-pull-request@v6
        with:
          path: homebrew-tap
          token: ${{ secrets.HOMEBREW_TAP_TOKEN }}
          commit-message: "Update ftdc to ${{ github.ref_name }}"
          title: "Update ftdc to ${{ github.ref_name }}"
          body: |
            Automated PR to update ftdc formula to ${{ github.ref_name }}.

            ## Checksums
            - darwin-amd64: `${{ needs.release.outputs.sha256_darwin_amd64 }}`
            - darwin-arm64: `${{ needs.release.outputs.sha256_darwin_arm64 }}`
            - linux-amd64: `${{ needs.release.outputs.sha256_linux_amd64 }}`
            - linux-arm64: `${{ needs.release.outputs.sha256_linux_arm64 }}`

            Release: https://github.com/maoertel/mongodb-ftdc/releases/tag/${{ github.ref_name }}
          branch: update-ftdc-${{ github.ref_name }}
          delete-branch: true