hidetatz / kubecolor

colorizes kubectl output
MIT License
1.43k stars 109 forks source link

Kubecolor autocompletion does not work and break canonical k alias #44

Closed bygui86 closed 3 years ago

bygui86 commented 3 years ago

I tried to enable the autocompletion for kubecolor following instructions on the official README:

Basically, configuring autocompletion for kubecolor requires adding following line in your shell config file.

# autocomplete for kubecolor
complete -o default -F __start_kubectl kubecolor

If you are using an alias like k="kubecolor", then just change above like:

complete -o default -F __start_kubectl k

but it does not work, no autocompletion for kubecolor and even worse no autocompletion anymore for k alias!

Here some info:

Here the output of kubectl version command:

$ kubectl version

Client Version: version.Info{Major:"1", Minor:"20", GitVersion:"v1.20.2", GitCommit:"faecb196815e248d3ecfb03c680a4507229c2a56", GitTreeState:"clean", BuildDate:"2021-01-14T05:14:17Z", GoVersion:"go1.15.6", Compiler:"gc", Platform:"darwin/amd64"}

Any help is really appreciated!!

7118path commented 3 years ago

Have a look here, works for my zsh setup: https://github.com/dty1er/kubecolor/issues/32#issuecomment-724411738

bygui86 commented 3 years ago

Thanks @ecsecta ! I will try asap

bygui86 commented 3 years ago

@ecsecta unfortunately the solution you proposed does not work.

source <(kubectl completion zsh)
alias kubectl="kubecolor"
compdef kubecolor="kubectl"

with complete -o default -F __start_kubectl kubectl commented

I forgot to mention:

hidetatz commented 3 years ago

@bygui86 Can you show me the whole .zshrc? On my environment this works:

source <(kubectl completion zsh)
alias kubectl="kubecolor"
compdef kubecolor="kubectl"
bygui86 commented 3 years ago

@dty1er I could but it would be pretty difficult to read... as I work on many different technologies everyday and I use tons of tools, I organised .zshrc in multiple subfiles to keep everything tidy.

Here an example of the .zshrc structure:

source configs.sh

source oh-my-zsh.zsh

source env-vars.sh

source ps1-prompt.sh

source functions.sh

source path.sh

source aliases.sh

source company_configs.sh

source autocompletion.sh

I tried both following ways (commenting everything else related to kubectl and k):

# ...

source <(kubectl completion zsh)
alias kubectl="kubecolor"
compdef kubecolor="kubectl"

# ...
### load all aliases together
# some aliases (e.g. ll, ...)
alias kubectl="kubecolor"
compdef kubecolor="kubectl"
# some other aliases (e.g. gcloud, kubectx, kubens, ...)

### source all autocompletion together
# source some autocompletion (e.g. brew, ...)
source <(kubectl completion zsh)
# source some other autocompletion (e.g. gcloud, helm, ...)

but still it doesn't work.

I will try excluding everything and loading only your 3 lines, let's see...

bygui86 commented 3 years ago

I tried with a super minimal .zshrc:

#!/bin/zsh

source "$HOME/oh-my-zsh.zsh"

source <(kubectl completion zsh)
alias kubectl="kubecolor"
compdef kubecolor="kubectl"

and it worked!

Now I have to guess which of my other scripts creates this issue :(

@dty1er any known conflict between kubecolor and other tools? for example gcloud, krew, kubectx, kubens or similar?

hidetatz commented 3 years ago

@bygui86 I see. Unfortunately, I currently don't know any tools which make the completion not working. I guess you can investigate it by bisecting your .zshrc. If you can find which part was causing this problem, I'd like you to report it here. The information should be much helpful for other people, so I want to consider writing it in this project README.

bygui86 commented 3 years ago

@dty1er I found the conflict:

if type brew &>/dev/null; then
    FPATH=$(brew --prefix)/share/zsh-completions:$FPATH
    autoload -Uz compinit
    compinit
fi

I use these lines to load zsh-completions.

Everything depends on the order...

WORKS

#!/bin/zsh

if type brew &>/dev/null; then
    FPATH=$(brew --prefix)/share/zsh-completions:$FPATH
    autoload -Uz compinit
    compinit
fi

source "$HOME/oh-my-zsh.zsh"

source <(kubectl completion zsh)
alias kubectl="kubecolor"
compdef kubecolor="kubectl"

DOES NOT WORK

#!/bin/zsh

source "$HOME/oh-my-zsh.zsh"

source <(kubectl completion zsh)
alias kubectl="kubecolor"
compdef kubecolor="kubectl"

if type brew &>/dev/null; then
    FPATH=$(brew --prefix)/share/zsh-completions:$FPATH
    autoload -Uz compinit
    compinit
fi

No matter other aliases, functions or autocompletions loaded before or after...

hidetatz commented 3 years ago

@bygui86

Thank you for your effort! I don't understand how zsh completion works deeply, but it seems like compinit should be located before compdef? I could find something similar, but I'm still not sure.

Anyway, I'm happy to hear it worked on your environment. Let me close this issue for now, but please feel free to reopen or open another one when you found something else. Thank you for using kubecolor!

karakanb commented 3 years ago

I am having a similar problem with the oh-my-zsh plugin, has anyone figured out how to make them work, without explicitly sourcing the autocomplete?

Currently, I cannot use the autocomplete or badges the plugin provides.