Some checks failed
Deploy to Cloudflare Pages / deploy (push) Has been cancelled
- 新增 docs/ 目录,使用 VitePress 构建文档站 - 添加 .gitea/workflows/deploy.yaml,部署到 Cloudflare Pages - 新增 setup.sh 一站式安装脚本(兼容 macOS bash 3.x) - 修复 Gemini CLI 脚本认证头(改用 Authorization: Bearer) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
322 lines
8.6 KiB
Bash
Executable File
322 lines
8.6 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# Gemini CLI Configuration Script for XCodeCLI
|
|
# This script configures Gemini CLI to use your XCodeCLI instance
|
|
# Automatically tests multiple API endpoints and selects the working one
|
|
|
|
set -e
|
|
|
|
# Colors for output
|
|
RED='\033[0;31m'
|
|
GREEN='\033[0;32m'
|
|
YELLOW='\033[1;33m'
|
|
BLUE='\033[0;34m'
|
|
NC='\033[0m' # No Color
|
|
|
|
# Configuration
|
|
DEFAULT_BASE_URL="https://api2.xcodecli.com"
|
|
GEMINI_CONFIG_DIR="$HOME/.gemini"
|
|
GEMINI_ENV_FILE="$GEMINI_CONFIG_DIR/.env"
|
|
|
|
# Function to print colored output
|
|
print_info() {
|
|
echo -e "${BLUE}[INFO]${NC} $1"
|
|
}
|
|
|
|
print_success() {
|
|
echo -e "${GREEN}[SUCCESS]${NC} $1"
|
|
}
|
|
|
|
print_warning() {
|
|
echo -e "${YELLOW}[WARNING]${NC} $1"
|
|
}
|
|
|
|
print_error() {
|
|
echo -e "${RED}[ERROR]${NC} $1"
|
|
}
|
|
|
|
# Function to backup existing settings
|
|
backup_settings() {
|
|
if [ -f "$GEMINI_ENV_FILE" ]; then
|
|
local backup_file="${GEMINI_ENV_FILE}.backup.$(date +%Y%m%d_%H%M%S)"
|
|
cp "$GEMINI_ENV_FILE" "$backup_file"
|
|
print_info "Backed up existing settings to: $backup_file"
|
|
fi
|
|
local settings_json_path="$GEMINI_CONFIG_DIR/settings.json"
|
|
if [ -f "$settings_json_path" ]; then
|
|
local backup_file="${settings_json_path}.backup.$(date +%Y%m%d_%H%M%S)"
|
|
cp "$settings_json_path" "$backup_file"
|
|
print_info "Backed up existing settings to: $backup_file"
|
|
fi
|
|
}
|
|
|
|
# Function to create settings directory
|
|
create_settings_dir() {
|
|
if [ ! -d "$GEMINI_CONFIG_DIR" ]; then
|
|
mkdir -p "$GEMINI_CONFIG_DIR"
|
|
print_info "Created Gemini configuration directory: $GEMINI_CONFIG_DIR"
|
|
fi
|
|
}
|
|
|
|
# Function to validate API key format
|
|
validate_api_key() {
|
|
local api_key="$1"
|
|
if [[ ! "$api_key" =~ ^[A-Za-z0-9_-]+$ ]]; then
|
|
print_error "Invalid API key format. API key should contain only alphanumeric characters, hyphens, and underscores."
|
|
return 1
|
|
fi
|
|
return 0
|
|
}
|
|
|
|
# Function to test API connection and return working base URL
|
|
test_api_connection() {
|
|
local api_key="$1"
|
|
|
|
local test_urls=(
|
|
"https://api2.xcodecli.com"
|
|
"https://api.xcodecli.com"
|
|
)
|
|
|
|
print_info "Testing API connections..." >&2
|
|
|
|
for base_url in "${test_urls[@]}"; do
|
|
print_info "Testing $base_url..." >&2
|
|
|
|
local test_endpoint="$base_url/v1/models"
|
|
|
|
# Test with Authorization Bearer header (XCodeCLI API style)
|
|
local response
|
|
response=$(curl -s -w "%{http_code}" -o /tmp/gemini_test_response \
|
|
-X GET "$test_endpoint" \
|
|
-H "Content-Type: application/json" \
|
|
-H "Authorization: Bearer $api_key" \
|
|
2>/dev/null || echo "000")
|
|
|
|
if [ "$response" = "200" ]; then
|
|
# Check if response contains models (supports both {data:[]} and {models:[]} formats)
|
|
if grep -qE '"(data|models)"' /tmp/gemini_test_response 2>/dev/null; then
|
|
local model_count
|
|
model_count=$(grep -oE '"id"[[:space:]]*:' /tmp/gemini_test_response | wc -l | tr -d ' ')
|
|
print_success "API connection successful! Found $model_count models at $base_url" >&2
|
|
rm -f /tmp/gemini_test_response
|
|
echo "$base_url"
|
|
return 0
|
|
else
|
|
print_warning "API responded but no models found at $base_url" >&2
|
|
fi
|
|
elif [ "$response" = "401" ]; then
|
|
print_warning "API key authentication failed for $base_url" >&2
|
|
elif [ "$response" = "000" ]; then
|
|
print_warning "Cannot connect to $base_url" >&2
|
|
else
|
|
print_warning "API test failed for $base_url with HTTP status: $response" >&2
|
|
fi
|
|
|
|
rm -f /tmp/gemini_test_response
|
|
done
|
|
|
|
print_error "All API connections failed. Please check your API key and internet connection." >&2
|
|
return 1
|
|
}
|
|
|
|
# Function to create Gemini CLI settings file
|
|
create_settings_file() {
|
|
local base_url="$1"
|
|
local api_key="$2"
|
|
|
|
# Write to .env file
|
|
cat <<EOF > "$GEMINI_ENV_FILE"
|
|
GOOGLE_GEMINI_BASE_URL="$base_url"
|
|
GEMINI_API_KEY="$api_key"
|
|
GEMINI_MODEL="gemini-3-pro-preview"
|
|
EOF
|
|
print_success "Gemini CLI settings written to: $GEMINI_ENV_FILE"
|
|
|
|
# Write to settings.json
|
|
local settings_json_path="$GEMINI_CONFIG_DIR/settings.json"
|
|
cat <<EOF > "$settings_json_path"
|
|
{
|
|
"general": {
|
|
"previewFeatures": true
|
|
},
|
|
"security": {
|
|
"auth": {
|
|
"selectedType": "gemini-api-key"
|
|
}
|
|
}
|
|
}
|
|
EOF
|
|
print_success "Gemini CLI settings written to: $settings_json_path"
|
|
}
|
|
|
|
# Function to display current settings
|
|
display_settings() {
|
|
if [ -f "$GEMINI_ENV_FILE" ]; then
|
|
print_info "Current Gemini CLI settings file ($GEMINI_ENV_FILE):"
|
|
echo "----------------------------------------"
|
|
cat "$GEMINI_ENV_FILE"
|
|
echo "----------------------------------------"
|
|
else
|
|
print_info "No existing Gemini CLI .env file found."
|
|
fi
|
|
}
|
|
|
|
# Main function
|
|
main() {
|
|
print_info "Gemini CLI Configuration Script for XCodeCLI"
|
|
echo "======================================================="
|
|
echo
|
|
|
|
# Parse command line arguments
|
|
local api_key=""
|
|
local test_only=false
|
|
local show_settings=false
|
|
|
|
# Check for environment variable API_KEY
|
|
if [ -n "$API_KEY" ]; then
|
|
api_key="$API_KEY"
|
|
fi
|
|
|
|
while [[ $# -gt 0 ]]; do
|
|
case $1 in
|
|
-k|--key)
|
|
api_key="$2"
|
|
shift 2
|
|
;;
|
|
-t|--test)
|
|
test_only=true
|
|
shift
|
|
;;
|
|
-s|--show)
|
|
show_settings=true
|
|
shift
|
|
;;
|
|
-h|--help)
|
|
cat <<EOF
|
|
Usage: $0 [OPTIONS]
|
|
|
|
This script automatically tests multiple API endpoints and selects the working one:
|
|
- https://api2.xcodecli.com
|
|
- https://api.xcodecli.com
|
|
|
|
Options:
|
|
-k, --key KEY Set the API key
|
|
-t, --test Test API connections only (requires -k)
|
|
-s, --show Show current settings and exit
|
|
-h, --help Show this help message
|
|
|
|
Examples:
|
|
$0 --key your-api-key-here
|
|
$0 --test --key your-api-key-here
|
|
$0 --show
|
|
|
|
Interactive mode (no arguments):
|
|
$0
|
|
|
|
Environment Variables:
|
|
API_KEY API key for authentication
|
|
EOF
|
|
exit 0
|
|
;;
|
|
*)
|
|
print_error "Unknown option: $1"
|
|
print_info "Use --help for usage information"
|
|
exit 1
|
|
;;
|
|
esac
|
|
done
|
|
|
|
# Show settings and exit if requested
|
|
if [ "$show_settings" = true ]; then
|
|
display_settings
|
|
exit 0
|
|
fi
|
|
|
|
# Interactive mode if no API key provided
|
|
if [ -z "$api_key" ]; then
|
|
print_info "Interactive setup mode"
|
|
echo
|
|
|
|
# Get API key
|
|
while [ -z "$api_key" ]; do
|
|
read -p "Enter your API key: " api_key
|
|
if [ -z "$api_key" ]; then
|
|
print_warning "API key is required"
|
|
elif ! validate_api_key "$api_key"; then
|
|
api_key=""
|
|
fi
|
|
done
|
|
fi
|
|
|
|
# Validate inputs
|
|
if [ -z "$api_key" ]; then
|
|
print_error "API key is required"
|
|
print_info "Use --help for usage information"
|
|
exit 1
|
|
fi
|
|
|
|
# Validate API key
|
|
if ! validate_api_key "$api_key"; then
|
|
exit 1
|
|
fi
|
|
|
|
print_info "API Key: ${api_key:0:8}...${api_key: -4}"
|
|
echo
|
|
|
|
# Test API connection and get working base URL
|
|
local base_url
|
|
base_url=$(test_api_connection "$api_key")
|
|
|
|
if [ $? -ne 0 ] || [ -z "$base_url" ]; then
|
|
if [ "$test_only" = true ]; then
|
|
exit 1
|
|
fi
|
|
|
|
read -p "All API tests failed. Continue anyway? (y/N): " -n 1 -r
|
|
echo
|
|
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
|
|
print_info "Setup cancelled"
|
|
exit 1
|
|
fi
|
|
|
|
# If user chooses to continue anyway, use default URL
|
|
base_url="$DEFAULT_BASE_URL"
|
|
print_warning "Using default URL: $base_url"
|
|
else
|
|
print_info "Selected working base URL: $base_url"
|
|
fi
|
|
|
|
# Exit if test only
|
|
if [ "$test_only" = true ]; then
|
|
print_success "API test completed successfully"
|
|
exit 0
|
|
fi
|
|
|
|
# Create settings directory
|
|
create_settings_dir
|
|
|
|
# Backup existing settings
|
|
backup_settings
|
|
|
|
# Create new settings file
|
|
create_settings_file "$base_url" "$api_key"
|
|
|
|
echo
|
|
print_success "Gemini CLI has been configured successfully!"
|
|
print_info "You can now use Gemini CLI with your API router."
|
|
print_info ""
|
|
print_info "To verify the setup, run:"
|
|
print_info " gemini --version"
|
|
print_info ""
|
|
print_info "Configuration file location: $GEMINI_ENV_FILE"
|
|
|
|
if [ -f "$GEMINI_ENV_FILE" ]; then
|
|
echo
|
|
print_info "Current settings:"
|
|
cat "$GEMINI_ENV_FILE"
|
|
fi
|
|
}
|
|
|
|
# Run main function
|
|
main "$@"
|