Make kubectl bash completion namespace aware and add noun aliases

- add namespace filtering to bash completion
- add --namespace flag bash completion
- add bash completion noun aliases
- adapt to new cobra package structure
This commit is contained in:
Dr. Stefan Schimanski
2016-03-20 19:14:25 +01:00
parent 8af3be87e4
commit 7e0bb885f1
8 changed files with 99 additions and 17 deletions

View File

@@ -30,12 +30,47 @@ import (
const (
bash_completion_func = `# call kubectl get $1,
__kubectl_namespace_flag()
{
local ret two_word_ns
ret=""
two_word_ns=false
for w in "${words[@]}"; do
if [ "$two_word_ns" = true ]; then
ret="--namespace=${w}"
two_word_ns=false
continue
fi
case "${w}" in
--namespace=*)
ret=${w}
;;
--namespace)
two_word_ns=true
;;
--all-namespaces)
ret=${w}
;;
esac
done
echo $ret
}
__kubectl_get_namespaces()
{
local template kubectl_out
template="{{ range .items }}{{ .metadata.name }} {{ end }}"
if kubectl_out=$(kubectl get -o template --template="${template}" namespace 2>/dev/null); then
COMPREPLY=( $( compgen -W "${kubectl_out[*]}" -- "$cur" ) )
fi
}
__kubectl_parse_get()
{
local template
template="{{ range .items }}{{ .metadata.name }} {{ end }}"
local kubectl_out
if kubectl_out=$(kubectl get -o template --template="${template}" "$1" 2>/dev/null); then
if kubectl_out=$(kubectl get $(__kubectl_namespace_flag) -o template --template="${template}" "$1" 2>/dev/null); then
COMPREPLY=( $( compgen -W "${kubectl_out[*]}" -- "$cur" ) )
fi
}
@@ -71,7 +106,7 @@ __kubectl_get_containers()
fi
local last=${nouns[${len} -1]}
local kubectl_out
if kubectl_out=$(kubectl get -o template --template="${template}" pods "${last}" 2>/dev/null); then
if kubectl_out=$(kubectl get $(__kubectl_namespace_flag) -o template --template="${template}" pods "${last}" 2>/dev/null); then
COMPREPLY=( $( compgen -W "${kubectl_out[*]}" -- "$cur" ) )
fi
}
@@ -196,6 +231,14 @@ Find more information at https://github.com/kubernetes/kubernetes.`,
cmds.AddCommand(NewCmdExplain(f, out))
cmds.AddCommand(NewCmdConvert(f, out))
if cmds.Flag("namespace").Annotations == nil {
cmds.Flag("namespace").Annotations = map[string][]string{}
}
cmds.Flag("namespace").Annotations[cobra.BashCompCustom] = append(
cmds.Flag("namespace").Annotations[cobra.BashCompCustom],
"__kubectl_get_namespaces",
)
return cmds
}