fix: align Git Bash shell config handling
This commit is contained in:
+12
-1
@@ -673,7 +673,18 @@ detect_shell_rc() {
|
|||||||
fi
|
fi
|
||||||
;;
|
;;
|
||||||
bash)
|
bash)
|
||||||
if [ -f "$HOME/.bashrc" ]; then
|
# Git Bash on Windows commonly starts as a login shell, so prefer
|
||||||
|
# .bash_profile there when it already exists. On Unix-like shells,
|
||||||
|
# keep the traditional .bashrc-first behavior.
|
||||||
|
if [ -n "$MSYSTEM" ] || [ -n "$MINGW_PREFIX" ]; then
|
||||||
|
if [ -f "$HOME/.bash_profile" ]; then
|
||||||
|
echo "$HOME/.bash_profile"
|
||||||
|
elif [ -f "$HOME/.bashrc" ]; then
|
||||||
|
echo "$HOME/.bashrc"
|
||||||
|
else
|
||||||
|
echo "$HOME/.profile"
|
||||||
|
fi
|
||||||
|
elif [ -f "$HOME/.bashrc" ]; then
|
||||||
echo "$HOME/.bashrc"
|
echo "$HOME/.bashrc"
|
||||||
elif [ -f "$HOME/.bash_profile" ]; then
|
elif [ -f "$HOME/.bash_profile" ]; then
|
||||||
echo "$HOME/.bash_profile"
|
echo "$HOME/.bash_profile"
|
||||||
|
|||||||
@@ -1,13 +1,15 @@
|
|||||||
"""
|
"""
|
||||||
Shell configuration utilities for persisting environment variables.
|
Shell configuration utilities for persisting environment variables.
|
||||||
|
|
||||||
Supports both bash and zsh, detecting the user's default shell.
|
Supports bash and zsh with platform-aware fallbacks for login-shell config
|
||||||
|
files such as ``.bash_profile``, ``.zshenv``, and ``.profile``.
|
||||||
Used primarily for persisting ADEN_API_KEY across sessions.
|
Used primarily for persisting ADEN_API_KEY across sessions.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
import platform
|
||||||
import re
|
import re
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Literal
|
from typing import Literal
|
||||||
@@ -34,9 +36,9 @@ def detect_shell() -> ShellType:
|
|||||||
else:
|
else:
|
||||||
# Try to detect from config file existence
|
# Try to detect from config file existence
|
||||||
home = Path.home()
|
home = Path.home()
|
||||||
if (home / ".zshrc").exists():
|
if (home / ".zshrc").exists() or (home / ".zshenv").exists():
|
||||||
return "zsh"
|
return "zsh"
|
||||||
elif (home / ".bashrc").exists():
|
elif (home / ".bashrc").exists() or (home / ".bash_profile").exists():
|
||||||
return "bash"
|
return "bash"
|
||||||
return "unknown"
|
return "unknown"
|
||||||
|
|
||||||
@@ -55,14 +57,12 @@ def get_shell_config_path(shell_type: ShellType | None = None) -> Path:
|
|||||||
shell_type = detect_shell()
|
shell_type = detect_shell()
|
||||||
|
|
||||||
home = Path.home()
|
home = Path.home()
|
||||||
|
candidates = _get_shell_config_candidates(home, shell_type)
|
||||||
|
|
||||||
if shell_type == "zsh":
|
for candidate in candidates:
|
||||||
return home / ".zshrc"
|
if candidate.exists():
|
||||||
elif shell_type == "bash":
|
return candidate
|
||||||
return home / ".bashrc"
|
return candidates[0]
|
||||||
else:
|
|
||||||
# Default to .bashrc for unknown shells
|
|
||||||
return home / ".bashrc"
|
|
||||||
|
|
||||||
|
|
||||||
def check_env_var_in_shell_config(
|
def check_env_var_in_shell_config(
|
||||||
@@ -79,29 +79,47 @@ def check_env_var_in_shell_config(
|
|||||||
Returns:
|
Returns:
|
||||||
Tuple of (exists, current_value or None)
|
Tuple of (exists, current_value or None)
|
||||||
"""
|
"""
|
||||||
config_path = get_shell_config_path(shell_type)
|
if shell_type is None:
|
||||||
|
shell_type = detect_shell()
|
||||||
|
|
||||||
if not config_path.exists():
|
for config_path in _get_shell_config_candidates(Path.home(), shell_type):
|
||||||
return False, None
|
if not config_path.exists():
|
||||||
|
continue
|
||||||
|
|
||||||
content = config_path.read_text(encoding="utf-8")
|
content = config_path.read_text(encoding="utf-8")
|
||||||
|
|
||||||
# Look for export ENV_VAR=value or export ENV_VAR="value"
|
# Look for export ENV_VAR=value or export ENV_VAR="value"
|
||||||
pattern = rf"^export\s+{re.escape(env_var)}=(.+)$"
|
pattern = rf"^export\s+{re.escape(env_var)}=(.+)$"
|
||||||
match = re.search(pattern, content, re.MULTILINE)
|
match = re.search(pattern, content, re.MULTILINE)
|
||||||
|
|
||||||
if match:
|
if match:
|
||||||
value = match.group(1).strip()
|
value = match.group(1).strip()
|
||||||
# Remove surrounding quotes if present
|
# Remove surrounding quotes if present
|
||||||
if (value.startswith('"') and value.endswith('"')) or (
|
if (value.startswith('"') and value.endswith('"')) or (
|
||||||
value.startswith("'") and value.endswith("'")
|
value.startswith("'") and value.endswith("'")
|
||||||
):
|
):
|
||||||
value = value[1:-1]
|
value = value[1:-1]
|
||||||
return True, value
|
return True, value
|
||||||
|
|
||||||
return False, None
|
return False, None
|
||||||
|
|
||||||
|
|
||||||
|
def _get_shell_config_candidates(home: Path, shell_type: ShellType) -> list[Path]:
|
||||||
|
"""Return candidate config files in lookup order for the detected shell."""
|
||||||
|
if shell_type == "zsh":
|
||||||
|
return [home / ".zshrc", home / ".zshenv"]
|
||||||
|
|
||||||
|
if shell_type == "bash":
|
||||||
|
# Git Bash commonly launches login shells on Windows, so prefer
|
||||||
|
# ``.bash_profile`` there for writes, but keep ``.bashrc`` in the
|
||||||
|
# lookup list so older setups continue to work.
|
||||||
|
if platform.system() == "Windows":
|
||||||
|
return [home / ".bash_profile", home / ".bashrc", home / ".profile"]
|
||||||
|
return [home / ".bashrc", home / ".bash_profile", home / ".profile"]
|
||||||
|
|
||||||
|
return [home / ".profile", home / ".bashrc"]
|
||||||
|
|
||||||
|
|
||||||
def add_env_var_to_shell_config(
|
def add_env_var_to_shell_config(
|
||||||
env_var: str,
|
env_var: str,
|
||||||
value: str,
|
value: str,
|
||||||
|
|||||||
Reference in New Issue
Block a user