Skip to content
Myr

fix: unbound variables in bash completion (#1321)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

when `set -o nounset` in Bash, the warnings of unbound variables will break the bash completion. use `kubectl` as example: ```sh $ set -o nounset $ my-cli <Tab>-bash: BASH_COMP_DEBUG_FILE: unbound variable $ ``` the warning break bash completion without any completion result, and cause my cursor move to the newline. Use `${variable:-}` substitution in Bash, that assign an empty string as default for unbound variables to fix the warnings.

authored by Eden Tsai · committed by GitHub · parent d65ba12 · Browse files · 1 file+8 added−8 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+8 added−8 removed

  • other:+8 added−8 removed

1 file+8 added−8 removed

modified:bash_completions.go+8 added-8 removed
22 unmodified lines
23 WriteStringAndCheck(buf, fmt.Sprintf("# bash completion for %-36s -*- shell-script -*-\n", name))24 WriteStringAndCheck(buf, fmt.Sprintf(`25__%[1]s_debug()26{27 if [[ -n ${BASH_COMP_DEBUG_FILE} ]]; then27 if [[ -n ${BASH_COMP_DEBUG_FILE:-} ]]; then28 echo "$*" >> "${BASH_COMP_DEBUG_FILE}"29 fi30}31
154 unmodified lines
186 if [[ ${index} -ge 0 ]]; then187 PREFIX=""188 cur="${cur#*=}"189 ${flags_completion[${index}]}190 if [ -n "${ZSH_VERSION}" ]; then190 if [ -n "${ZSH_VERSION:-}" ]; then191 # zsh completion needs --flag= prefix192 eval "COMPREPLY=( \"\${COMPREPLY[@]/#/${flag}=}\" )"193 fi194 fi
82 unmodified lines
277 __%[1]s_debug "${FUNCNAME[0]}: c is $c words[c] is ${words[c]}"278279 # if a command required a flag, and we found it, unset must_have_one_flag()280 local flagname=${words[c]}281 local flagvalue281 local flagvalue=""282 # if the word contained an =283 if [[ ${words[c]} == *"="* ]]; then284 flagvalue=${flagname#*=} # take in as flagvalue after the =285 flagname=${flagname%%=*} # strip everything after the =
10 unmodified lines
296 fi297298 # keep flag value with flagname as flaghash299 # flaghash variable is an associative array which is only supported in bash > 3.300 if [[ -z "${BASH_VERSION}" || "${BASH_VERSINFO[0]}" -gt 3 ]]; then300 if [[ -z "${BASH_VERSION:-}" || "${BASH_VERSINFO[0]:-}" -gt 3 ]]; then301 if [ -n "${flagvalue}" ] ; then302 flaghash[${flagname}]=${flagvalue}303 elif [ -n "${words[ $((c+1)) ]}" ] ; then304 flaghash[${flagname}]=${words[ $((c+1)) ]}
63 unmodified lines
368 elif [[ $c -eq 0 ]]; then369 __%[1]s_handle_command370 elif __%[1]s_contains_word "${words[c]}" "${command_aliases[@]}"; then371 # aliashash variable is an associative array which is only supported in bash > 3.372 if [[ -z "${BASH_VERSION}" || "${BASH_VERSINFO[0]}" -gt 3 ]]; then372 if [[ -z "${BASH_VERSION:-}" || "${BASH_VERSINFO[0]:-}" -gt 3 ]]; then373 words[c]=${aliashash[${words[c]}]}374 __%[1]s_handle_command375 else376 __%[1]s_handle_noun
32 unmodified lines
409 local commands=("%[1]s")410 local command_aliases=()411 local must_have_one_flag=()412 local must_have_one_noun=()413 local has_completion_function414 local last_command413 local has_completion_function=""414 local last_command=""415 local nouns=()416 local noun_aliases=()417418 __%[1]s_handle_word
201 unmodified lines
620 }621622 sort.Strings(cmd.Aliases)623624 WriteStringAndCheck(buf, fmt.Sprint(` if [[ -z "${BASH_VERSION}" || "${BASH_VERSINFO[0]}" -gt 3 ]]; then`, "\n"))624 WriteStringAndCheck(buf, fmt.Sprint(` if [[ -z "${BASH_VERSION:-}" || "${BASH_VERSINFO[0]:-}" -gt 3 ]]; then`, "\n"))625 for _, value := range cmd.Aliases {626 WriteStringAndCheck(buf, fmt.Sprintf(" command_aliases+=(%q)\n", value))627 WriteStringAndCheck(buf, fmt.Sprintf(" aliashash[%q]=%q\n", value, cmd.Name()))628 }
69 unmodified lines