Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add zsh completion for kubectx subcommand (-d) #178

Merged
merged 1 commit into from
Oct 24, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions completion/kubectx.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@

local KUBECTX="${HOME}/.kube/kubectx"
PREV=""

local all_contexts="$(kubectl config get-contexts --output='name')"
if [ -f "$KUBECTX" ]; then
# show '-' only if there's a saved previous context
local PREV=$(cat "${KUBECTX}")
_arguments "1: :(-
$(kubectl config get-contexts --output='name'))"

_arguments \
"-d:*: :(${all_contexts})" \
"(- *): :(- ${all_contexts})"
Comment on lines +12 to +13
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks a lot for this PR!
Can you explain this syntax and how it works real quick for me?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, of course.

"-d:*: :(${all_contexts})"

  • -d -> The kubectx subcommand.
  • '*' -> This describes multiple arguments. All contexts matching the pattern (*, all) will be completed.
  • ' ' -> An empty description.
  • (${all_contexts}) -> An action describing all words to be completed.

"(- *): :(- ${all_contexts})"

  • (- *) -> If one of the contexts has already been selected (and is on the command line), it will not complete other options anymore. (i.e this will avoid kubectx ctx1 ctx2)
  • ' ' -> An empty description.
  • (- ${all_contexts}) -> An action describing all words to be completed, including the current one (-).

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does the description have a space character to be “empty”?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK the spec needs this space to know what optspec I'm using (optname:*pattern:message:action and no optname:message:action). I can add a description for them if you want. WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another relevant point is I'm not completing with all the current subcommands like -d, -h and -c. The subcommand -d will only be completed if the user enters the -d option (typing).

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, makes sense. Thanks.
I might find you again when there's an issue with this file. 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok 😄

else
_arguments "1: :($(kubectl config get-contexts --output='name'))"
_arguments \
"-d:*: :(${all_contexts})" \
"(- *): :(${all_contexts})"
fi