mirror of https://github.com/k3s-io/k3s
![]() Automatic merge from submit-queue Fix a bug that --flag=val causes completion error in zsh **What this PR does / why we need it**: This PR fixes a bug that flag of syntax like --flag=val causes completion error in zsh. ``` kubectl --namespace=foo g__handle_flag:25: bad math expression: operand expected at end of string ``` This problem is due to [dynamic scope](https://en.wikipedia.org/wiki/Scope_(computer_science)#Dynamic_scoping) of shell variables. If a variable is declared as local to a function, that scope remains until the function returns. In kubectl completion zsh, `declare -A flaghash` in __start_kubectl() is replaced with `__kubectl_declare -A flaghash` by __kubectl_convert_bash_to_zsh(). As a result of it, flaghash is declared in __kubectl_declare(), and it can not access to flaghash declared in __kubectl_declare() from __handle_flag(). Therefore an error occurs in __handle_flag(). The following is the minimum reproduction code. ```sh #!/usr/bin/env zsh set -e __kubectl_declare() { builtin declare "$@" } __handle_flag() { local flagname="--namespace=" local flagval="kube-system" flaghash[${flagname}]=${flagval} echo "flaghash[${flagname}]=${flaghash[${flagname}]}" } __handle_word() { __handle_flag } __start_kubectl() { __kubectl_declare -A flaghash __handle_word } __start_kubectl # # $ zsh reproduction.zsh # __handle_flag:4: bad math expression: operand expected at end of string # # __start_kubectl { # # __kubectl_declare { # # builtin declare -A flaghash # # } # # __handle_word { # # __handle_flag { # # # It is unable to access flaghash declared in __kubectl_declare from here # flaghash[${flagname}]=${flagval} # # } # # } # } ``` The following is the fixed code. ```sh #!/usr/bin/env zsh set -e __handle_flag() { local flagname="--namespace=" local flagval="kube-system" flaghash[${flagname}]=${flagval} echo "flaghash[${flagname}]=${flaghash[${flagname}]}" } __handle_word() { __handle_flag } __start_kubectl() { builtin declare -A flaghash __handle_word } __start_kubectl # # $ zsh fixed.zsh # flaghash[--namespace=]=kube-system # # __start_kubectl { # # builtin declare -A flaghash # # __handle_word { # # __handle_flag { # # # It is able to access flaghash declared in __start_kubectl from here :) # flaghash[${flagname}]=${flagval} # # } # # } # } ``` https://gist.github.com/superbrothers/0ede4292f6d973f93e54368e227a4902 **Which issue this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close that issue when PR gets merged)*: fixes kubernetes/kubectl#42 **Special notes for your reviewer**: @mengqiy **Release note**: ```release-note NONE ``` |
||
---|---|---|
.. | ||
api | ||
apimachinery/tests | ||
apis | ||
auth | ||
bootstrap/api | ||
capabilities | ||
client | ||
cloudprovider | ||
controller | ||
conversion | ||
credentialprovider | ||
features | ||
fieldpath | ||
fields | ||
generated | ||
hyperkube | ||
kubeapiserver | ||
kubectl | ||
kubelet | ||
kubemark | ||
labels | ||
master | ||
printers | ||
probe | ||
proxy | ||
quota | ||
registry | ||
routes | ||
runtime | ||
security | ||
securitycontext | ||
serviceaccount | ||
ssh | ||
types | ||
util | ||
version | ||
volume | ||
watch | ||
BUILD | ||
OWNERS |