mirror of
https://github.com/wnlen/clash-for-linux.git
synced 2026-10-10 04:03:04 +08:00
Load shell proxy functions from system-wide Zsh startup files, keep project lookup portable across Bash and Zsh, and cover enable, login restore, disable, and uninstall behavior.\n\nRefs #291\n\nCo-authored-by: Junle Chen <601122452@qq.com>
This commit is contained in:
+16
-3
@@ -38,6 +38,12 @@ _clashctl_real_on_target() {
|
||||
|
||||
_clash_alias_project_dir() {
|
||||
local self_dir
|
||||
|
||||
if [ -n "${CLASH_FOR_LINUX_PROJECT_DIR:-}" ]; then
|
||||
echo "$CLASH_FOR_LINUX_PROJECT_DIR"
|
||||
return 0
|
||||
fi
|
||||
|
||||
self_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
|
||||
echo "$self_dir"
|
||||
}
|
||||
@@ -56,10 +62,17 @@ _clash_alias_yq_bin() {
|
||||
|
||||
_clash_alias_env_value() {
|
||||
local key="$1"
|
||||
local env_file
|
||||
local env_file value
|
||||
|
||||
if [ -n "${!key:-}" ]; then
|
||||
printf '%s' "${!key}"
|
||||
case "$key" in
|
||||
""|*[!A-Za-z0-9_]*)
|
||||
return 0
|
||||
;;
|
||||
esac
|
||||
|
||||
eval "value=\${$key:-}"
|
||||
if [ -n "${value:-}" ]; then
|
||||
printf '%s' "$value"
|
||||
return 0
|
||||
fi
|
||||
|
||||
|
||||
+34
-5
@@ -2633,7 +2633,7 @@ cleanup_legacy_shell_entries() {
|
||||
}
|
||||
|
||||
install_shell_alias_entry() {
|
||||
local profile_file alias_file shell_rc bash_completion_file zsh_completion_file home_dir
|
||||
local profile_file alias_file shell_rc bash_completion_file zsh_completion_file home_dir system_zsh_rc
|
||||
|
||||
cleanup_legacy_shell_entries
|
||||
|
||||
@@ -2650,9 +2650,10 @@ cat > "$profile_file" <<EOF
|
||||
# clash-for-linux shell entry
|
||||
export PATH="$(command_install_dir):\$PATH"
|
||||
|
||||
if [ -n "\${BASH_VERSION:-}" ] \
|
||||
&& [ -z "\${CLASH_FOR_LINUX_SHELL_LOADED:-}" ] \
|
||||
&& [ -r "$alias_file" ]; then
|
||||
if [ -z "\${CLASH_FOR_LINUX_SHELL_LOADED:-}" ] \
|
||||
&& [ -r "$alias_file" ] \
|
||||
&& { [ -n "\${BASH_VERSION:-}" ] || [ -n "\${ZSH_VERSION:-}" ]; }; then
|
||||
export CLASH_FOR_LINUX_PROJECT_DIR="$PROJECT_DIR"
|
||||
CLASH_FOR_LINUX_SHELL_LOADED="1"
|
||||
source "$alias_file"
|
||||
fi
|
||||
@@ -2676,6 +2677,13 @@ EOF
|
||||
install_rc_source_block "$shell_rc" "$profile_file"
|
||||
done
|
||||
|
||||
if [ "$INSTALL_SCOPE" = "system" ]; then
|
||||
system_zsh_rc="$(system_zsh_rc_file 2>/dev/null || true)"
|
||||
if [ -n "${system_zsh_rc:-}" ]; then
|
||||
install_rc_source_block "$system_zsh_rc" "$profile_file"
|
||||
fi
|
||||
fi
|
||||
|
||||
install_alias_command_wrappers
|
||||
}
|
||||
|
||||
@@ -2700,8 +2708,16 @@ remove_clashctl_entry() {
|
||||
}
|
||||
|
||||
remove_shell_alias_entry() {
|
||||
local profile_file
|
||||
local profile_file system_zsh_rc
|
||||
profile_file="$(profile_entry_file)"
|
||||
|
||||
if [ "$INSTALL_SCOPE" = "system" ]; then
|
||||
system_zsh_rc="$(system_zsh_rc_file 2>/dev/null || true)"
|
||||
if [ -n "${system_zsh_rc:-}" ]; then
|
||||
remove_rc_source_block "$system_zsh_rc"
|
||||
fi
|
||||
fi
|
||||
|
||||
rm -f "$profile_file" 2>/dev/null || true
|
||||
|
||||
if [ "$INSTALL_SCOPE" = "user" ]; then
|
||||
@@ -2766,6 +2782,19 @@ profile_entry_file() {
|
||||
fi
|
||||
}
|
||||
|
||||
system_zsh_rc_file() {
|
||||
local candidate
|
||||
|
||||
for candidate in /etc/zsh/zshrc /etc/zshrc; do
|
||||
if [ -f "$candidate" ]; then
|
||||
echo "$candidate"
|
||||
return 0
|
||||
fi
|
||||
done
|
||||
|
||||
return 1
|
||||
}
|
||||
|
||||
install_rc_source_block() {
|
||||
local profile_file="$1"
|
||||
local source_target="$2"
|
||||
|
||||
Reference in New Issue
Block a user