Skip to content

Commit

Permalink
Add proper support for overridden $ZDOTDIR (#92)
Browse files Browse the repository at this point in the history
Add proper support for overridden $ZDOTDIR

This ensures kubie copies .zshlogin and .zshrc from the correct place where ZDOTDIR has been overridden by the user.
  • Loading branch information
c10l authored Dec 2, 2021
1 parent 6b5c7e7 commit ffb6687
Showing 1 changed file with 26 additions and 18 deletions.
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

0 comments on commit ffb6687

Please sign in to comment.