Files
sf-cli-wrapper/sf-web-logout.ps1
2025-08-29 15:37:05 +08:00

162 lines
3.9 KiB
PowerShell

#!/usr/bin/env pwsh
<#
.SYNOPSIS
Web logout wrapper for Salesforce CLI
.DESCRIPTION
A user-friendly wrapper around 'sf org logout' that simplifies
logging out from Salesforce orgs with better formatting
and error handling.
.PARAMETER to
Org alias or username to logout from
.PARAMETER al
Logout from all authenticated orgs (sets --all)
.PARAMETER ve
Enable verbose output showing logout details
.PARAMETER hp
Show this help message
.EXAMPLE
.\sf-web-logout.ps1 -to "DEMO"
.\sf-web-logout.ps1 -al
.\sf-web-logout.ps1 -to "NUSHUB-PROD" -ve
.NOTES
This script automatically checks for Salesforce CLI installation and runs
diagnostics if the CLI is not found.
#>
param(
[string]$to,
[switch]$al,
[switch]$ve,
[switch]$hp
)
# Show help if requested
if ($hp) {
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 {
Write-Host "❌ Salesforce CLI (sf) not found!" -ForegroundColor Red
Write-Host ""
Write-Host "Running environment check to help you get started..." -ForegroundColor Yellow
Write-Host ""
$checkScript = if (Test-Path "sf-check.ps1") {
".\sf-check.ps1"
} elseif (Test-Path "sf-check") {
"./sf-check"
} else {
$null
}
if ($checkScript) {
try {
Invoke-Expression $checkScript
} catch {
Write-Host "Error running diagnostics: $($_.Exception.Message)" -ForegroundColor Red
}
} else {
Write-Host "sf-check not found. Please install the Salesforce CLI from:" -ForegroundColor Red
Write-Host "https://developer.salesforce.com/tools/sfdxcli" -ForegroundColor Red
}
}
# Validate that both target org and all aren't specified
if ($to -and $al) {
Write-Host "Error: Cannot specify both -to and -al parameters" -ForegroundColor Red
exit 1
}
# Silently check for Salesforce CLI
if (-not (Test-SalesforceCLI)) {
Invoke-SalesforceCheck
exit 1
}
# Build the sf command
$sfArgs = @("org", "logout")
# Add parameters based on input
if ($to) {
$sfArgs += "--target-org"
$sfArgs += $to
}
if ($al) {
$sfArgs += "--all"
}
# Display verbose information if requested
if ($ve) {
Write-Host "🚪 Starting Logout Process" -ForegroundColor Blue
Write-Host "=========================" -ForegroundColor Blue
if ($to) {
Write-Host "Target org: $to" -ForegroundColor Cyan
}
if ($al) {
Write-Host "Logout from: All authenticated orgs" -ForegroundColor Cyan
}
Write-Host ""
}
# Display the command being run
Write-Host ">>> Running: sf $($sfArgs -join ' ')" -ForegroundColor Gray
# Execute the command
try {
$startTime = Get-Date
& sf @sfArgs
$exitCode = $LASTEXITCODE
$endTime = Get-Date
$duration = $endTime - $startTime
if ($ve) {
Write-Host ""
Write-Host "⏱️ Logout completed in $($duration.TotalSeconds) seconds" -ForegroundColor Green
}
if ($exitCode -eq 0) {
Write-Host ""
Write-Host "✅ Logout completed successfully!" -ForegroundColor Green
if ($to) {
Write-Host "🚪 Logged out from org: $to" -ForegroundColor Yellow
} elseif ($al) {
Write-Host "🚪 Logged out from all authenticated orgs" -ForegroundColor Yellow
}
} else {
Write-Host ""
Write-Host "❌ Logout failed with exit code: $exitCode" -ForegroundColor Red
Write-Host "💡 Check the org alias/username and try again" -ForegroundColor Yellow
}
exit $exitCode
} catch {
Write-Host "Error executing sf command: $($_.Exception.Message)" -ForegroundColor Red
exit 1
}