当别名包含`--kubeconfig`时如何使用`complete -F _start_kubectl`?

问题描述 投票:0回答:1

我遇到了 kubectl 别名完成的问题,当

kubectl
--kubeconfig
时,它不会通过 Tab 自动完成。

但是当我使用完整的 kubectl 命令(通过 control + option + e 扩展)时,它会起作用。

系统信息:

  • osx
    14.3.1
  • bash-completion@2
    已安装

本地设置:

$ type kk
kk is aliased to 'kubectl --kubeconfig ~/.kube/8/config'

$ complete -p kk
complete -o bashdefault -o default -o nosort -F __start_kubectl kk

$ cat ~/.inputrc
# allow iso-latin1 characters to be inserted
set convert-meta                   on
# don't strip characters to 7 bits when reading
set input-meta                     on
set completion-ignore-case         on
set show-all-if-ambiguous          on
set show-all-if-unmodified         on
set mark-symlinked-directories     on
set print-completions-horizontally on
set enable-bracketed-paste         off
set colored-stats                  on
set skip-completed-text            on

详情(视频中): https://streamable.com/poigy6

macos autocomplete
1个回答
0
投票

@Tblue 获取线索,别名补全工作通过 cykerway/complete-alias

得以完善

步骤:

  1. 下载并来源完整别名
$ curl -o ~/.config/complete_alias -fsSL https://github.com/cykerway/complete-alias/raw/master/complete_alias
$ source ~/.config/complete_alias

# verify
$ type _complete_alias
  1. 将补全函数设置为别名:
$ alias kk='kubectl --kubeconfig ~/.kube/8/config'
$ complete -F _complete_alias kk

或在 rc 文件中:

alias k1='kubecolor --kubeconfig ~/.kube/1/config'
alias k2='kubecolor --kubeconfig ~/.kube/2/config'
alias k3='kubecolor --kubeconfig ~/.kube/2/config'

source ~/.config/complete_alias
complete -o nosort -o bashdefault -o default -F _complete_alias $(alias | sed -rn 's/^alias ([^=]+)=.+kubec.+$/\1/p' | xargs)

结果: enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.