kubernetes-Mac中设置kubectl命令行自动补全的方法

kubectl 的子命令和参数比较多,敲起来比较容易出错,查看了kubernetes文档,安装起来比较容易,以下是安装过程。

1. 你的系统如果没安装Homebrew,请先安装(brew官网安装指南):

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

2. 查看自动补全功能的帮助,下面按照帮助操作:

mac-temp:~ zyh$ kubectl completion --help
Output shell completion code for the specified shell (bash or zsh). The shell
code must be evalutated to provide interactive completion of kubectl commands.
This can be done by sourcing it from the .bash _profile. 

Detailed instructions on how to do this are available here:
https://kubernetes.io/docs/tasks/tools/install-kubectl/#enabling-shell-autocompletion

Note for zsh users: [1] zsh completions are only supported in versions of zsh >=
5.2

Examples:
  # Installing bash completion on macOS using homebrew
  ## If running Bash 3.2 included with macOS
  brew install bash-completion
  ## or, if running Bash 4.1+
  brew install bash-completion@2
  ## If kubectl is installed via homebrew, this should start working
immediately.
  ## If you've installed via other means, you may need add the completion to
your completion directory
  kubectl completion bash > $(brew --prefix)/etc/bash_completion.d/kubectl

3. 检查你的bash版本,并安装 bash-completion:

mac-temp:~ zyh$ /bin/bash -version
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin17)
Copyright (C) 2007 Free Software Foundation, Inc.

可以看出系统的bash版本为3.2,所以安装以下版本:

$ brew install bash-completion

4. 把kubectl completion bash 增加到你的completion 目录:

$ kubectl completion bash > $(brew --prefix)/etc/bash_completion.d/kubectl

5. 调用brew info命令,根据提示将指令添加到~/.bash_profile中:

mac-temp:~ zyh$ brew info bash-completion
bash-completion: stable 1.3 (bottled)
Programmable completion for Bash 3.2
https://bash-completion.alioth.debian.org/
Conflicts with:
  bash-completion@2 (because Differing version of same formula)
/usr/local/Cellar/bash-completion/1.3_3 (189 files, 608.2KB) *
  Poured from bottle on 2018-06-17 at 22:37:13
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/bash-completion.rb
==> Caveats
Add the following line to your ~/.bash_profile:
  [ -f /usr/local/etc/bash_completion ] && . /usr/local/etc/bash_completion


Bash completion has been installed to:
  /usr/local/etc/bash_completion.d

即在 ~/.bash_profile 增加:

[ -f /usr/local/etc/bash_completion ] && . /usr/local/etc/bash_completion

6.重启终端,就可以使用,(按tab键,就可以补全子命令或参数)

参考:

https://kubernetes.io/docs/tasks/tools/install-kubectl/#enabling-shell-autocompletion

Mac中设置docker命令行自动补全的方法,参考: https://docs.docker.com/compose/completion/

猜你喜欢

转载自blog.csdn.net/kozazyh/article/details/80721695