Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add proper support for overridden $ZDOTDIR #92

Merged
merged 2 commits into from
Dec 2, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 26 additions & 18 deletions src/shell/zsh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,6 @@ pub fn spawn_shell(info: &ShellSpawnInfo) -> Result<()> {
write!(
zshrc_buf,
r#"
# If a zsh_history file exists, copy it over before zsh initialization so history is maintained
if [[ -f "${{ZDOTDIR:-HOME}}/.zsh_history" ]] ; then
cp "${{ZDOTDIR:-HOME}}"/.zsh_history $ZDOTDIR
fi

KUBIE_LOGIN_SHELL=0
if [[ "$OSTYPE" == "darwin"* ]] ; then
KUBIE_LOGIN_SHELL=1
fi

# Reference for loading behavior
# https://shreevatsa.wordpress.com/2008/03/30/zshbash-startup-files-loading-order-bashrc-zshrc-etc/

Expand All @@ -35,10 +25,26 @@ elif [[ -f "/etc/zsh/zshenv" ]] ; then
source "/etc/zsh/zshenv"
fi

# TODO: It is possible for users to modify ZDOTDIR in ~/.zshenv to put zsh files in another place.
# TODO: Currently modification of this variable it not supported by kubie.
if [[ -f "$HOME/.zshenv" ]] ; then
tmp_ZDOTDIR=$ZDOTDIR
source "$HOME/.zshenv"
# If the user has overridden $ZDOTDIR, we save that in $_KUBIE_USER_ZDOTDIR for later reference
# and reset $ZDOTDIR
if [[ "$tmp_ZDOTDIR" != "$ZDOTDIR" ]]; then
_KUBIE_USER_ZDOTDIR=$ZDOTDIR
ZDOTDIR=$tmp_ZDOTDIR
unset tmp_ZDOTDIR
fi
fi

# If a zsh_history file exists, copy it over before zsh initialization so history is maintained
if [[ -f "$HISTFILE" ]] ; then
cp $HISTFILE $ZDOTDIR
fi

KUBIE_LOGIN_SHELL=0
if [[ "$OSTYPE" == "darwin"* ]] ; then
KUBIE_LOGIN_SHELL=1
fi

if [[ -f "/etc/zprofile" && "$KUBIE_LOGIN_SHELL" == "1" ]] ; then
Expand All @@ -47,8 +53,8 @@ elif [[ -f "/etc/zsh/zprofile" && "$KUBIE_LOGIN_SHELL" == "1" ]] ; then
source "/etc/zsh/zprofile"
fi

if [[ -f "${{ZDOTDIR:-HOME}}/.zprofile" && "$KUBIE_LOGIN_SHELL" == "1" ]] ; then
source "${{ZDOTDIR:-HOME}}/.zprofile"
if [[ -f "${{_KUBIE_USER_ZDOTDIR:-$HOME}}/.zprofile" && "$KUBIE_LOGIN_SHELL" == "1" ]] ; then
source "${{_KUBIE_USER_ZDOTDIR:-$HOME}}/.zprofile"
fi

if [[ -f "/etc/zshrc" ]] ; then
Expand All @@ -57,8 +63,8 @@ elif [[ -f "/etc/zsh/zshrc" ]] ; then
source "/etc/zsh/zshrc"
fi

if [[ -f "${{ZDOTDIR:-HOME}}/.zshrc" ]] ; then
source "${{ZDOTDIR:-HOME}}/.zshrc"
if [[ -f "${{_KUBIE_USER_ZDOTDIR:-$HOME}}/.zshrc" ]] ; then
source "${{_KUBIE_USER_ZDOTDIR:-$HOME}}/.zshrc"
fi

if [[ -f "/etc/zlogin" && "$KUBIE_LOGIN_SHELL" == "1" ]] ; then
Expand All @@ -67,10 +73,12 @@ elif [[ -f "/etc/zsh/zlogin" && "$KUBIE_LOGIN_SHELL" == "1" ]] ; then
source "/etc/zsh/zlogin"
fi

if [[ -f "${{ZDOTDIR:-HOME}}/.zlogin" && "$KUBIE_LOGIN_SHELL" == "1" ]] ; then
source "${{ZDOTDIR:-HOME}}/.zlogin"
if [[ -f "${{_KUBIE_USER_ZDOTDIR:-$HOME}}/.zlogin" && "$KUBIE_LOGIN_SHELL" == "1" ]] ; then
source "${{_KUBIE_USER_ZDOTDIR:-$HOME}}/.zlogin"
fi

unset _KUBIE_USER_ZDOTDIR

autoload -Uz add-zsh-hook

# This function sets the proper KUBECONFIG variable before a command runs,
Expand Down