Private
Public Access
1
0

refactor: 简化 shell 检测,仅支持 bash 和 zsh

- 移除 fish shell 支持
- 其他 shell 显示不支持错误并退出
- 简化 write_env_to_shell 函数

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-12-12 14:03:00 +08:00
parent 9fee340a85
commit 27ec4a2d38
3 changed files with 13 additions and 21 deletions

View File

@@ -29,10 +29,10 @@ get_shell_rc() {
else
echo "$HOME/.bash_profile"
fi
elif [ "${SHELL##*/}" = "fish" ]; then
echo "$HOME/.config/fish/config.fish"
else
echo "$HOME/.profile"
print_error "不支持当前 shell: ${SHELL##*/}"
print_error "仅支持 bash 和 zsh"
exit 1
fi
}
@@ -53,18 +53,13 @@ write_env_to_shell() {
fi
# 转义特殊字符(使用单引号包裹更安全)
local export_line
if [ "${SHELL##*/}" = "fish" ]; then
export_line="set -Ux $var_name '$var_value'"
else
export_line="export $var_name='$var_value'"
fi
local export_line="export $var_name='$var_value'"
# 删除旧的同名变量行,添加新行
local tmp_file
tmp_file=$(mktemp)
if [ -s "$rc_file" ]; then
grep -v "^export $var_name=" "$rc_file" | grep -v "^set -Ux $var_name " >"$tmp_file" 2>/dev/null || true
grep -v "^export $var_name=" "$rc_file" >"$tmp_file" 2>/dev/null || true
fi
echo "$export_line" >>"$tmp_file"

View File

@@ -38,10 +38,10 @@ get_shell_rc() {
else
echo "$HOME/.bash_profile"
fi
elif [ "${SHELL##*/}" = "fish" ]; then
echo "$HOME/.config/fish/config.fish"
else
echo "$HOME/.profile"
print_error "不支持当前 shell: ${SHELL##*/}"
print_error "仅支持 bash 和 zsh"
exit 1
fi
}
@@ -62,18 +62,13 @@ write_env_to_shell() {
fi
# 转义特殊字符(使用单引号包裹更安全)
local export_line
if [ "${SHELL##*/}" = "fish" ]; then
export_line="set -Ux $var_name '$var_value'"
else
export_line="export $var_name='$var_value'"
fi
local export_line="export $var_name='$var_value'"
# 删除旧的同名变量行,添加新行
local tmp_file
tmp_file=$(mktemp)
if [ -s "$rc_file" ]; then
grep -v "^export $var_name=" "$rc_file" | grep -v "^set -Ux $var_name " >"$tmp_file" 2>/dev/null || true
grep -v "^export $var_name=" "$rc_file" >"$tmp_file" 2>/dev/null || true
fi
echo "$export_line" >>"$tmp_file"

View File

@@ -33,7 +33,9 @@ get_shell_rc() {
echo "$HOME/.bash_profile"
fi
else
echo "$HOME/.profile"
error "不支持当前 shell: ${SHELL##*/}"
error "仅支持 bash 和 zsh"
exit 1
fi
}