Files
sf-cli-wrapper/sf-check
reynold 452e095f90 Reorganize directory structure: move utility files to misc/ directory
- Move all non-wrapper files (testing scripts, documentation, utilities) to misc/ directory
- Keep SF CLI wrapper scripts and README.md in root directory for better organization
- Maintain clean root directory with only the actual wrapper scripts and main documentation
- All wrapper scripts remain easily accessible and discoverable
- Supporting files are organized in misc/ subdirectory
2025-08-28 22:32:19 +08:00

289 lines
7.2 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
show_help() {
cat <<'EOF'
sf-check — verify Salesforce CLI environment and configuration
USAGE:
sf-check [-ve] [-hp]
OPTIONS:
-ve Verbose output (show detailed information)
-hp Show this help
DESCRIPTION:
This script verifies that the Salesforce CLI is properly installed and configured.
It checks for:
- SF CLI installation and version
- Available orgs and authentication status
- Basic configuration settings
- Common issues and recommendations
EXAMPLES:
sf-check # Basic environment check
sf-check -ve # Verbose output with detailed information
EOF
}
# Function to print status messages
print_status() {
local status="$1"
local message="$2"
case "$status" in
"OK")
printf "[${GREEN}${NC}] %s\n" "$message"
;;
"WARN")
printf "[${YELLOW}!${NC}] %s\n" "$message"
;;
"ERROR")
printf "[${RED}${NC}] %s\n" "$message"
;;
"INFO")
printf "[${BLUE}i${NC}] %s\n" "$message"
;;
esac
}
# Function to check if a command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Function to check SF CLI installation
check_sf_installation() {
print_status "INFO" "Checking Salesforce CLI installation..."
if command_exists sf; then
local sf_version
sf_version=$(sf --version 2>/dev/null | head -n 1 || echo "unknown")
print_status "OK" "Salesforce CLI found: $sf_version"
if [[ "$VERBOSE" == "true" ]]; then
echo " Location: $(which sf)"
fi
return 0
else
print_status "ERROR" "Salesforce CLI (sf) not found in PATH"
echo " Please install the Salesforce CLI from: https://developer.salesforce.com/tools/sfdxcli"
return 1
fi
}
# Function to check authenticated orgs
check_orgs() {
print_status "INFO" "Checking authenticated orgs..."
if ! command_exists sf; then
return 1
fi
local org_list
if org_list=$(sf org list --json 2>/dev/null); then
local org_count
org_count=$(echo "$org_list" | jq -r '.result | length' 2>/dev/null || echo "0")
if [[ "$org_count" -gt 0 ]]; then
print_status "OK" "Found $org_count authenticated org(s)"
if [[ "$VERBOSE" == "true" ]]; then
echo "$org_list" | jq -r '.result[] | " - \(.alias // .username) (\(.orgId[0:15])...)"' 2>/dev/null || {
echo " (Unable to parse org details - jq not available)"
}
fi
else
print_status "WARN" "No authenticated orgs found"
echo " Run 'sf org login web' or 'sf org login jwt' to authenticate"
fi
else
print_status "ERROR" "Unable to list orgs (sf org list failed)"
return 1
fi
}
# Function to check default org
check_default_org() {
print_status "INFO" "Checking default org configuration..."
if ! command_exists sf; then
return 1
fi
local default_org
if default_org=$(sf config get target-org --json 2>/dev/null | jq -r '.result[0].value // empty' 2>/dev/null); then
if [[ -n "$default_org" && "$default_org" != "null" ]]; then
print_status "OK" "Default org set: $default_org"
else
print_status "WARN" "No default org configured"
echo " Set with: sf config set target-org <org-alias>"
fi
else
print_status "WARN" "Unable to check default org configuration"
fi
}
# Function to check plugins
check_plugins() {
if [[ "$VERBOSE" != "true" ]]; then
return 0
fi
print_status "INFO" "Checking installed plugins..."
if ! command_exists sf; then
return 1
fi
local plugins
if plugins=$(sf plugins --json 2>/dev/null | jq -r '.result[] | .name' 2>/dev/null); then
if [[ -n "$plugins" ]]; then
echo "$plugins" | while IFS= read -r plugin; do
echo " - $plugin"
done
else
echo " No additional plugins installed"
fi
else
echo " Unable to list plugins"
fi
}
# Function to check system requirements
check_system() {
if [[ "$VERBOSE" != "true" ]]; then
return 0
fi
print_status "INFO" "System information..."
echo " OS: $(uname -s) $(uname -r)"
echo " Shell: $SHELL"
if command_exists node; then
echo " Node.js: $(node --version)"
else
print_status "WARN" "Node.js not found (required for some SF CLI features)"
fi
if command_exists git; then
echo " Git: $(git --version | head -n 1)"
else
print_status "WARN" "Git not found (recommended for source control)"
fi
if command_exists jq; then
echo " jq: $(jq --version)"
else
print_status "WARN" "jq not found (recommended for JSON processing)"
fi
}
# Function to run diagnostics
run_diagnostics() {
print_status "INFO" "Running SF CLI diagnostics..."
if ! command_exists sf; then
return 1
fi
if sf doctor --json >/dev/null 2>&1; then
print_status "OK" "SF CLI doctor check passed"
else
print_status "WARN" "SF CLI doctor check had issues"
if [[ "$VERBOSE" == "true" ]]; then
echo " Run 'sf doctor' for detailed diagnostics"
fi
fi
}
# Parse command line arguments
VERBOSE=false
# Show help if no arguments provided
if [[ $# -eq 0 ]]; then
show_help
exit 0
fi
# Parse arguments manually for two-character options
while [[ $# -gt 0 ]]; do
case $1 in
-ve)
VERBOSE=true
shift
;;
-hp)
show_help
exit 0
;;
-*)
echo "Unknown option: $1" >&2
echo
show_help
exit 1
;;
*)
echo "Unexpected argument: $1" >&2
echo
show_help
exit 1
;;
esac
done
# Main execution
main() {
echo "🔍 Salesforce CLI Environment Check"
echo "=================================="
echo ""
local has_errors=false
# Core checks
if ! check_sf_installation; then
has_errors=true
fi
check_orgs || has_errors=true
check_default_org
# Additional checks for verbose mode
if [[ "$VERBOSE" == "true" ]]; then
echo ""
check_plugins
echo ""
check_system
echo ""
run_diagnostics
fi
echo ""
echo "=================================="
if [[ "$has_errors" == "true" ]]; then
print_status "ERROR" "Some critical issues found. Please address them before using the SF CLI wrappers."
exit 1
else
print_status "OK" "Environment check completed successfully!"
echo ""
echo "Your Salesforce CLI environment is ready to use with sf-cli-wrapper scripts."
echo "Available commands: sf-deploy, sf-dry-run, sf-web-open"
if [[ "$VERBOSE" != "true" ]]; then
echo ""
echo "Run 'sf-check -ve' for detailed system information."
fi
fi
}
# Run main function
main