feat: 增强三个 CLI 工具配置脚本
- Gemini CLI: 添加 GEMINI_MODEL 环境变量、settings.json 输出及备份逻辑 - Codex: 修复模型名称一致性 (gpt-5-codex)、添加 auth.json 备份逻辑 - Claude Code: 添加 VSCode 插件 config.json 配置支持 - CLAUDE.md: 重构文档,添加三个工具配置差异表 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -38,7 +38,7 @@ API_KEY=""
|
||||
|
||||
# Function to show help
|
||||
show_help() {
|
||||
cat << EOF
|
||||
cat <<EOF
|
||||
Codex Configuration Script for XCodeCLI
|
||||
|
||||
This script automatically tests multiple API endpoints and selects the working one:
|
||||
@@ -69,27 +69,27 @@ EOF
|
||||
# Parse command line arguments
|
||||
while [[ $# -gt 0 ]]; do
|
||||
case $1 in
|
||||
--key)
|
||||
API_KEY="$2"
|
||||
shift 2
|
||||
;;
|
||||
--test)
|
||||
TEST_ONLY=true
|
||||
shift
|
||||
;;
|
||||
--show)
|
||||
SHOW_SETTINGS=true
|
||||
shift
|
||||
;;
|
||||
--help)
|
||||
show_help
|
||||
exit 0
|
||||
;;
|
||||
*)
|
||||
print_error "Unknown option: $1"
|
||||
show_help
|
||||
exit 1
|
||||
;;
|
||||
--key)
|
||||
API_KEY="$2"
|
||||
shift 2
|
||||
;;
|
||||
--test)
|
||||
TEST_ONLY=true
|
||||
shift
|
||||
;;
|
||||
--show)
|
||||
SHOW_SETTINGS=true
|
||||
shift
|
||||
;;
|
||||
--help)
|
||||
show_help
|
||||
exit 0
|
||||
;;
|
||||
*)
|
||||
print_error "Unknown option: $1"
|
||||
show_help
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
@@ -168,127 +168,39 @@ test_api_connection() {
|
||||
create_codex_config() {
|
||||
local base_url="$1"
|
||||
local api_key="$2"
|
||||
|
||||
|
||||
# Create config directory if it doesn't exist
|
||||
mkdir -p "$HOME/.codex"
|
||||
|
||||
|
||||
# Create config.toml
|
||||
cat > "$HOME/.codex/config.toml" << EOF
|
||||
cat >"$HOME/.codex/config.toml" <<EOF
|
||||
model_provider = "xcodecli"
|
||||
model = "gpt-5-codex"
|
||||
model = "gpt-5.1-codex"
|
||||
model_reasoning_effort = "high"
|
||||
|
||||
[model_providers.xcodecli]
|
||||
name = "xcodecli"
|
||||
base_url = "${base_url}/v1"
|
||||
wire_api = "responses"
|
||||
env_key = "XCODECLI_API_KEY"
|
||||
env_key = "OPENAI_API_KEY"
|
||||
EOF
|
||||
|
||||
cat > "$HOME/.codex/auth.json" << EOF
|
||||
cat >"$HOME/.codex/auth.json" <<EOF
|
||||
{
|
||||
"OPENAI_API_KEY": "$api_key",
|
||||
"XCODECLI_API_KEY": "$api_key"
|
||||
"OPENAI_API_KEY": "$api_key"
|
||||
}
|
||||
EOF
|
||||
|
||||
|
||||
print_success "Codex configuration written to: $HOME/.codex/config.toml"
|
||||
print_success "Codex auth file written to: $HOME/.codex/auth.json"
|
||||
return 0
|
||||
}
|
||||
|
||||
# Function to set environment variable
|
||||
set_environment_variable() {
|
||||
local api_key="$1"
|
||||
|
||||
# Export for current session
|
||||
export XCODECLI_API_KEY="$api_key"
|
||||
|
||||
# Detect shell and add to appropriate config file
|
||||
local shell_config=""
|
||||
local shell_name=""
|
||||
|
||||
# First check $SHELL to determine user's default shell
|
||||
if [ -n "$SHELL" ]; then
|
||||
shell_name=$(basename "$SHELL")
|
||||
case "$shell_name" in
|
||||
bash)
|
||||
shell_config="$HOME/.bashrc"
|
||||
[ -f "$HOME/.bash_profile" ] && shell_config="$HOME/.bash_profile"
|
||||
;;
|
||||
zsh)
|
||||
shell_config="$HOME/.zshrc"
|
||||
;;
|
||||
fish)
|
||||
shell_config="$HOME/.config/fish/config.fish"
|
||||
;;
|
||||
*)
|
||||
shell_config="$HOME/.profile"
|
||||
;;
|
||||
esac
|
||||
# Fallback to checking version variables if $SHELL is not set
|
||||
elif [ -n "$BASH_VERSION" ]; then
|
||||
shell_config="$HOME/.bashrc"
|
||||
[ -f "$HOME/.bash_profile" ] && shell_config="$HOME/.bash_profile"
|
||||
elif [ -n "$ZSH_VERSION" ]; then
|
||||
shell_config="$HOME/.zshrc"
|
||||
elif [ -n "$FISH_VERSION" ]; then
|
||||
shell_config="$HOME/.config/fish/config.fish"
|
||||
else
|
||||
shell_config="$HOME/.profile"
|
||||
fi
|
||||
|
||||
print_info "Detected shell: ${shell_name:-$(basename $SHELL 2>/dev/null || echo 'unknown')}"
|
||||
print_info "Using config file: $shell_config"
|
||||
|
||||
# Handle Fish shell differently (uses 'set -x' instead of 'export')
|
||||
if [ "$shell_name" = "fish" ] || [[ "$shell_config" == *"fish"* ]]; then
|
||||
# Fish shell syntax
|
||||
if [ -f "$shell_config" ] && grep -q "set -x XCODECLI_API_KEY" "$shell_config"; then
|
||||
# Update existing
|
||||
if [[ "$OSTYPE" == "darwin"* ]]; then
|
||||
sed -i '' "s/set -x XCODECLI_API_KEY.*/set -x XCODECLI_API_KEY \"$api_key\"/" "$shell_config"
|
||||
else
|
||||
sed -i "s/set -x XCODECLI_API_KEY.*/set -x XCODECLI_API_KEY \"$api_key\"/" "$shell_config"
|
||||
fi
|
||||
print_info "Updated XCODECLI_API_KEY in $shell_config"
|
||||
else
|
||||
# Add new
|
||||
mkdir -p "$(dirname "$shell_config")"
|
||||
echo "" >> "$shell_config"
|
||||
echo "# API Router API key for Codex" >> "$shell_config"
|
||||
echo "set -x XCODECLI_API_KEY \"$api_key\"" >> "$shell_config"
|
||||
print_info "Added XCODECLI_API_KEY to $shell_config"
|
||||
fi
|
||||
else
|
||||
# Bash/Zsh/sh syntax
|
||||
if [ -f "$shell_config" ] && grep -q "export XCODECLI_API_KEY=" "$shell_config"; then
|
||||
# Update existing
|
||||
if [[ "$OSTYPE" == "darwin"* ]]; then
|
||||
# macOS
|
||||
sed -i '' "s/export XCODECLI_API_KEY=.*/export XCODECLI_API_KEY=\"$api_key\"/" "$shell_config"
|
||||
else
|
||||
# Linux
|
||||
sed -i "s/export XCODECLI_API_KEY=.*/export XCODECLI_API_KEY=\"$api_key\"/" "$shell_config"
|
||||
fi
|
||||
print_info "Updated XCODECLI_API_KEY in $shell_config"
|
||||
else
|
||||
# Add new
|
||||
echo "" >> "$shell_config"
|
||||
echo "# API Router API key for Codex" >> "$shell_config"
|
||||
echo "export XCODECLI_API_KEY=\"$api_key\"" >> "$shell_config"
|
||||
print_info "Added XCODECLI_API_KEY to $shell_config"
|
||||
fi
|
||||
fi
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
# Function to show current settings
|
||||
show_current_settings() {
|
||||
print_info "Current Codex settings:"
|
||||
echo "----------------------------------------"
|
||||
|
||||
|
||||
if [ -f "$HOME/.codex/config.toml" ]; then
|
||||
print_info "Configuration file: $HOME/.codex/config.toml"
|
||||
echo ""
|
||||
@@ -297,18 +209,20 @@ show_current_settings() {
|
||||
else
|
||||
print_info "No configuration file found at $HOME/.codex/config.toml"
|
||||
fi
|
||||
|
||||
|
||||
echo "----------------------------------------"
|
||||
print_info "Environment variable:"
|
||||
|
||||
if [ ! -z "$XCODECLI_API_KEY" ]; then
|
||||
local masked_key="${XCODECLI_API_KEY:0:8}...${XCODECLI_API_KEY: -4}"
|
||||
print_info "XCODECLI_API_KEY: $masked_key"
|
||||
else
|
||||
print_info "XCODECLI_API_KEY: (not set)"
|
||||
|
||||
if [ -f "$HOME/.codex/auth.json" ]; then
|
||||
print_info "Auth file: $HOME/.codex/auth.json"
|
||||
echo "----------------------------------------"
|
||||
local api_key
|
||||
api_key=$(grep -o '"OPENAI_API_KEY"[[:space:]]*:[[:space:]]*"[^"]*"' "$HOME/.codex/auth.json" | sed 's/.*: *"//' | sed 's/"$//')
|
||||
if [ -n "$api_key" ]; then
|
||||
local masked_key="${api_key:0:8}...${api_key: -4}"
|
||||
print_info "OPENAI_API_KEY: $masked_key"
|
||||
fi
|
||||
echo "----------------------------------------"
|
||||
fi
|
||||
|
||||
echo "----------------------------------------"
|
||||
}
|
||||
|
||||
# Main function
|
||||
@@ -390,28 +304,12 @@ main() {
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Set environment variable
|
||||
if ! set_environment_variable "$API_KEY"; then
|
||||
print_warning "Failed to set environment variable automatically"
|
||||
print_info "Please set manually: export XCODECLI_API_KEY=\"$API_KEY\""
|
||||
fi
|
||||
|
||||
echo ""
|
||||
print_success "Codex has been configured successfully!"
|
||||
print_info "You can now use Codex with your XCodeCLI API router."
|
||||
print_info ""
|
||||
print_info "To apply the environment variable in your current session, run:"
|
||||
|
||||
# Provide correct command based on detected shell
|
||||
local current_shell=$(basename "$SHELL" 2>/dev/null || echo "bash")
|
||||
if [ "$current_shell" = "fish" ]; then
|
||||
print_info " set -x XCODECLI_API_KEY \"$API_KEY\""
|
||||
else
|
||||
print_info " export XCODECLI_API_KEY=\"$API_KEY\""
|
||||
fi
|
||||
print_info "Or restart your terminal."
|
||||
print_info ""
|
||||
print_info "Configuration file: $HOME/.codex/config.toml"
|
||||
print_info "Auth file: $HOME/.codex/auth.json"
|
||||
|
||||
# Show current settings
|
||||
echo ""
|
||||
|
||||
Reference in New Issue
Block a user