#!/bin/bash # Claude Code Configuration Script for xcodecli # This script configures Claude Code to use your xcodecli instance 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://api.xcodecli.com" CLAUDE_CONFIG_DIR="$HOME/.claude" CLAUDE_SETTINGS_FILE="$CLAUDE_CONFIG_DIR/settings.json" # 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 check if jq is installed check_jq() { if ! command -v jq &> /dev/null; then print_error "jq is required but not installed." print_info "Please install jq:" print_info " macOS: brew install jq" print_info " Ubuntu/Debian: sudo apt-get install jq" print_info " CentOS/RHEL: sudo yum install jq" exit 1 fi } # Function to backup existing settings backup_settings() { if [ -f "$CLAUDE_SETTINGS_FILE" ]; then local backup_file="${CLAUDE_SETTINGS_FILE}.backup.$(date +%Y%m%d_%H%M%S)" cp "$CLAUDE_SETTINGS_FILE" "$backup_file" print_info "Backed up existing settings to: $backup_file" fi } # Function to create settings directory create_settings_dir() { if [ ! -d "$CLAUDE_CONFIG_DIR" ]; then mkdir -p "$CLAUDE_CONFIG_DIR" print_info "Created Claude configuration directory: $CLAUDE_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 test_api_connection() { local base_url="$1" local api_key="$2" print_info "Testing API connection..." # Determine the correct endpoint based on whether this is a team URL local test_endpoint local balance_field if [[ "$base_url" == */team ]]; then test_endpoint="$base_url/api/v1/team/stats/spending" balance_field="daily_remaining" else test_endpoint="$base_url/api/v1/claude/balance" balance_field="balance" fi # Test a simple request to the API local response response=$(curl -s -w "%{http_code}" -o /tmp/claude_test_response \ -X GET "$test_endpoint" \ -H "Content-Type: application/json" \ -H "X-API-Key: $api_key" \ 2>/dev/null || echo "000") if [ "$response" = "200" ]; then local balance balance=$(cat /tmp/claude_test_response | jq -r ".${balance_field}" 2>/dev/null || echo "unknown") if [[ "$base_url" == */team ]]; then print_success "API connection successful! Daily remaining: \$${balance}" else print_success "API connection successful! Current balance: \$${balance}" fi rm -f /tmp/claude_test_response return 0 elif [ "$response" = "401" ]; then print_error "API key authentication failed. Please check your API key." rm -f /tmp/claude_test_response return 1 elif [ "$response" = "000" ]; then print_error "Cannot connect to API server. Please check the URL and your internet connection." rm -f /tmp/claude_test_response return 1 else print_error "API test failed with HTTP status: $response" rm -f /tmp/claude_test_response return 1 fi } # Function to create Claude Code settings create_settings() { local base_url="$1" local api_key="$2" local settings_json settings_json=$(cat < /dev/null 2>&1; then print_error "Generated settings JSON is invalid" return 1 fi # Write settings file echo "$settings_json" > "$CLAUDE_SETTINGS_FILE" print_success "Claude Code settings written to: $CLAUDE_SETTINGS_FILE" } # Function to display current settings display_settings() { if [ -f "$CLAUDE_SETTINGS_FILE" ]; then print_info "Current Claude Code settings:" echo "----------------------------------------" cat "$CLAUDE_SETTINGS_FILE" | jq . echo "----------------------------------------" else print_info "No existing Claude Code settings found." fi } # Main function main() { print_info "Claude Code Configuration Script for xcodecli" echo "=======================================================" echo # Check dependencies check_jq # Parse command line arguments local base_url="" local api_key="" local test_only=false local show_settings=false while [[ $# -gt 0 ]]; do case $1 in -u|--url) base_url="$2" shift 2 ;; -k|--key) api_key="$2" shift 2 ;; -t|--test) test_only=true shift ;; -s|--show) show_settings=true shift ;; -h|--help) cat <