magma-code 0.1.1

A CLI tool for ingesting code files into Magma scanner with smart file filtering and Git integration
# Magma Ingest CLI Installation Script for Windows
# This script installs the magma-ingest CLI tool

param(
    [switch]$Force,
    [string]$InstallDir = "$env:USERPROFILE\.local\bin"
)

# Colors for output
$Red = [System.ConsoleColor]::Red
$Green = [System.ConsoleColor]::Green
$Yellow = [System.ConsoleColor]::Yellow
$Blue = [System.ConsoleColor]::Blue

function Write-ColorOutput {
    param(
        [string]$Message,
        [System.ConsoleColor]$Color = [System.ConsoleColor]::White
    )
    $originalColor = $Host.UI.RawUI.ForegroundColor
    $Host.UI.RawUI.ForegroundColor = $Color
    Write-Output $Message
    $Host.UI.RawUI.ForegroundColor = $originalColor
}

function Test-Command {
    param([string]$Command)
    try {
        Get-Command $Command -ErrorAction Stop | Out-Null
        return $true
    }
    catch {
        return $false
    }
}

Write-ColorOutput "🔥 Installing Magma Ingest CLI Tool..." $Blue

# Check if Rust/Cargo is installed and try cargo install first
if (Test-Command "cargo") {
    Write-ColorOutput "🦀 Rust detected! Installing from crates.io..." $Green
    try {
        cargo install magma-ingest
        Write-ColorOutput "✅ magma-ingest installed successfully via cargo!" $Green
        Write-ColorOutput "🚀 Usage: magma-ingest --org <org-id> --key <api-key> --repo-name <repo-name>" $Blue
        exit 0
    }
    catch {
        Write-ColorOutput "❌ Failed to install via cargo, trying alternative method..." $Yellow
    }
}

Write-ColorOutput "⚠️  Rust/Cargo not found or cargo install failed." $Yellow
Write-ColorOutput "📥 Attempting to download pre-built binary..." $Blue

# Configuration
$RepoOwner = "bluemagma-compliance"
$RepoName = "magma-ingest"
$BinaryName = "magma-ingest.exe"

# Create install directory
if (!(Test-Path $InstallDir)) {
    New-Item -ItemType Directory -Path $InstallDir -Force | Out-Null
}

try {
    # Get latest release info
    Write-ColorOutput "🔍 Fetching latest release information..." $Blue
    $LatestReleaseUrl = "https://api.github.com/repos/$RepoOwner/$RepoName/releases/latest"
    $ReleaseInfo = Invoke-RestMethod -Uri $LatestReleaseUrl
    
    # Find Windows binary
    $WindowsAsset = $ReleaseInfo.assets | Where-Object { $_.name -like "*windows*" -or $_.name -like "*win*" -or $_.name -like "*.exe" }
    
    if (!$WindowsAsset) {
        throw "No Windows binary found in latest release"
    }
    
    $DownloadUrl = $WindowsAsset.browser_download_url
    $TempFile = "$env:TEMP\magma-ingest-installer.zip"
    
    Write-ColorOutput "📥 Downloading from: $DownloadUrl" $Blue
    Invoke-WebRequest -Uri $DownloadUrl -OutFile $TempFile
    
    # Extract if it's a zip file
    if ($WindowsAsset.name -like "*.zip") {
        $TempExtractDir = "$env:TEMP\magma-ingest-extract"
        if (Test-Path $TempExtractDir) {
            Remove-Item $TempExtractDir -Recurse -Force
        }
        Expand-Archive -Path $TempFile -DestinationPath $TempExtractDir
        
        # Find the binary in extracted files
        $BinaryPath = Get-ChildItem -Path $TempExtractDir -Name $BinaryName -Recurse | Select-Object -First 1
        if ($BinaryPath) {
            Copy-Item -Path (Join-Path $TempExtractDir $BinaryPath) -Destination (Join-Path $InstallDir $BinaryName) -Force
        } else {
            throw "Binary not found in extracted files"
        }
        
        # Cleanup
        Remove-Item $TempExtractDir -Recurse -Force
    } else {
        # Direct binary download
        Copy-Item -Path $TempFile -Destination (Join-Path $InstallDir $BinaryName) -Force
    }
    
    # Cleanup temp file
    Remove-Item $TempFile -Force
    
    Write-ColorOutput "✅ magma-ingest installed to $InstallDir\$BinaryName" $Green
}
catch {
    Write-ColorOutput "❌ Failed to download pre-built binary: $($_.Exception.Message)" $Red
    Write-ColorOutput "💡 Please install Rust and run: cargo install magma-ingest" $Yellow
    Write-ColorOutput "   Install Rust from: https://rustup.rs/" $Blue
    exit 1
}

# Check if install directory is in PATH
$CurrentPath = [Environment]::GetEnvironmentVariable("PATH", "User")
if ($CurrentPath -notlike "*$InstallDir*") {
    Write-ColorOutput "⚠️  Warning: $InstallDir is not in your PATH." $Yellow
    Write-ColorOutput "   Adding to user PATH..." $Blue
    
    try {
        $NewPath = "$CurrentPath;$InstallDir"
        [Environment]::SetEnvironmentVariable("PATH", $NewPath, "User")
        Write-ColorOutput "✅ Added $InstallDir to your PATH" $Green
        Write-ColorOutput "🔄 Please restart your terminal or run: refreshenv" $Blue
    }
    catch {
        Write-ColorOutput "❌ Failed to update PATH. Please add manually:" $Red
        Write-ColorOutput "   $InstallDir" $Yellow
    }
}

# Verify installation
$BinaryPath = Join-Path $InstallDir $BinaryName
if (Test-Path $BinaryPath) {
    Write-ColorOutput "🎉 Installation complete!" $Green
    Write-ColorOutput "🚀 Usage: magma-ingest --org <org-id> --key <api-key> --repo-name <repo-name>" $Blue
    Write-ColorOutput "❓ Help:  magma-ingest --help" $Blue
    
    # Try to run version command
    try {
        & $BinaryPath --version
    }
    catch {
        Write-ColorOutput "⚠️  Binary installed but may need PATH refresh" $Yellow
    }
} else {
    Write-ColorOutput "❌ Installation verification failed" $Red
    exit 1
}