Hi, everyone! I am writing some wrapper functions over existing commands, e.g. kubectldc() { DC="$1" shift KUBECONFIG="$HOME/.kube/$DC.kubeconfig" kubectl "$@" } Kubectl is a well-behaved command, which does have rich completion available. Is it possible to somehow reuse its completion for kubectldc command? As-is the function definition becomes a tradeoff between "using kubectl directly for completion but having to select config manually" and "using the wrapper for automatic config but no completion".
Hi Anton,
Le dim. 31 oct. 2021 à 00:08, Anton Suslov <anton_suslov@me.com> a écrit :
> Is it possible to somehow reuse [kubectl's] completion for kubectldc command
Try the following:
compdef kubectldc=kubectl
Best regards,
--
Jérémie
Hi, Jérémie,
kubectdc command as defined here has an extra first argument. kubectl doesn't expect
it, and because of that the completion gets extremely confused if I do compdef like this.
What I want is to be able to provide my completion for the first argument, and re-use the kubectl
completion for the rest of the args.
> On 31 Oct 2021, at 02:00, Jérémie Roquet <jroquet@arkanosis.net> wrote:
>
> Hi Anton,
>
>> Le dim. 31 oct. 2021 à 00:08, Anton Suslov <anton_suslov@me.com> a écrit :
>> Is it possible to somehow reuse [kubectl's] completion for kubectldc command
>
> Try the following:
>
> compdef kubectldc=kubectl
>
> Best regards,
>
> --
> Jérémie
On 10/31/21, Anton Suslov <anton_suslov@me.com> wrote:
> Hi, everyone!
>
> I am writing some wrapper functions over existing commands, e.g.
>
> kubectldc() {
> DC="$1"
> shift
> KUBECONFIG="$HOME/.kube/$DC.kubeconfig" kubectl "$@"
> }
>
> Kubectl is a well-behaved command, which does have rich completion
> available. Is it possible to somehow reuse its completion for kubectldc
> command? As-is the function definition becomes a tradeoff between "using
> kubectl directly for completion but having to select config manually" and
> "using the wrapper for automatic config but no completion".
compdef -e 'shift words; (( CURRENT-- )); service=kubectl;
words[1]=kubectl; _normal' kubectldc
There is a side effect that if you try to complete the first word, it
will try to complete command names. You can handle that with some
extra code if you want, but at that point you should probably create a
_kubectldc completer as it will become a bit unwieldy for a one-liner.
--
Mikael Magnusson
On Sun, Oct 31, 2021 at 6:23 AM Mikael Magnusson <mikachu@gmail.com> wrote:
>
> compdef -e 'shift words; (( CURRENT-- )); service=kubectl;
> words[1]=kubectl; _normal' kubectldc
>
> There is a side effect that if you try to complete the first word, it
> will try to complete command names. You can handle that with some
> extra code if you want, but at that point you should probably create a
> _kubectldc completer as it will become a bit unwieldy for a one-liner.
It's not too bad, assuming the first argument just needs ordinary
default completions.
compdef -e '((CURRENT>2)) && {
words[1,2]=(kubectl); (( CURRENT-- )); service=kubectl;
} || words[1]=:; _normal' kubectldc
If there is already a completion function for the DC argument, you can
replace "words[1]=:" with an assignment to service to select that
completion.
Oh, thanks, that was exactly what I was looking for!
I have considered doing something similar, but I am still a bit afraid of the completer being sufficiently smart and observing some other things and breaking.
By the way, how do I define completion for a function?
I tried creating a _kubectldc file in a directory in my fpath, but the completion doesn't
work, and it doesn't seem like it even gets loaded automatically. When I do compdef
like you suggested, everything works, but I want to do some more complex logic than it
is nice to fit in the compdef argument. I tried doing the RTFM, but it suggests that
proper filename and #compdef kubectldc should be enough.
> On 31 Oct 2021, at 22:32, Bart Schaefer <schaefer@brasslantern.com> wrote:
>
> On Sun, Oct 31, 2021 at 6:23 AM Mikael Magnusson <mikachu@gmail.com> wrote:
>>
>> compdef -e 'shift words; (( CURRENT-- )); service=kubectl;
>> words[1]=kubectl; _normal' kubectldc
>>
>> There is a side effect that if you try to complete the first word, it
>> will try to complete command names. You can handle that with some
>> extra code if you want, but at that point you should probably create a
>> _kubectldc completer as it will become a bit unwieldy for a one-liner.
>
> It's not too bad, assuming the first argument just needs ordinary
> default completions.
>
> compdef -e '((CURRENT>2)) && {
> words[1,2]=(kubectl); (( CURRENT-- )); service=kubectl;
> } || words[1]=:; _normal' kubectldc
>
> If there is already a completion function for the DC argument, you can
> replace "words[1]=:" with an assignment to service to select that
> completion.
Please disregard my last e-mail, I did not do compinit.
> On 1 Nov 2021, at 09:13, Anton Suslov <anton_suslov@me.com> wrote:
>
> Oh, thanks, that was exactly what I was looking for!
>
> I have considered doing something similar, but I am still a bit afraid of the completer being sufficiently smart and observing some other things and breaking.
>
> By the way, how do I define completion for a function?
> I tried creating a _kubectldc file in a directory in my fpath, but the completion doesn't
> work, and it doesn't seem like it even gets loaded automatically. When I do compdef
> like you suggested, everything works, but I want to do some more complex logic than it
> is nice to fit in the compdef argument. I tried doing the RTFM, but it suggests that
> proper filename and #compdef kubectldc should be enough.
>
>>> On 31 Oct 2021, at 22:32, Bart Schaefer <schaefer@brasslantern.com> wrote:
>>>
>>> On Sun, Oct 31, 2021 at 6:23 AM Mikael Magnusson <mikachu@gmail.com> wrote:
>>>
>>> compdef -e 'shift words; (( CURRENT-- )); service=kubectl;
>>> words[1]=kubectl; _normal' kubectldc
>>>
>>> There is a side effect that if you try to complete the first word, it
>>> will try to complete command names. You can handle that with some
>>> extra code if you want, but at that point you should probably create a
>>> _kubectldc completer as it will become a bit unwieldy for a one-liner.
>>
>> It's not too bad, assuming the first argument just needs ordinary
>> default completions.
>>
>> compdef -e '((CURRENT>2)) && {
>> words[1,2]=(kubectl); (( CURRENT-- )); service=kubectl;
>> } || words[1]=:; _normal' kubectldc
>>
>> If there is already a completion function for the DC argument, you can
>> replace "words[1]=:" with an assignment to service to select that
>> completion.