Created
February 5, 2021 10:48
-
-
Save fragolinux/0577c20aadd98fae15cc91ee9ac0c683 to your computer and use it in GitHub Desktop.
direnv enabler script, inspired by "basher" install script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
## direnv enabler script, inspired by "basher" install script | |
die() { | |
echo "!! $1 " >&2 | |
echo "!! -----------------------------" >&2 | |
exit 1 | |
} | |
## stop if direnv is not installed | |
command -v direnv >/dev/null 2>&1 || die "I require direnv but it's not installed. Aborting." | |
## now check what shell is running | |
shell_type=$(basename "$SHELL") | |
echo ". detected shell type: $shell_type" | |
case "$shell_type" in | |
bash) startup_type="simple" ; startup_script="$HOME/.bashrc" ;; | |
zsh) startup_type="simple" ; startup_script="$HOME/.zshrc" ;; | |
sh) startup_type="simple" ; startup_script="$HOME/.profile";; | |
fish) startup_type="fish" ; startup_script="$HOME/.config/fish/config.fish" ;; | |
*) startup_type="?" ; startup_script="" ; ;; | |
esac | |
## startup script should exist already | |
[[ -n "$startup_script" && ! -f "$startup_script" ]] && die "startup script [$startup_script] does not exist" | |
## direnv_keyword will allow us to remove the lines upon uninstall | |
direnv_keyword="direnv5ea843" | |
## now add the basher initialisation lines to the user's startup script | |
echo ". add direnv initialisation to [$startup_script]" | |
if [[ "$startup_type" == "simple" ]]; then | |
( | |
echo "eval \"\$(direnv hook $shell_type)\" ##$direnv_keyword" | |
echo "eval \"\$(direnv stdlib)\" ##$direnv_keyword" | |
) >>"$startup_script" | |
elif [[ "$startup_type" == "fish" ]]; then | |
( | |
echo "status --is-interactive; and . (direnv hook - $shell_type | psub) ##$direnv_keyword" | |
echo "status --is-interactive; and . (direnv stdlib | psub) ##$direnv_keyword" | |
) >>"$startup_script" | |
else | |
die "unknown shell [$shell_type] - can't initialise" | |
fi | |
## script is finished | |
echo "direnv config is installed - open a new terminal window to start using it" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment