Files
unlimitedcoding/codex/ucodex_install.sh
delta-cloud-208e 3b81f61192 fix(codex): set OPENAI_API_KEY in shell profile + current session
Install/update scripts now:
- Export env vars for current session (fixes "Missing OPENAI_API_KEY")
- Write to ~/.bashrc or ~/.zshrc for persistence
- Handle sudo (detect real user via SUDO_USER)
- Remove stale entries before writing

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-08 10:32:20 +00:00

105 lines
3.5 KiB
Bash
Executable File

#!/bin/bash
# UnlimitedCoding — Codex CLI Installer
# Downloads Codex binary from GitHub + applies config patches
#
# Usage:
# curl -fsSL https://git.sensey24.ru/.../ucodex_install.sh | sudo bash
set -e
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
CYAN='\033[0;36m'
BOLD='\033[1m'
NC='\033[0m'
echo -e "${BOLD}=== UnlimitedCoding — Codex CLI Installer ===${NC}"
# Check prerequisites
for cmd in python3 curl; do
if ! command -v "$cmd" &>/dev/null; then
echo -e "${RED}Error: $cmd is required but not found${NC}"
exit 1
fi
done
# Check Python version (need 3.11+ for tomllib)
PY_VER=$(python3 -c "import sys; print(f'{sys.version_info.major}.{sys.version_info.minor}')")
PY_MAJOR=$(echo "$PY_VER" | cut -d. -f1)
PY_MINOR=$(echo "$PY_VER" | cut -d. -f2)
if [ "$PY_MAJOR" -lt 3 ] || ([ "$PY_MAJOR" -eq 3 ] && [ "$PY_MINOR" -lt 11 ]); then
echo -e "${RED}Error: Python 3.11+ required (found $PY_VER)${NC}"
exit 1
fi
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Step 1: Install/update Codex binary
echo -e "\n${BOLD}Step 1: Installing Codex CLI binary...${NC}"
if [ -f "$SCRIPT_DIR/update-codex.sh" ]; then
bash "$SCRIPT_DIR/update-codex.sh"
else
echo -e "${RED}update-codex.sh not found${NC}"
exit 1
fi
# Step 2: Apply config patches
echo -e "\n${BOLD}Step 2: Applying config patches...${NC}"
if [ ! -f "$SCRIPT_DIR/codex_config.json" ]; then
echo -e "${YELLOW}codex_config.json not found, copying example...${NC}"
cp "$SCRIPT_DIR/codex_config.example.json" "$SCRIPT_DIR/codex_config.json"
echo -e "${YELLOW}Edit codex_config.json with your API endpoint and key, then re-run.${NC}"
exit 1
fi
python3 "$SCRIPT_DIR/codex_patcher.py" --apply --config "$SCRIPT_DIR/codex_config.json"
# Step 3: Set env vars for current and future sessions
echo -e "\n${BOLD}Step 3: Setting environment variables...${NC}"
API_KEY=$(python3 -c "import json; print(json.load(open('$SCRIPT_DIR/codex_config.json'))['api_key'])")
BASE_URL=$(python3 -c "import json; print(json.load(open('$SCRIPT_DIR/codex_config.json'))['base_url'])")
# Export for current session
export OPENAI_API_KEY="$API_KEY"
export OPENAI_BASE_URL="${BASE_URL}/v1"
# Determine real user (even when running under sudo)
REAL_USER="${SUDO_USER:-$USER}"
REAL_HOME=$(eval echo "~$REAL_USER")
# Write to user's shell profile for persistence
PROFILE=""
if [ -f "$REAL_HOME/.bashrc" ]; then
PROFILE="$REAL_HOME/.bashrc"
elif [ -f "$REAL_HOME/.zshrc" ]; then
PROFILE="$REAL_HOME/.zshrc"
elif [ -f "$REAL_HOME/.profile" ]; then
PROFILE="$REAL_HOME/.profile"
fi
if [ -n "$PROFILE" ]; then
# Remove old entries if present
sed -i '/^export OPENAI_API_KEY=/d' "$PROFILE" 2>/dev/null || true
sed -i '/^export OPENAI_BASE_URL=/d' "$PROFILE" 2>/dev/null || true
# Add new entries
echo "export OPENAI_API_KEY=\"$API_KEY\"" >> "$PROFILE"
echo "export OPENAI_BASE_URL=\"${BASE_URL}/v1\"" >> "$PROFILE"
echo -e " ${GREEN}Env vars written to $PROFILE${NC}"
else
echo -e " ${YELLOW}No shell profile found. Add manually:${NC}"
echo -e " export OPENAI_API_KEY=\"$API_KEY\""
echo -e " export OPENAI_BASE_URL=\"${BASE_URL}/v1\""
fi
# Step 4: Validate
echo -e "\n${BOLD}Step 4: Validating...${NC}"
python3 "$SCRIPT_DIR/update_codex_patcher.py" --validate
echo -e "\n${GREEN}=== Installation complete! ===${NC}"
echo -e "Run: ${CYAN}codex${NC} to start"
if [ -n "$PROFILE" ]; then
echo -e "${YELLOW}Note: Run 'source $PROFILE' or restart shell for env vars to take effect.${NC}"
fi