Files
sf-cli-wrapper/sf-org-create
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

265 lines
6.3 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
show_help() {
cat <<'EOF'
sf-org-create — wrapper for smart scratch org creation
USAGE:
sf-org-create -al <ORG_NAME> [-dd <DAYS>] [-df <CONFIG_FILE>] [-st] [-tp <TEMPLATE>] [-hp]
OPTIONS:
-al Name/alias for the new scratch org (required)
-dd Duration in days (default: 7, max: 30)
-df Path to scratch org definition file (default: config/project-scratch-def.json)
-st Set as default org alias after creation
-tp Use predefined template (standard, testing, minimal, full)
-hp Show this help
EXAMPLES:
1) Create basic scratch org:
sf-org-create -al "MyDevOrg"
2) Create testing org for 1 day:
sf-org-create -al "QuickTest" -dd 1 -tp testing
3) Create with custom config and set as default:
sf-org-create -al "MainDev" -dd 14 -df "config/custom-scratch-def.json" -st
4) Create full-featured org:
sf-org-create -al "FullEnv" -tp full -dd 30
TEMPLATES:
- standard: Basic scratch org with common features
- testing: Optimized for running tests (minimal data)
- minimal: Bare-bones org for quick tasks
- full: All available features enabled
Notes:
- If no config file is specified, will look for config/project-scratch-def.json
- Templates override config file settings
- Org will be automatically opened in browser after creation
EOF
}
# Default values
ORG_NAME=""
DURATION=7
CONFIG_FILE=""
SET_DEFAULT=false
TEMPLATE=""
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)
ORG_NAME="$2"
shift 2
;;
-dd)
DURATION="$2"
shift 2
;;
-df)
CONFIG_FILE="$2"
shift 2
;;
-st)
SET_DEFAULT=true
shift
;;
-tp)
TEMPLATE="$2"
shift 2
;;
-hp)
show_help
exit 0
;;
*)
echo "Unknown option: $1" >&2
echo
show_help
exit 1
;;
esac
done
# Validate required parameters
if [[ -z "$ORG_NAME" ]]; then
echo "Error: Org alias (-al) is required." >&2
echo
show_help
exit 1
fi
# Validate duration
if ! [[ "$DURATION" =~ ^[0-9]+$ ]] || [[ "$DURATION" -lt 1 ]] || [[ "$DURATION" -gt 30 ]]; then
echo "Error: Duration must be a number between 1 and 30." >&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
# Create temporary config file for templates
create_template_config() {
local template="$1"
local temp_config="/tmp/sf-org-create-$$.json"
case "$template" in
"standard")
cat > "$temp_config" <<EOF
{
"orgName": "Standard Scratch Org",
"edition": "Developer",
"features": ["EnableSetPasswordInApi", "MultiCurrency", "AuthorApex"],
"settings": {
"lightningExperienceSettings": {
"enableS1DesktopEnabled": true
},
"mobileSettings": {
"enableS1EncryptedStoragePref2": false
}
}
}
EOF
;;
"testing")
cat > "$temp_config" <<EOF
{
"orgName": "Testing Scratch Org",
"edition": "Developer",
"features": ["EnableSetPasswordInApi", "AuthorApex"],
"settings": {
"lightningExperienceSettings": {
"enableS1DesktopEnabled": true
}
}
}
EOF
;;
"minimal")
cat > "$temp_config" <<EOF
{
"orgName": "Minimal Scratch Org",
"edition": "Developer",
"features": ["AuthorApex"]
}
EOF
;;
"full")
cat > "$temp_config" <<EOF
{
"orgName": "Full-Featured Scratch Org",
"edition": "Enterprise",
"features": [
"EnableSetPasswordInApi", "MultiCurrency", "AuthorApex",
"ContactsToMultipleAccounts", "ServiceCloud", "SalesCloud",
"Communities", "Sites", "PersonAccounts"
],
"settings": {
"lightningExperienceSettings": {
"enableS1DesktopEnabled": true
},
"mobileSettings": {
"enableS1EncryptedStoragePref2": false
},
"enhancedEmailSettings": {
"enableRestrictTlsAndRequireSSL": true
}
}
}
EOF
;;
*)
echo "Error: Unknown template '$template'. Available: standard, testing, minimal, full" >&2
exit 1
;;
esac
echo "$temp_config"
}
# Determine config file to use
FINAL_CONFIG=""
if [[ -n "$TEMPLATE" ]]; then
echo "📝 Creating scratch org definition from template: $TEMPLATE"
FINAL_CONFIG=$(create_template_config "$TEMPLATE")
elif [[ -n "$CONFIG_FILE" ]]; then
if [[ ! -f "$CONFIG_FILE" ]]; then
echo "Error: Config file '$CONFIG_FILE' not found." >&2
exit 1
fi
FINAL_CONFIG="$CONFIG_FILE"
elif [[ -f "config/project-scratch-def.json" ]]; then
echo "📝 Using default config: config/project-scratch-def.json"
FINAL_CONFIG="config/project-scratch-def.json"
else
echo "⚠️ No config file found, creating minimal scratch org"
FINAL_CONFIG=$(create_template_config "minimal")
fi
# Build the command
CMD=(sf org create scratch)
CMD+=(--definition-file "$FINAL_CONFIG")
CMD+=(--duration-days "$DURATION")
CMD+=(--alias "$ORG_NAME")
# Set as default if requested
if [[ "$SET_DEFAULT" == "true" ]]; then
CMD+=(--set-default)
fi
echo "🚀 Creating scratch org '$ORG_NAME' for $DURATION days..."
echo ">>> Running: ${CMD[*]}"
echo
if "${CMD[@]}"; then
echo
echo "✅ Scratch org '$ORG_NAME' created successfully!"
# Clean up temp config if we created one
if [[ -n "$TEMPLATE" ]]; then
rm -f "$FINAL_CONFIG"
fi
# Open the org
echo "🌐 Opening org in browser..."
sf org open --target-org "$ORG_NAME" || echo "⚠️ Could not open org in browser"
# Show org info
echo
echo "📊 Org Information:"
sf org display --target-org "$ORG_NAME" || echo "Could not display org info"
else
# Clean up temp config if we created one
if [[ -n "$TEMPLATE" ]]; then
rm -f "$FINAL_CONFIG"
fi
echo "❌ Failed to create scratch org"
exit 1
fi