From 01de33ccc140ea0e6e3fe0bfd0bd961f3f7c54ab Mon Sep 17 00:00:00 2001 From: reynold Date: Fri, 29 Aug 2025 15:37:05 +0800 Subject: [PATCH] added addl wrappers --- README.md | 75 +++++++++++++++++++- sf-web-login | 109 +++++++++++++++++++++++++++++ sf-web-login.ps1 | 170 ++++++++++++++++++++++++++++++++++++++++++++++ sf-web-logout | 107 +++++++++++++++++++++++++++++ sf-web-logout.ps1 | 161 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 621 insertions(+), 1 deletion(-) create mode 100755 sf-web-login create mode 100644 sf-web-login.ps1 create mode 100755 sf-web-logout create mode 100644 sf-web-logout.ps1 diff --git a/README.md b/README.md index ffafc09..f87ae25 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,10 @@ Core: - **[`sf-web-open`](#sf-web-open)** - Quick org browser opener for `sf org open` - **[`sf-check`](#sf-check)** - Environment verification tool to check SF CLI installation and configuration +Authentication: +- **[`sf-web-login` / `sf-web-login.ps1`](#sf-web-login--sf-web-loginps1)** - Web-based org authentication for `sf org login web` +- **[`sf-web-logout` / `sf-web-logout.ps1`](#sf-web-logout--sf-web-logoutps1)** - Org logout wrapper for `sf org logout` + Org and metadata: - **[`sf-org-create` / `sf-org-create.ps1`](#sf-org-create)** - Smart scratch org creation - **[`sf-org-info` / `sf-org-info.ps1`](#sf-org-info--sf-org-infops1)** - Quick org info, limits, and context @@ -78,7 +82,7 @@ sf-data-export -qy "SELECT Id FROM Account" -to myorg -fm csv 2. Make the scripts executable: ```bash chmod +x \ - sf-deploy sf-dry-run sf-web-open sf-check \ + sf-deploy sf-dry-run sf-web-open sf-web-login sf-web-logout sf-check \ sf-org-create sf-org-info sf-retrieve sf-test-run sf-apex-run \ sf-data-export sf-data-import sf-logs-tail ``` @@ -91,6 +95,8 @@ sf-data-export -qy "SELECT Id FROM Account" -to myorg -fm csv ln -s /path/to/sf-cli-wrapper/sf-deploy /usr/local/bin/sf-deploy ln -s /path/to/sf-cli-wrapper/sf-dry-run /usr/local/bin/sf-dry-run ln -s /path/to/sf-cli-wrapper/sf-web-open /usr/local/bin/sf-web-open +ln -s /path/to/sf-cli-wrapper/sf-web-login /usr/local/bin/sf-web-login +ln -s /path/to/sf-cli-wrapper/sf-web-logout /usr/local/bin/sf-web-logout ln -s /path/to/sf-cli-wrapper/sf-check /usr/local/bin/sf-check ln -s /path/to/sf-cli-wrapper/sf-org-create /usr/local/bin/sf-org-create ln -s /path/to/sf-cli-wrapper/sf-org-info /usr/local/bin/sf-org-info @@ -118,6 +124,8 @@ ln -s /path/to/sf-cli-wrapper/sf-logs-tail /usr/local/bin/sf-logs-tail Set-Alias sf-deploy "C:\\path\\to\\sf-cli-wrapper\\sf-deploy.ps1" Set-Alias sf-dry-run "C:\\path\\to\\sf-cli-wrapper\\sf-dry-run.ps1" Set-Alias sf-web-open "C:\\path\\to\\sf-cli-wrapper\\sf-web-open.ps1" +Set-Alias sf-web-login "C:\\path\\to\\sf-cli-wrapper\\sf-web-login.ps1" +Set-Alias sf-web-logout "C:\\path\\to\\sf-cli-wrapper\\sf-web-logout.ps1" Set-Alias sf-check "C:\\path\\to\\sf-cli-wrapper\\sf-check.ps1" Set-Alias sf-org-create "C:\\path\\to\\sf-cli-wrapper\\sf-org-create.ps1" Set-Alias sf-org-info "C:\\path\\to\\sf-cli-wrapper\\sf-org-info.ps1" @@ -527,6 +535,71 @@ sf-web-open -to NUSHUB-DR2 -ur --- +### [🏠](#salesforce-cli-wrapper-scripts) sf-web-login / sf-web-login.ps1 + +Web-based org authentication wrapper for `sf org login web`. + +**Usage:** +```bash +sf-web-login [-al ALIAS] [-in INSTANCE_URL] [-ud] [-ve] [-hp] +``` +```powershell +sf-web-login.ps1 -al "NUSHUB-PROD" -ve +``` + +**Options:** +- `-al` - Alias for the authenticated org (passes --alias) +- `-in` - Instance URL to authenticate against (passes --instance-url) +- `-ud` - Update default org after login (passes --set-default) +- `-ve` - Enable verbose output showing login details +- `-hp` - Show help + +**Examples:** +```bash +# Login to production with alias +sf-web-login -al NUSHUB-PROD + +# Login to specific instance with alias and set as default +sf-web-login -al MySandbox -in https://test.my-domain.my.salesforce.com -ud + +# Login with verbose output +sf-web-login -al TestOrg -ve +``` + +--- + +### [🏠](#salesforce-cli-wrapper-scripts) sf-web-logout / sf-web-logout.ps1 + +Org logout wrapper for `sf org logout`. + +**Usage:** +```bash +sf-web-logout [-to ORG_ALIAS_OR_USERNAME] [-al] [-ve] [-hp] +``` +```powershell +sf-web-logout.ps1 -to "DEMO" -ve +``` + +**Options:** +- `-to` - Org alias or username to logout from (passes --target-org) +- `-al` - Logout from all authenticated orgs (passes --all) +- `-ve` - Enable verbose output showing logout details +- `-hp` - Show help + +**Examples:** +```bash +# Logout from specific org +sf-web-logout -to DEMO + +# Logout from all orgs +sf-web-logout -al + +# Logout with verbose output +sf-web-logout -to NUSHUB-PROD -ve +``` + +--- + ### [🏠](#salesforce-cli-wrapper-scripts) sf-check Environment verification tool that checks if the Salesforce CLI is properly installed and configured. diff --git a/sf-web-login b/sf-web-login new file mode 100755 index 0000000..e367ad0 --- /dev/null +++ b/sf-web-login @@ -0,0 +1,109 @@ +#!/usr/bin/env bash +set -euo pipefail + +show_help() { + cat <<'EOF' +sf-web-login — wrapper for `sf org login web` + +USAGE: + sf-web-login [-al ] [-in ] [-ud] [-hp] [-ve] + +OPTIONS: + -al Alias for the authenticated org (passes --alias) + -in Instance URL to authenticate against (passes --instance-url) + -ud Update default org after login (passes --set-default) + -ve Enable verbose output + -hp Show this help + +EXAMPLES: + 1) Login to production with alias: + sf-web-login -al NUSHUB-PROD + + 2) Login to specific instance with alias and set as default: + sf-web-login -al MySandbox -in https://test.my-domain.my.salesforce.com -ud + + 3) Simple login (will prompt for alias): + sf-web-login +EOF +} + +ALIAS="" +INSTANCE_URL="" +SET_DEFAULT=0 +VERBOSE=0 + +# If no args → show help + examples and exit without error +if [[ $# -eq 0 ]]; then + show_help + exit 0 +fi + +# Parse command line arguments using manual parsing for two-character options +while [[ $# -gt 0 ]]; do + case $1 in + -al) + ALIAS="$2" + shift 2 + ;; + -in) + INSTANCE_URL="$2" + shift 2 + ;; + -ud) + SET_DEFAULT=1 + shift + ;; + -ve) + VERBOSE=1 + shift + ;; + -hp) + show_help + exit 0 + ;; + *) + echo "Unknown option: $1" >&2 + echo + show_help + exit 1 + ;; + esac +done + +# Silent environment check - verify SF CLI is available +if ! command -v sf >/dev/null 2>&1; then + echo "❌ Salesforce CLI (sf) not found!" + echo + echo "Running environment check to help you get started..." + echo + + # Try to find and run sf-check in the same directory as this script + SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" + if [[ -x "$SCRIPT_DIR/sf-check" ]]; then + "$SCRIPT_DIR/sf-check" + elif command -v sf-check >/dev/null 2>&1; then + sf-check + else + echo "sf-check not found. Please install the Salesforce CLI from:" + echo "https://developer.salesforce.com/tools/sfdxcli" + fi + exit 1 +fi + +CMD=(sf org login web) + +[[ -n "$ALIAS" ]] && CMD+=(--alias "$ALIAS") +[[ -n "$INSTANCE_URL" ]] && CMD+=(--instance-url "$INSTANCE_URL") +[[ $SET_DEFAULT -eq 1 ]] && CMD+=(--set-default) + +if [[ $VERBOSE -eq 1 ]]; then + echo "🔐 Starting Web Login" + echo "====================" + [[ -n "$ALIAS" ]] && echo "Alias: $ALIAS" + [[ -n "$INSTANCE_URL" ]] && echo "Instance URL: $INSTANCE_URL" + [[ $SET_DEFAULT -eq 1 ]] && echo "Will set as default org: Yes" + echo +fi + +echo ">>> Running: ${CMD[*]}" +exec "${CMD[@]}" diff --git a/sf-web-login.ps1 b/sf-web-login.ps1 new file mode 100644 index 0000000..abd3921 --- /dev/null +++ b/sf-web-login.ps1 @@ -0,0 +1,170 @@ +#!/usr/bin/env pwsh + +<# +.SYNOPSIS + Web login wrapper for Salesforce CLI + +.DESCRIPTION + A user-friendly wrapper around 'sf org login web' that simplifies + authenticating to Salesforce orgs via web browser with better formatting + and error handling. + +.PARAMETER al + Alias for the authenticated org + +.PARAMETER in + Instance URL to authenticate against + +.PARAMETER ud + Update default org after login (sets --set-default) + +.PARAMETER ve + Enable verbose output showing login details + +.PARAMETER hp + Show this help message + +.EXAMPLE + .\sf-web-login.ps1 -al "NUSHUB-PROD" + .\sf-web-login.ps1 -al "MySandbox" -in "https://test.my-domain.my.salesforce.com" -ud + .\sf-web-login.ps1 -ve + +.NOTES + This script automatically checks for Salesforce CLI installation and runs + diagnostics if the CLI is not found. +#> + +param( + [string]$al, + [string]$in, + [switch]$ud, + [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 + } +} + +# Silently check for Salesforce CLI +if (-not (Test-SalesforceCLI)) { + Invoke-SalesforceCheck + exit 1 +} + +# Build the sf command +$sfArgs = @("org", "login", "web") + +# Add parameters based on input +if ($al) { + $sfArgs += "--alias" + $sfArgs += $al +} + +if ($in) { + $sfArgs += "--instance-url" + $sfArgs += $in +} + +if ($ud) { + $sfArgs += "--set-default" +} + +# Display verbose information if requested +if ($ve) { + Write-Host "🔐 Starting Web Login" -ForegroundColor Blue + Write-Host "====================" -ForegroundColor Blue + + if ($al) { + Write-Host "Alias: $al" -ForegroundColor Cyan + } + + if ($in) { + Write-Host "Instance URL: $in" -ForegroundColor Cyan + } + + if ($ud) { + Write-Host "Will set as default org: Yes" -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 "⏱️ Login completed in $($duration.TotalSeconds) seconds" -ForegroundColor Green + } + + if ($exitCode -eq 0) { + Write-Host "" + Write-Host "✅ Web login completed successfully!" -ForegroundColor Green + + if ($al) { + Write-Host "🏷️ Org authenticated with alias: $al" -ForegroundColor Cyan + } + + if ($ud) { + Write-Host "⚡ Set as default org" -ForegroundColor Yellow + } + } else { + Write-Host "" + Write-Host "❌ Web login failed with exit code: $exitCode" -ForegroundColor Red + Write-Host "💡 Check your browser and try again" -ForegroundColor Yellow + } + + exit $exitCode + +} catch { + Write-Host "Error executing sf command: $($_.Exception.Message)" -ForegroundColor Red + exit 1 +} diff --git a/sf-web-logout b/sf-web-logout new file mode 100755 index 0000000..d3a4865 --- /dev/null +++ b/sf-web-logout @@ -0,0 +1,107 @@ +#!/usr/bin/env bash +set -euo pipefail + +show_help() { + cat <<'EOF' +sf-web-logout — wrapper for `sf org logout` + +USAGE: + sf-web-logout [-to ] [-al] [-hp] [-ve] + +OPTIONS: + -to Org alias or username to logout from (passes --target-org) + -al Logout from all authenticated orgs (passes --all) + -ve Enable verbose output + -hp Show this help + +EXAMPLES: + 1) Logout from specific org: + sf-web-logout -to DEMO + + 2) Logout from all orgs: + sf-web-logout -al + + 3) Logout with verbose output: + sf-web-logout -to NUSHUB-PROD -ve +EOF +} + +TARGET_ORG="" +LOGOUT_ALL=0 +VERBOSE=0 + +# If no args → show help + examples and exit without error +if [[ $# -eq 0 ]]; then + show_help + exit 0 +fi + +# Parse command line arguments using manual parsing for two-character options +while [[ $# -gt 0 ]]; do + case $1 in + -to) + TARGET_ORG="$2" + shift 2 + ;; + -al) + LOGOUT_ALL=1 + shift + ;; + -ve) + VERBOSE=1 + shift + ;; + -hp) + show_help + exit 0 + ;; + *) + echo "Unknown option: $1" >&2 + echo + show_help + exit 1 + ;; + esac +done + +# Validate that both target org and all aren't specified +if [[ -n "$TARGET_ORG" && $LOGOUT_ALL -eq 1 ]]; then + echo "Error: Cannot specify both -to and -al options" >&2 + exit 1 +fi + +# Silent environment check - verify SF CLI is available +if ! command -v sf >/dev/null 2>&1; then + echo "❌ Salesforce CLI (sf) not found!" + echo + echo "Running environment check to help you get started..." + echo + + # Try to find and run sf-check in the same directory as this script + SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" + if [[ -x "$SCRIPT_DIR/sf-check" ]]; then + "$SCRIPT_DIR/sf-check" + elif command -v sf-check >/dev/null 2>&1; then + sf-check + else + echo "sf-check not found. Please install the Salesforce CLI from:" + echo "https://developer.salesforce.com/tools/sfdxcli" + fi + exit 1 +fi + +CMD=(sf org logout) + +[[ -n "$TARGET_ORG" ]] && CMD+=(--target-org "$TARGET_ORG") +[[ $LOGOUT_ALL -eq 1 ]] && CMD+=(--all) + +if [[ $VERBOSE -eq 1 ]]; then + echo "🚪 Starting Logout Process" + echo "=========================" + [[ -n "$TARGET_ORG" ]] && echo "Target org: $TARGET_ORG" + [[ $LOGOUT_ALL -eq 1 ]] && echo "Logout from: All authenticated orgs" + echo +fi + +echo ">>> Running: ${CMD[*]}" +exec "${CMD[@]}" diff --git a/sf-web-logout.ps1 b/sf-web-logout.ps1 new file mode 100644 index 0000000..fa229cf --- /dev/null +++ b/sf-web-logout.ps1 @@ -0,0 +1,161 @@ +#!/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 +}