Files
sf-cli-wrapper/sf-apex-run
reynold 1ae8df8561 Enforce strict two-character option scheme across all wrapper scripts
- Removed long options from input parsing in all bash scripts
- Updated all help texts to show only two-character options
- Fixed error messages to reference short options only
- All scripts now reject long options like --help, --verbose, --target-org
- Maintained internal use of long sf CLI commands (e.g., --target-org passed to sf)
- Updated README.md documentation to reflect two-character scheme only
- Scripts affected: sf-retrieve, sf-test-run, sf-data-import, sf-data-export
- PowerShell scripts already used correct two-character parameter scheme
- All wrapper scripts now have consistent user interface

This ensures strict consistency in the two-character option scheme
while maintaining backward compatibility for the sf CLI commands themselves.
2025-08-28 22:07:34 +08:00

219 lines
4.8 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
show_help() {
cat <<'EOF'
sf-apex-run — wrapper for executing anonymous Apex code
USAGE:
sf-apex-run -to <ORG_ALIAS> (-fl <FILE> | -cd <CODE>) [-hp]
OPTIONS:
-to Target org alias or username to execute in (required)
-fl File path to Apex file to execute
-cd Code string to execute directly
-hp Show this help
EXAMPLES:
1) Execute Apex from file:
sf-apex-run -to DEMO-ORG -fl "scripts/debug.apex"
2) Execute inline Apex code:
sf-apex-run -to DEMO-ORG -cd "System.debug('Hello World!');"
3) Execute complex Apex from file:
sf-apex-run -to DEMO-ORG -fl "scripts/data-cleanup.apex"
COMMON USE CASES:
- Running debug scripts
- Data manipulation and cleanup
- Testing code snippets
- One-time data fixes
- System diagnostics
APEX FILE EXAMPLES:
Create a file like 'debug.apex':
```
List<Account> accounts = [SELECT Id, Name FROM Account LIMIT 5];
for (Account acc : accounts) {
System.debug('Account: ' + acc.Name);
}
System.debug('Total accounts: ' + accounts.size());
```
Notes:
- Either -fl (file) or -cd (code) must be specified, but not both
- Apex files should contain valid Apex code
- Output will show execution results and any debug logs
- Be careful with DML operations - they will actually execute!
EOF
}
# Default values
ORG=""
APEX_FILE=""
APEX_CODE=""
if [[ $# -eq 0 ]]; then
show_help
exit 0
fi
# Parse arguments manually for two-character options
while [[ $# -gt 0 ]]; do
case $1 in
-to)
if [[ -n "${2:-}" && ! "$2" =~ ^- ]]; then
ORG="$2"
shift 2
else
echo "Error: -to requires a target org argument" >&2
show_help
exit 1
fi
;;
-fl)
if [[ -n "${2:-}" && ! "$2" =~ ^- ]]; then
APEX_FILE="$2"
shift 2
else
echo "Error: -fl requires a file path argument" >&2
show_help
exit 1
fi
;;
-cd)
if [[ -n "${2:-}" ]]; then
APEX_CODE="$2"
shift 2
else
echo "Error: -cd requires a code argument" >&2
show_help
exit 1
fi
;;
-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
# Validate required parameters
if [[ -z "$ORG" ]]; then
echo "Error: Target org (-to) is required." >&2
echo
show_help
exit 1
fi
# Validate that either file or code is specified, but not both
if [[ -z "$APEX_FILE" && -z "$APEX_CODE" ]]; then
echo "Error: Must specify either -fl (file) or -cd (code)." >&2
echo
show_help
exit 1
fi
if [[ -n "$APEX_FILE" && -n "$APEX_CODE" ]]; then
echo "Error: Cannot specify both -fl (file) and -cd (code). Use one method." >&2
echo
show_help
exit 1
fi
# Validate file exists if specified
if [[ -n "$APEX_FILE" && ! -f "$APEX_FILE" ]]; then
echo "Error: Apex file '$APEX_FILE' not found." >&2
exit 1
fi
# Silent environment check
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
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
# Build the command
CMD=(sf apex run)
CMD+=(--target-org "$ORG")
# Create temporary file for code if needed
TEMP_FILE=""
if [[ -n "$APEX_CODE" ]]; then
TEMP_FILE=$(mktemp -t "apex-code-XXXXXX.apex")
echo "$APEX_CODE" > "$TEMP_FILE"
CMD+=(--file "$TEMP_FILE")
else
CMD+=(--file "$APEX_FILE")
fi
# Show what we're executing
echo "⚡ Executing Apex code in org '$ORG'..."
if [[ -n "$APEX_FILE" ]]; then
echo " File: $APEX_FILE"
if [[ -f "$APEX_FILE" ]]; then
echo " Lines: $(wc -l < "$APEX_FILE")"
fi
elif [[ -n "$APEX_CODE" ]]; then
if [[ ${#APEX_CODE} -gt 50 ]]; then
echo " Code: ${APEX_CODE:0:50}..."
else
echo " Code: $APEX_CODE"
fi
fi
echo
echo ">>> Running: ${CMD[*]}"
echo
# Execute the command
if "${CMD[@]}"; then
echo
echo "✅ Apex execution completed successfully!"
# Show helpful next steps
echo
echo "💡 Next steps:"
echo " - Check debug logs: sf apex get log --target-org \"$ORG\""
echo " - View recent logs: sf-logs-tail -to \"$ORG\" (if available)"
else
RESULT=$?
echo
echo "❌ Apex execution failed"
echo "Check the output above for compilation or runtime errors."
# Clean up temp file
[[ -n "$TEMP_FILE" && -f "$TEMP_FILE" ]] && rm -f "$TEMP_FILE"
exit $RESULT
fi
# Clean up temp file
[[ -n "$TEMP_FILE" && -f "$TEMP_FILE" ]] && rm -f "$TEMP_FILE"