Merge pull request #1903 from gaelicWizard/unbound

Gracefully handle unbound parameters
pull/1915/head
Noah Gorny 2021-08-31 20:16:26 +03:00 committed by GitHub
commit 5782325dea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 41 additions and 27 deletions

View File

@ -3,7 +3,7 @@
BASH_IT_LOG_PREFIX="core: main: " BASH_IT_LOG_PREFIX="core: main: "
# Only set $BASH_IT if it's not already set # Only set $BASH_IT if it's not already set
if [ -z "$BASH_IT" ]; then if [ -z "${BASH_IT:-}" ]; then
# Setting $BASH to maintain backwards compatibility # Setting $BASH to maintain backwards compatibility
export BASH_IT=$BASH export BASH_IT=$BASH
BASH="$(bash -c 'echo $BASH')" BASH="$(bash -c 'echo $BASH')"
@ -20,12 +20,12 @@ source "${BASH_IT}"/vendor/github.com/erichs/composure/composure.sh
source "${BASH_IT}/lib/log.bash" source "${BASH_IT}/lib/log.bash"
# We can only log it now # We can only log it now
[ -z "$BASH_IT_OLD_BASH_SETUP" ] || _log_warning "BASH_IT variable not initialized, please upgrade your bash-it version and reinstall it!" [ -z "${BASH_IT_OLD_BASH_SETUP:-}" ] || _log_warning "BASH_IT variable not initialized, please upgrade your bash-it version and reinstall it!"
# For backwards compatibility, look in old BASH_THEME location # For backwards compatibility, look in old BASH_THEME location
if [ -z "$BASH_IT_THEME" ]; then if [ -z "${BASH_IT_THEME:-}" ]; then
_log_warning "BASH_IT_THEME variable not initialized, please upgrade your bash-it version and reinstall it!" _log_warning "BASH_IT_THEME variable not initialized, please upgrade your bash-it version and reinstall it!"
export BASH_IT_THEME="$BASH_THEME" export BASH_IT_THEME="${BASH_THEME:-}"
unset BASH_THEME unset BASH_THEME
fi fi
@ -122,7 +122,7 @@ for _bash_it_config_file in $CUSTOM; do
done done
unset _bash_it_config_file unset _bash_it_config_file
if [[ $PROMPT ]]; then if [[ "${PROMPT:-}" ]]; then
export PS1="\[""$PROMPT""\]" export PS1="\[""$PROMPT""\]"
fi fi
@ -144,7 +144,7 @@ if [ -e "$HOME/.jekyllconfig" ]; then
fi fi
# BASH_IT_RELOAD_LEGACY is set. # BASH_IT_RELOAD_LEGACY is set.
if ! command -v reload &> /dev/null && [ -n "$BASH_IT_RELOAD_LEGACY" ]; then if ! command -v reload &> /dev/null && [ -n "${BASH_IT_RELOAD_LEGACY:-}" ]; then
case $OSTYPE in case $OSTYPE in
darwin*) darwin*)
alias reload='source ~/.bash_profile' alias reload='source ~/.bash_profile'

View File

@ -3,6 +3,14 @@
# Loads the system's Bash completion modules. # Loads the system's Bash completion modules.
# If Homebrew is installed (OS X), it's Bash completion modules are loaded. # If Homebrew is installed (OS X), it's Bash completion modules are loaded.
if shopt -qo nounset
then # Bash-completion is too large and complex to expect to handle unbound variables throughout the whole codebase.
BASH_IT_RESTORE_NOUNSET=true
shopt -uo nounset
else
BASH_IT_RESTORE_NOUNSET=false
fi
if [[ -r "${BASH_COMPLETION:-}" ]] ; then if [[ -r "${BASH_COMPLETION:-}" ]] ; then
source "${BASH_COMPLETION}" source "${BASH_COMPLETION}"
elif [[ -r /etc/bash_completion ]] ; then elif [[ -r /etc/bash_completion ]] ; then
@ -22,3 +30,9 @@ then
source "$BASH_IT_HOMEBREW_PREFIX"/etc/profile.d/bash_completion.sh source "$BASH_IT_HOMEBREW_PREFIX"/etc/profile.d/bash_completion.sh
fi fi
fi fi
if $BASH_IT_RESTORE_NOUNSET
then
shopt -so nounset
fi
unset BASH_IT_RESTORE_NOUNSET

View File

@ -156,7 +156,7 @@ bash-it ()
$func $arg $func $arg
done done
if [ -n "$BASH_IT_AUTOMATIC_RELOAD_AFTER_CONFIG_CHANGE" ]; then if [ -n "${BASH_IT_AUTOMATIC_RELOAD_AFTER_CONFIG_CHANGE:-}" ]; then
_bash-it-reload _bash-it-reload
fi fi
else else
@ -354,8 +354,8 @@ _bash-it-migrate() {
disable_func="_disable-$single_type" disable_func="_disable-$single_type"
enable_func="_enable-$single_type" enable_func="_enable-$single_type"
$disable_func $component_name $disable_func "$component_name"
$enable_func $component_name $enable_func "$component_name"
done done
done done
@ -375,7 +375,7 @@ _bash-it-version() {
cd "${BASH_IT}" || return cd "${BASH_IT}" || return
if [ -z $BASH_IT_REMOTE ]; then if [ -z "${BASH_IT_REMOTE:-}" ]; then
BASH_IT_REMOTE="origin" BASH_IT_REMOTE="origin"
fi fi

16
lib/log.bash 100644 → 100755
View File

@ -22,8 +22,8 @@ function _log_general()
param '3: message to log' param '3: message to log'
group 'log' group 'log'
message=$2${BASH_IT_LOG_PREFIX}$3 message=$2${BASH_IT_LOG_PREFIX:-default: }$3
_has_colors && echo -e "$1${message}${echo_normal}" || echo -e "${message}" _has_colors && echo -e "$1${message}${echo_normal:-}" || echo -e "${message}"
} }
function _log_debug() function _log_debug()
@ -33,8 +33,8 @@ function _log_debug()
example '$ _log_debug "Loading plugin git..."' example '$ _log_debug "Loading plugin git..."'
group 'log' group 'log'
[[ "$BASH_IT_LOG_LEVEL" -ge $BASH_IT_LOG_LEVEL_ALL ]] || return 0 [[ "${BASH_IT_LOG_LEVEL:-0}" -ge $BASH_IT_LOG_LEVEL_ALL ]] || return 0
_log_general "${echo_green}" "DEBUG: " "$1" _log_general "${echo_green:-}" "DEBUG: " "$1"
} }
function _log_warning() function _log_warning()
@ -44,8 +44,8 @@ function _log_warning()
example '$ _log_warning "git binary not found, disabling git plugin..."' example '$ _log_warning "git binary not found, disabling git plugin..."'
group 'log' group 'log'
[[ "$BASH_IT_LOG_LEVEL" -ge $BASH_IT_LOG_LEVEL_WARNING ]] || return 0 [[ "${BASH_IT_LOG_LEVEL:-0}" -ge $BASH_IT_LOG_LEVEL_WARNING ]] || return 0
_log_general "${echo_yellow}" " WARN: " "$1" _log_general "${echo_yellow:-}" " WARN: " "$1"
} }
function _log_error() function _log_error()
@ -55,6 +55,6 @@ function _log_error()
example '$ _log_error "Failed to load git plugin..."' example '$ _log_error "Failed to load git plugin..."'
group 'log' group 'log'
[[ "$BASH_IT_LOG_LEVEL" -ge $BASH_IT_LOG_LEVEL_ERROR ]] || return 0 [[ "${BASH_IT_LOG_LEVEL:-0}" -ge $BASH_IT_LOG_LEVEL_ERROR ]] || return 0
_log_general "${echo_red}" "ERROR: " "$1" _log_general "${echo_red:-}" "ERROR: " "$1"
} }

View File

@ -1,4 +1,4 @@
if [[ $BASH_PREVIEW ]]; if [[ "${BASH_PREVIEW:-}" ]];
then then
unset BASH_PREVIEW #Prevent infinite looping unset BASH_PREVIEW #Prevent infinite looping
echo " echo "

2
lib/utilities.bash 100644 → 100755
View File

@ -58,7 +58,7 @@ _bash-it-array-dedup() {
# Outputs a full path of the grep found on the filesystem # Outputs a full path of the grep found on the filesystem
_bash-it-grep() { _bash-it-grep() {
if [[ -z "${BASH_IT_GREP}" ]] ; then if [[ -z "${BASH_IT_GREP:-}" ]] ; then
export BASH_IT_GREP="$(which egrep || which grep || '/usr/bin/grep')" export BASH_IT_GREP="$(which egrep || which grep || '/usr/bin/grep')"
fi fi
printf "%s " "${BASH_IT_GREP}" printf "%s " "${BASH_IT_GREP}"

View File

@ -70,7 +70,7 @@ function passgen ()
# Create alias pass to passgen when pass isn't installed or # Create alias pass to passgen when pass isn't installed or
# BASH_IT_LEGACY_PASS is true. # BASH_IT_LEGACY_PASS is true.
if ! command -v pass &>/dev/null || [[ "$BASH_IT_LEGACY_PASS" = true ]] if ! command -v pass &>/dev/null || [[ "${BASH_IT_LEGACY_PASS:-}" = true ]]
then then
alias pass=passgen alias pass=passgen
fi fi

View File

@ -11,19 +11,19 @@ load ../../lib/log
@test "lib log: basic debug logging with BASH_IT_LOG_LEVEL_ALL" { @test "lib log: basic debug logging with BASH_IT_LOG_LEVEL_ALL" {
BASH_IT_LOG_LEVEL=$BASH_IT_LOG_LEVEL_ALL BASH_IT_LOG_LEVEL=$BASH_IT_LOG_LEVEL_ALL
run _log_debug "test test test" run _log_debug "test test test"
assert_output "DEBUG: test test test" assert_output "DEBUG: default: test test test"
} }
@test "lib log: basic warning logging with BASH_IT_LOG_LEVEL_ALL" { @test "lib log: basic warning logging with BASH_IT_LOG_LEVEL_ALL" {
BASH_IT_LOG_LEVEL=$BASH_IT_LOG_LEVEL_ALL BASH_IT_LOG_LEVEL=$BASH_IT_LOG_LEVEL_ALL
run _log_warning "test test test" run _log_warning "test test test"
assert_output " WARN: test test test" assert_output " WARN: default: test test test"
} }
@test "lib log: basic error logging with BASH_IT_LOG_LEVEL_ALL" { @test "lib log: basic error logging with BASH_IT_LOG_LEVEL_ALL" {
BASH_IT_LOG_LEVEL=$BASH_IT_LOG_LEVEL_ALL BASH_IT_LOG_LEVEL=$BASH_IT_LOG_LEVEL_ALL
run _log_error "test test test" run _log_error "test test test"
assert_output "ERROR: test test test" assert_output "ERROR: default: test test test"
} }
@test "lib log: basic debug logging with BASH_IT_LOG_LEVEL_WARNING" { @test "lib log: basic debug logging with BASH_IT_LOG_LEVEL_WARNING" {
@ -35,13 +35,13 @@ load ../../lib/log
@test "lib log: basic warning logging with BASH_IT_LOG_LEVEL_WARNING" { @test "lib log: basic warning logging with BASH_IT_LOG_LEVEL_WARNING" {
BASH_IT_LOG_LEVEL=$BASH_IT_LOG_LEVEL_WARNING BASH_IT_LOG_LEVEL=$BASH_IT_LOG_LEVEL_WARNING
run _log_warning "test test test" run _log_warning "test test test"
assert_output " WARN: test test test" assert_output " WARN: default: test test test"
} }
@test "lib log: basic error logging with BASH_IT_LOG_LEVEL_WARNING" { @test "lib log: basic error logging with BASH_IT_LOG_LEVEL_WARNING" {
BASH_IT_LOG_LEVEL=$BASH_IT_LOG_LEVEL_WARNING BASH_IT_LOG_LEVEL=$BASH_IT_LOG_LEVEL_WARNING
run _log_error "test test test" run _log_error "test test test"
assert_output "ERROR: test test test" assert_output "ERROR: default: test test test"
} }
@ -60,7 +60,7 @@ load ../../lib/log
@test "lib log: basic error logging with BASH_IT_LOG_LEVEL_ERROR" { @test "lib log: basic error logging with BASH_IT_LOG_LEVEL_ERROR" {
BASH_IT_LOG_LEVEL=$BASH_IT_LOG_LEVEL_ERROR BASH_IT_LOG_LEVEL=$BASH_IT_LOG_LEVEL_ERROR
run _log_error "test test test" run _log_error "test test test"
assert_output "ERROR: test test test" assert_output "ERROR: default: test test test"
} }
@test "lib log: basic debug silent logging" { @test "lib log: basic debug silent logging" {