fix: ocp-connect writes to all shell rc files + system-level env vars

- Writes to both .bashrc and .zshrc on macOS (covers both shells)
- macOS: launchctl setenv for GUI apps and daemons
- Linux: ~/.config/environment.d/ocp.conf for systemd user services
- Ensures IDEs and daemons can discover OCP models

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-12 12:58:27 +10:00
co-authored by Claude Opus 4.6
parent cb6c2a8b5f
commit 7f46405152
2 changed files with 67 additions and 27 deletions
+1 -1
View File
@@ -152,7 +152,7 @@ OCP Connect
source /home/user/.bashrc source /home/user/.bashrc
``` ```
After running, reload your shell (`source ~/.bashrc` or `source ~/.zshrc`) and your IDE will automatically use the remote OCP. After running, reload your shell (`source ~/.bashrc` or `source ~/.zshrc`). On macOS, the script also sets `launchctl setenv` so GUI apps and daemons can see the env vars immediately (resets on reboot — re-run `ocp-connect` after restart).
**Manual setup** — if you prefer not to use the script: **Manual setup** — if you prefer not to use the script:
```bash ```bash
+66 -26
View File
@@ -141,22 +141,29 @@ main() {
echo " ✓ API accessible ($model_count models available)" echo " ✓ API accessible ($model_count models available)"
echo "" echo ""
# Step 5: Detect shell rc file # Step 5: Detect shell rc files and OS
local rc_file local rc_files=()
if [[ "${SHELL:-}" == */zsh ]]; then local is_mac=false
rc_file="$HOME/.zshrc" [[ "$(uname)" == "Darwin" ]] && is_mac=true
elif [[ "${SHELL:-}" == */fish ]]; then
# Write to all relevant rc files
if [[ "${SHELL:-}" == */fish ]]; then
echo " Note: fish shell detected. Writing to ~/.bashrc — add to fish config manually." echo " Note: fish shell detected. Writing to ~/.bashrc — add to fish config manually."
rc_file="$HOME/.bashrc" rc_files+=("$HOME/.bashrc")
else else
rc_file="$HOME/.bashrc" # Always write both on macOS (default shell is zsh but some tools source bashrc)
[[ -f "$HOME/.bashrc" || "${SHELL:-}" == */bash ]] && rc_files+=("$HOME/.bashrc")
[[ -f "$HOME/.zshrc" || "${SHELL:-}" == */zsh ]] && rc_files+=("$HOME/.zshrc")
# If neither exists, create for current shell
[[ ${#rc_files[@]} -eq 0 ]] && rc_files+=("$HOME/.${SHELL##*/}rc")
fi fi
# Step 6: Remove any previously written OCP LAN lines (idempotent) # Step 6: Remove any previously written OCP LAN lines (idempotent) from all rc files
if [[ -f "$rc_file" ]]; then for rc_file in "${rc_files[@]}"; do
local tmp_rc if [[ -f "$rc_file" ]]; then
tmp_rc=$(mktemp) local tmp_rc
if python3 - "$rc_file" "$tmp_rc" <<'PYEOF' tmp_rc=$(mktemp)
if python3 - "$rc_file" "$tmp_rc" <<'PYEOF'
import sys import sys
src, dst = sys.argv[1], sys.argv[2] src, dst = sys.argv[1], sys.argv[2]
lines = open(src).readlines() lines = open(src).readlines()
@@ -173,27 +180,60 @@ for line in lines:
out.append(line) out.append(line)
open(dst, 'w').writelines(out) open(dst, 'w').writelines(out)
PYEOF PYEOF
then then
cp "$tmp_rc" "$rc_file" cp "$tmp_rc" "$rc_file"
fi
rm -f "$tmp_rc"
fi fi
rm -f "$tmp_rc" done
fi
# Step 7: Append new config # Step 7: Append new config to all rc files
{ for rc_file in "${rc_files[@]}"; do
echo "" {
echo "# OCP LAN (added by ocp connect)" echo ""
echo "export OPENAI_BASE_URL=$base_url/v1" echo "# OCP LAN (added by ocp connect)"
if [[ -n "$key" ]]; then echo "export OPENAI_BASE_URL=$base_url/v1"
echo "export OPENAI_API_KEY=$key" if [[ -n "$key" ]]; then
fi echo "export OPENAI_API_KEY=$key"
} >> "$rc_file" fi
} >> "$rc_file"
done
echo " Written to $rc_file:" echo " Shell config:"
for rc_file in "${rc_files[@]}"; do
echo " ✓ $(basename "$rc_file")"
done
echo " OPENAI_BASE_URL=$base_url/v1" echo " OPENAI_BASE_URL=$base_url/v1"
if [[ -n "$key" ]]; then if [[ -n "$key" ]]; then
echo " OPENAI_API_KEY=${key:0:8}..." echo " OPENAI_API_KEY=${key:0:8}..."
fi fi
# Step 7b: System-level env vars (for IDEs, daemons, GUI apps)
if $is_mac; then
# macOS: launchctl setenv makes vars visible to all GUI apps and launchd services
launchctl setenv OPENAI_BASE_URL "$base_url/v1" 2>/dev/null
if [[ -n "$key" ]]; then
launchctl setenv OPENAI_API_KEY "$key" 2>/dev/null
fi
echo ""
echo " System-level (launchctl):"
echo " ✓ OPENAI_BASE_URL set for GUI apps and daemons"
echo " Note: launchctl vars reset on reboot. Add to Login Items or re-run ocp-connect."
else
# Linux: write to environment.d for systemd user services
local env_dir="$HOME/.config/environment.d"
mkdir -p "$env_dir" 2>/dev/null
{
echo "OPENAI_BASE_URL=$base_url/v1"
if [[ -n "$key" ]]; then
echo "OPENAI_API_KEY=$key"
fi
} > "$env_dir/ocp.conf"
echo ""
echo " System-level (systemd):"
echo " ✓ $env_dir/ocp.conf"
echo " Applies to systemd user services after re-login."
fi
echo "" echo ""
# Step 8: Quick smoke test # Step 8: Quick smoke test