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

kubectl autocompletion #17

Closed
rumstead opened this issue Apr 18, 2022 · 4 comments
Closed

kubectl autocompletion #17

rumstead opened this issue Apr 18, 2022 · 4 comments

Comments

@rumstead
Copy link
Owner

kubectl safe should use the same autocompletion as kubectl

@rumstead rumstead changed the title Determine if we can reuse kubectl completion kubectl autocompletion Apr 18, 2022
@rumstead
Copy link
Owner Author

rumstead commented Apr 28, 2022

For ZSH you can do the following

alias ks="kubectl safe"
functions[_kubectl-og]=$functions[_kubectl]

_kubectl() {
    words[$words[(i)safe]]=()
    _kubectl-og "$words[*]"
}

# this can be improved and narrowed down 
zstyle ':completion:*' completer _complete _ignored _files

https://unix.stackexchange.com/questions/477142/how-to-extend-existing-zsh-completion-functions

@rumstead
Copy link
Owner Author

@rumstead
Copy link
Owner Author

You can now add the below anywhere on your path. Ensure it is executable.

#!/usr/bin/env bash

# If we are completing a flag, use Cobra's builtin completion system.
# To know if we are completing a flag we need the last argument starts with a `-` and does not contain an `=`
args=("$@")
lastArg=${args[((${#args[@]}-1))]}
if [[ "$lastArg" == -* ]]; then
   if [[ "$lastArg" != *=* ]]; then
      kubectl safe __complete "$@"
   fi
else
   kubectl __complete "$@"
fi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant