From 6b7f34f7f863b940d263e693cd4ba8668453e029 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gregor=20B=C3=BCckendorf?= Date: Thu, 13 Dec 2018 17:15:32 +0100 Subject: [PATCH] bashrc: checking if the environment is loaded before executing stuff that relies on it --- .bashrc | 48 +++++++++++++++++++++++++++++++++---- .environment.d/dotfiles.env | 4 ++++ 2 files changed, 47 insertions(+), 5 deletions(-) create mode 100644 .environment.d/dotfiles.env diff --git a/.bashrc b/.bashrc index 6d2f65a..13b4b51 100644 --- a/.bashrc +++ b/.bashrc @@ -9,11 +9,49 @@ if (( BASH_VERSINFO[0] < 4 )); then echo "Please get a newer version of Bash." echo "Your bashrc won't work otherwise." else - # Source .bashrc.d - for file in ~/.bashrc.d/*.bashrc; do - . "$file" - done - unset -v file + # Basic safe boolean evaluation. See this Gist for details: + # https://gist.github.com/gliech/184dc7566821442202f21dfe15e2b7ff + function truthy { + if [[ "${1,,}" == @(y|yes|on|true|1) ]]; then + return 0 + fi + return 1 + } + + function falsy { + if [[ "${1,,}" == @(n|no|off|false|0) ]]; then + return 0 + fi + return 1 + } + + function true_false_default { + case "${1,,}" in + y | yes | on | true | 1) + return 0 ;; + n | no | off | false | 0) + return 1 ;; + esac + shift + eval "$@" + return 1 + } + + export -f truthy falsy true_false_default + + if truthy $DOTFILES_ACTIVE; then + + # Source .bashrc.d + for file in ~/.bashrc.d/*.bashrc; do + . "$file" + done + + unset -v file + + elif [[ ! -v DOTFILES_ACTIVE ]]; then + echo "Dotfiles environment not found." + echo "You should probably reload your login session before proceeding." + fi fi diff --git a/.environment.d/dotfiles.env b/.environment.d/dotfiles.env new file mode 100644 index 0000000..10de78c --- /dev/null +++ b/.environment.d/dotfiles.env @@ -0,0 +1,4 @@ +DOTFILES_ACTIVE=true +DOTFILES_DIR="${HOME}/.dotfiles" +DOTFILES_GITDIR="${HOME}/.dotgit" +