bashrc: Added an autosource function that allows us to source other files similar to how you would import python modules
This commit is contained in:
parent
ea57c0fc60
commit
9867ab3cac
5 changed files with 66 additions and 35 deletions
28
.bash_functions/bool_eval.sh
Normal file
28
.bash_functions/bool_eval.sh
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
# 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
|
||||||
|
}
|
||||||
|
|
43
.bashrc
43
.bashrc
|
@ -21,43 +21,18 @@ if (( BASH_VERSINFO[0] < 4 )); then
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Basic safe boolean evaluation. See this Gist for details:
|
# Determine if the rest of the configuration should be loaded. DOTFILES_ACTIVE
|
||||||
# https://gist.github.com/gliech/184dc7566821442202f21dfe15e2b7ff
|
# is a environment variable that can be set in ~/.environment.d/dotfiles.env
|
||||||
function truthy {
|
if [[ "${DOTFILES_ACTIVE,,}" == @(y|yes|on|true|1) ]]; then
|
||||||
if [[ "${1,,}" == @(y|yes|on|true|1) ]]; then
|
|
||||||
return 0
|
|
||||||
fi
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
|
|
||||||
function falsy {
|
# Load a function that allows us to import other function definitions from
|
||||||
if [[ "${1,,}" == @(n|no|off|false|0) ]]; then
|
# the BASH_FUNC_PATH without providing their exact location and export it
|
||||||
return 0
|
. "$DOTFILES_DIR/bash_functions/autosource.sh"
|
||||||
fi
|
export -f autosource
|
||||||
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
|
|
||||||
|
|
||||||
# Now that we have the bare essentials, determine if the rest of the configura-
|
|
||||||
# tion should be loaded. DOTFILES_ACTIVE is a environment variable that can be
|
|
||||||
# set in ~/.environment.d/dotfiles.env
|
|
||||||
if truthy $DOTFILES_ACTIVE; then
|
|
||||||
|
|
||||||
# Load utilty functions that may be used by other configurations
|
# Load utilty functions that may be used by other configurations
|
||||||
. "$DOTFILES_DIR/bash_functions.sh"
|
autosource bool_eval
|
||||||
|
autosource dot_utility
|
||||||
|
|
||||||
# Load the rest of the bash configs in ~/.basrc.d
|
# Load the rest of the bash configs in ~/.basrc.d
|
||||||
for file in ~/.bashrc.d/*.sh; do
|
for file in ~/.bashrc.d/*.sh; do
|
||||||
|
|
28
.dotfiles/bash_functions/autosource.sh
Normal file
28
.dotfiles/bash_functions/autosource.sh
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
function autosource {
|
||||||
|
# Test if 1 argument was given
|
||||||
|
if (( $# == 0 )); then
|
||||||
|
echo "autosource: basename of a file in BASH_FUNC_PATH required"
|
||||||
|
echo "autosource: usage: autosource basename [arguments]"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Split the BASH_FUNC_PATH into an array of directory names
|
||||||
|
IFS=: read -ra bash_function_dirs <<< "${BASH_FUNC_PATH}"
|
||||||
|
|
||||||
|
# Look for a .sh file with the basename given as first argument in each direc-
|
||||||
|
# tory in each directory in turn
|
||||||
|
for bash_function_dir in "${bash_function_dirs[@]}"; do
|
||||||
|
if [[ -r "${bash_function_dir}/${1}.sh" ]]; then
|
||||||
|
|
||||||
|
# If we find a matching file, source it and skip the rest of this function
|
||||||
|
. "${bash_function_dir}/${1}.sh" ${@:2}
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
# If the loop completes no matching file is in BASH_FUNC_PATH
|
||||||
|
echo "autosource: no ${1} in (${BASH_FUNC_PATH})"
|
||||||
|
return 1
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
DOTFILES_ACTIVE=true
|
DOTFILES_ACTIVE=true
|
||||||
DOTFILES_DIR="${HOME}/.dotfiles"
|
DOTFILES_DIR="${HOME}/.dotfiles"
|
||||||
DOTFILES_GITDIR="${HOME}/.dotgit"
|
DOTFILES_GITDIR="${HOME}/.dotgit"
|
||||||
|
BASH_FUNC_PATH="${HOME}/.bash_functions:${DOTFILES_DIR}/bash_functions"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue