Files
sf-cli-wrapper/sf-apex-run
2025-08-28 16:34:16 +08:00

174 lines
4.1 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 -o <ORG_ALIAS> (-f <FILE> | -c <CODE>) [-h]
OPTIONS:
-o Org alias or username to execute in (required)
-f Path to Apex file to execute
-c Apex code string to execute directly
-h Show this help
EXAMPLES:
1) Execute Apex from file:
sf-apex-run -o DEMO-ORG -f "scripts/debug.apex"
2) Execute inline Apex code:
sf-apex-run -o DEMO-ORG -c "System.debug('Hello World!');"
3) Execute complex Apex from file:
sf-apex-run -o DEMO-ORG -f "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 -f (file) or -c (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
while getopts ":o:f:c:h" opt; do
case "$opt" in
o) ORG="$OPTARG" ;;
f) APEX_FILE="$OPTARG" ;;
c) APEX_CODE="$OPTARG" ;;
h) show_help; exit 0 ;;
\?) echo "Unknown option: -$OPTARG" >&2; echo; show_help; exit 1 ;;
:) echo "Option -$OPTARG requires an argument." >&2; echo; show_help; exit 1 ;;
esac
done
# Validate required parameters
if [[ -z "$ORG" ]]; then
echo "Error: Org (-o) 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 -f (file) or -c (code)." >&2
echo
show_help
exit 1
fi
if [[ -n "$APEX_FILE" && -n "$APEX_CODE" ]]; then
echo "Error: Cannot specify both -f (file) and -c (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
echo " Code: ${APEX_CODE:0:50}${#APEX_CODE > 50 ? '...' : ''}"
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 -o \"$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"