Files
sf-cli-wrapper/sf-org-info.ps1

211 lines
5.2 KiB
PowerShell

#!/usr/bin/env pwsh
<#
.SYNOPSIS
Quick org information display wrapper for Salesforce CLI
.DESCRIPTION
A user-friendly wrapper around Salesforce CLI org commands that provides
quick access to org information, limits, and authentication status with
clean, formatted output.
.PARAMETER to
Target org username or alias (uses default if not specified)
.PARAMETER lm
Show detailed org limits information
.PARAMETER ls
List all authenticated orgs
.PARAMETER ve
Enable verbose output with additional details
.PARAMETER hp
Show this help message
.EXAMPLE
.\sf-org-info.ps1
.\sf-org-info.ps1 -to "myorg" -lm -ve
.\sf-org-info.ps1 -lm
.\sf-org-info.ps1 -ls
.NOTES
This script automatically checks for Salesforce CLI installation and runs
diagnostics if the CLI is not found.
#>
param(
[string]$to,
[switch]$lm,
[switch]$ls,
[switch]$ve,
[switch]$hp
)
# Show help if requested or if no parameters provided
if ($hp -or (-not $to -and -not $lm -and -not $ls -and -not $ve)) {
Get-Help $MyInvocation.MyCommand.Path -Detailed
exit 0
}
# Function to check if Salesforce CLI is installed
function Test-SalesforceCLI {
try {
$null = Get-Command sf -ErrorAction Stop
return $true
} catch {
return $false
}
}
# Function to run sf-check diagnostics
function Invoke-SalesforceCheck {
$checkScript = if (Test-Path "sf-check.ps1") {
".\sf-check.ps1"
} elseif (Test-Path "sf-check.sh") {
"bash sf-check.sh"
} else {
$null
}
if ($checkScript) {
Write-Host "Running Salesforce CLI diagnostics..." -ForegroundColor Yellow
Invoke-Expression $checkScript
} else {
Write-Host "Salesforce CLI not found and no diagnostic script available." -ForegroundColor Red
Write-Host "Please install the Salesforce CLI: https://developer.salesforce.com/tools/salesforcecli" -ForegroundColor Red
}
}
# Function to execute sf commands with error handling
function Invoke-SafeSfCommand {
param([string[]]$Arguments)
try {
Write-Host "Executing: sf $($Arguments -join ' ')" -ForegroundColor Gray
Write-Host ""
# Execute the command and capture the output
$output = & sf @Arguments 2>&1
$exitCode = $LASTEXITCODE
# Display the output
if ($output) {
$output | ForEach-Object { Write-Host $_ }
}
if ($exitCode -ne 0) {
Write-Host ""
Write-Host "❌ Command failed with exit code: $exitCode" -ForegroundColor Red
return $false
}
return $true
} catch {
Write-Host "Error executing sf command: $($_.Exception.Message)" -ForegroundColor Red
return $false
}
}
# Function to display section header
function Write-SectionHeader {
param([string]$Title)
Write-Host ""
Write-Host "🔍 $Title" -ForegroundColor Blue
Write-Host ("=" * (4 + $Title.Length)) -ForegroundColor Blue
}
# Silently check for Salesforce CLI
if (-not (Test-SalesforceCLI)) {
Invoke-SalesforceCheck
exit 1
}
# If list is specified, show all authenticated orgs and exit
if ($ls) {
Write-SectionHeader "Authenticated Orgs"
$success = Invoke-SafeSfCommand @("org", "list")
if ($success) {
Write-Host ""
Write-Host "✅ Listed all authenticated orgs" -ForegroundColor Green
}
exit 0
}
# Build the base sf command for org display
$sfArgs = @("org", "display")
# Add target org if specified
if ($to) {
$sfArgs += "--target-org"
$sfArgs += $to
Write-Host "Target org: $to" -ForegroundColor Cyan
}
# Add verbose flag if requested
if ($ve) {
$sfArgs += "--verbose"
}
# Display org information
Write-SectionHeader "Organization Information"
$success = Invoke-SafeSfCommand $sfArgs
if (-not $success) {
exit 1
}
# If limits are requested, show org limits
if ($lm) {
Write-SectionHeader "Organization Limits"
# Build limits command
$limitsArgs = @("org", "list", "limits")
if ($to) {
$limitsArgs += "--target-org"
$limitsArgs += $to
}
$success = Invoke-SafeSfCommand $limitsArgs
if (-not $success) {
Write-Host "Warning: Could not retrieve org limits" -ForegroundColor Yellow
}
}
# Show additional org context if verbose
if ($ve) {
Write-SectionHeader "Additional Context"
# Show all authenticated orgs for context
Write-Host "📋 All Authenticated Orgs:" -ForegroundColor Yellow
$success = Invoke-SafeSfCommand @("org", "list")
if ($success) {
Write-Host ""
}
# Try to show current user info
Write-Host "👤 Current User Info:" -ForegroundColor Yellow
$userArgs = @("org", "display", "user")
if ($to) {
$userArgs += "--target-org"
$userArgs += $to
}
$success = Invoke-SafeSfCommand $userArgs
}
# Final success message
Write-Host ""
Write-Host "✅ Organization information retrieved successfully!" -ForegroundColor Green
if (-not $lm -and -not $ve) {
Write-Host ""
Write-Host "💡 Tip: Use -lm to see org limits, -ve for more details, or -ls to see all authenticated orgs" -ForegroundColor Yellow
}