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

Completion and sudo #2179

Open
apostasie opened this issue Aug 18, 2024 · 4 comments
Open

Completion and sudo #2179

apostasie opened this issue Aug 18, 2024 · 4 comments

Comments

@apostasie
Copy link

Hi,

For nerdctl, we are interested in displaying different completion if called with sudo vs. called directly. (containerd/nerdctl#99 ).

Do you have a recommendation on how to implement this / approach this problem?

I assume we would have to patch:
https://github.com/spf13/cobra/blob/v1.8.1/bash_completionsV2.go#L84
to call with sudo if needed.

But then, how do we decide that? Use _complete_as_root to test?

Thanks a lot.

@marckhouzam
Copy link
Collaborator

First thing to note is that when shell completion is performed it is the completion script for the sudo command that gets called. And apparently that script knows to call the completion of the relevant command. I’d be curious to know what that completion does to eventually invoke the nerctl completion script. If we’re lucky maybe it can help figure out if the invocation is from sudo.

@marckhouzam
Copy link
Collaborator

it is the completion script for the sudo command that gets called

This is an assumption. Maybe the shell does something special for sudo. Either way I would suggest looking into the shell completion for sudo as a first step.

@apostasie
Copy link
Author

Yeah, I looked into it.

On Ubuntu 24.04 at least, this is here:

/usr/share/bash-completion/bash_completion (and /usr/share/bash-completion/completions/sudo )

Which is why I was thinking about using _complete_as_root to check.

@apostasie
Copy link
Author

What I mean is that the following works (as a POC):

    if _complete_as_root; then
      out=$(eval "sudo ${requestComp}" 2>/dev/null)
    else
      out=$(eval "${requestComp}" 2>/dev/null)

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

2 participants