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

Shell completion #6442

Merged
merged 3 commits into from Nov 13, 2020
Merged

Conversation

Luap99
Copy link
Member

@Luap99 Luap99 commented May 31, 2020

Add shell completion with cobra

Allow automatic generation for shell completion scripts
with the internal cobra functions (requires v1.0.0)

This should replace the handwritten completion scripts
and even adds support for fish and powershell(if needed?)

We can now create the scripts with

  • podman completion bash
  • podman completion zsh
  • podman completion fish
  • podman completion powershell

to test the completion run:
source <(podman completion bash)

I added the main completion for all commands
but this still could be improved with
better custom completion functions.
see: cmd/podman/completion/completion.go

Flag completion is for now only added to
podman generate systemd --restart-policy
for now
just to show how to implement this on flags

This is still WIP

Closes #6440

@openshift-ci-robot openshift-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label May 31, 2020
@openshift-ci-robot
Copy link
Collaborator

Hi @Luap99. Thanks for your PR.

I'm waiting for a containers member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@openshift-ci-robot openshift-ci-robot added the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label May 31, 2020
@Luap99
Copy link
Member Author

Luap99 commented May 31, 2020

/hold

@openshift-ci-robot openshift-ci-robot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label May 31, 2020
@Luap99
Copy link
Member Author

Luap99 commented May 31, 2020

@rhatdan @TomSweeneyRedHat please take a look :)

@Luap99 Luap99 force-pushed the podman-autocomplete branch 2 times, most recently from 2726a23 to bcebd68 Compare May 31, 2020 20:13
@openshift-ci-robot openshift-ci-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label May 31, 2020
@Luap99
Copy link
Member Author

Luap99 commented May 31, 2020

Ok i played with this a bit and tested the bash, zsh and fish completions. Bash and fish are working fine but zsh is not working properly (only completes static flags). There is already a PR in the cobra project which fixes this Issue so this should work once this get merged.

I start adding autocompletion support for the flags

@TomSweeneyRedHat
Copy link
Member

@saschagrunert just putting this on your radar as an FYI as I know you have poked around in this area on other projects.

@rhatdan
Copy link
Member

rhatdan commented Jun 2, 2020

@edsantiago PTAL I know you have played in this area in the past.

@Luap99 Luap99 force-pushed the podman-autocomplete branch 4 times, most recently from a3975e8 to d651b53 Compare June 2, 2020 19:52
@Luap99
Copy link
Member Author

Luap99 commented Jun 2, 2020

I started adding completion for flags.
I created the table below so you can see my progress and how each flag gets completed.

Command Main Args Flags
podman-attach
podman-container-attach
running containers --detach-keys -> ctrl-
podman-auto-update none
podman-build
podman-image-build
default shell skipping for now because flags are defined in buildah
podman-commit
podman-container-commit
all containers --change -> CMD, ENTRYPOINT, ENV, EXPOSE, LABEL, STOPSIGNAL, USER, VOLUME, WORKDIR
--format -> oci,docker
--iidfile -> default shell
--author -> none
--message -> none
podman-container-checkpoint running containers --export -> default shell
podman-container-cleanup exited containers --exec -> none
podman-container-exists all containers
podman-container-prune none --filter -> none
podman-container-restore all containers --import -> default shell
--name -> none
podman-container-runlabel all images --authfile -> default shell
--cert-dir -> default shell
--creds -> none
--name -> none
podman-cp
podman-container-cp
all containers and/or default shell --export -> default shell
podman-create
podman-container-create
only first arg as image --annotation -> none
--attach -> STDIN,STDOUT,STDERR
--authfile -> default shell
--blkio-weight -> 10,100,1000
--blkio-weight-device -> default shell
--cap-add -> all linux capabilities see man capabilities.7
--cap-drop -> all linux capabilities see man capabilities.7
--cgroupns -> host,container:[name],ns:[path],private
--cgroups -> enabled,disabled,no-conmon
--cgroup-parent -> default shell
--cidfile -> default shell
--conmon-pidfile -> default shell
--cpu* -> none
--detach-keys -> ctrl-
--device -> default shell
--device-cgroup-rule -> none
--device-read/write* -> default shell
--entrypoint -> none
--env -> none

I will edit this post to keep the table up to date with my progress.

Copy link
Collaborator

@edsantiago edsantiago left a comment

Choose a reason for hiding this comment

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

Am only partway through review. Meant to send the spelling correction below as a single comment, but github got confused with your push --force.

cmd/podman/completion/bash.go Outdated Show resolved Hide resolved
@edsantiago
Copy link
Collaborator

edsantiago commented Jun 2, 2020

I love the idea of this, and what I've seen of the implementation so far (but it will take me many more hours to complete review). If that zsh PR ever gets merged, then vendored in here, I would love to replace my crufty hand-written zsh completion.

My first showstopper question is: how are you testing this? I fired up a bash shell and:

$ source <(./bin/podman completion bash)
$ podman p<TAB>   --> bash: _get_comp_words_by_ref: command not found

[UPDATED TO ADD: bash-5.0.11-2.fc32]

@Luap99
Copy link
Member Author

Luap99 commented Jun 2, 2020

you have to use ./bin/podman p<TAB> because it implements some custom hidden args on the podman command

@edsantiago
Copy link
Collaborator

I did (try both podman and ./bin/podman); I redacted one just for brevity. No difference.

@Luap99
Copy link
Member Author

Luap99 commented Jun 2, 2020

ok i installed it with sudo make install i think the new podman version needs to be in your $PATH

@edsantiago
Copy link
Collaborator

Solved: you need to run this first:

$ source /usr/share/bash-completion/bash_completion

// - "f29/httpd:latest"
// - "f29/httpd"
// - "httpd:latest"
// - "httpd"
Copy link
Collaborator

Choose a reason for hiding this comment

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

I would strongly encourage you to add image IDs: not all images have names. (You should probably check for that)

Copy link
Member Author

Choose a reason for hiding this comment

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

ok i will include ids would you say i should show them only if 2 more chars are typed like the others?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think all image IDs should be listed, honestly: there are often cases where intermediate images have no tags, and someone running podman rmi <TAB> might want to see the full list of both IDs and tags. But this is just an opinion based on gut feel, and I don't have any evidence to support it. I think it would be OK to do what you think is best, and add a big FIXME comment saying that this needs UX testing. @mheon @rhatdan @TomSweeneyRedHat WDYT?

Copy link
Member

Choose a reason for hiding this comment

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

I'm a little worried that printing all IDs could get overwhelming, it's easy to accumulate a lot of dangling images by using podman build. I would expect us to offer tab completion once a few characters are typed, though.

Copy link
Member Author

@Luap99 Luap99 left a comment

Choose a reason for hiding this comment

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

This is ready for review.

Please see this #6442 (comment) if you want to test it.

cmd/podman/common/completion.go Outdated Show resolved Hide resolved
@Luap99 Luap99 changed the title [WIP] Shell completion Shell completion Nov 1, 2020
@openshift-ci-robot openshift-ci-robot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Nov 1, 2020
@Luap99 Luap99 force-pushed the podman-autocomplete branch 4 times, most recently from 2ee38a8 to e4cc230 Compare November 11, 2020 09:00
@edsantiago
Copy link
Collaborator

I've been testing this out all afternoon, and it's just beautiful work. I'd really like to see this merged before the tree changes and @Luap99 needs to rebase yet again. @containers/podman-maintainers can we make an effort to review and merge this?

@rhatdan
Copy link
Member

rhatdan commented Nov 11, 2020

/lgtm

@openshift-ci-robot openshift-ci-robot added the lgtm Indicates that a PR is ready to be merged. label Nov 11, 2020
@TomSweeneyRedHat
Copy link
Member

Thought I'd commented on this.
FWIW, it LGTM, but I'm not at all a subject matter expert here.

This is only temporary until the cobra following PRs are merged:

- PR#1258 Custom completion handle multiple shorhand flags together
- PR#1249 Fix fish handling of "ShellCompDirectiveNoSpace" and file completion
- PR#1213 Fix zsh completion handling of nospace and file completion
- PR#1146 Bash completion V2 with completion descriptions

Signed-off-by: Paul Holzinger <paul.holzinger@web.de>
Allow automatic generation for shell completion scripts
with the internal cobra functions (requires v1.0.0+).

This should replace the handwritten completion scripts
and even adds support for fish. With this approach it is
less likley that completions and code are out of sync.

We can now create the scripts with
- podman completion bash
- podman completion zsh
- podman completion fish

To test the completion run:
source <(podman completion bash)

The same works for podman-remote and podman --remote and
it will complete your remote containers/images with
the correct endpoints values from --url/--connection.

The completion logic is written in go and provided by the
cobra library. The completion functions lives in
`cmd/podman/completion/completion.go`.

The unit test at cmd/podman/shell_completion_test.go checks
if each command and flag has an autocompletion function set.
This prevents that commands and flags have no shell completion set.

This commit does not replace the current autocompletion scripts.

Closes containers#6440

Signed-off-by: Paul Holzinger <paul.holzinger@web.de>
Add a new make target (completion) to generate the shell
completion scripts. This will generate the scripts for bash,
zsh and fish for both podman and podman-remote with `podman completion`.
The scripts are put into the completions directory and can be
installed system wide with `sudo make install.completions`.

This commit replaces the current handwritten scripts for bash and zsh.

The `validate.completion` target has been adjusted to make sure nobody
edits these scripts directly.

Signed-off-by: Paul Holzinger <paul.holzinger@web.de>
@openshift-ci-robot openshift-ci-robot removed the lgtm Indicates that a PR is ready to be merged. label Nov 12, 2020
@Luap99
Copy link
Member Author

Luap99 commented Nov 12, 2020

Rebased with the new doc format since this would have blocked CI post-merge otherwise.

@edsantiago
Copy link
Collaborator

@Luap99 I don't think it would've blocked, since github would merge on top of master (i.e. rebasing for you) but thanks.

I didn't have a strong urge to repeat my testing this morning, and yesterday was a nice quiet PR day, so I just diff'ed against the previous submission:

$ git cdif pr/6442{-rev05,}

...and this is the only change I see which does not look like one of yesterday's PRs:

diff --git a/cmd/podman/completion/completion.go b/cmd/podman/completion/completion.go
index 71d07082c..ead8d1f05 100644
--- a/cmd/podman/completion/completion.go
+++ b/cmd/podman/completion/completion.go
@@ -87,7 +87,7 @@ func completion(cmd *cobra.Command, args []string) error {
        }

        _, err = io.WriteString(w, fmt.Sprintf(
-               "# This file is generated with %q; see: podman-completion(1)\n", cmd.CommandPath(),
+               "\n# This file is generated with %q; see: podman-completion(1)\n", cmd.CommandPath(),
        ))
        return err
 }

LGTM!

@rhatdan
Copy link
Member

rhatdan commented Nov 13, 2020

/lgtm
/hold cancel

@openshift-ci-robot openshift-ci-robot added lgtm Indicates that a PR is ready to be merged. and removed do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. labels Nov 13, 2020
@openshift-merge-robot openshift-merge-robot merged commit 2993e97 into containers:master Nov 13, 2020
@Luap99 Luap99 deleted the podman-autocomplete branch November 19, 2020 20:33
@github-actions github-actions bot added the locked - please file new issue/PR Assist humans wanting to comment on an old issue or PR with locked comments. label Sep 24, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 24, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. lgtm Indicates that a PR is ready to be merged. locked - please file new issue/PR Assist humans wanting to comment on an old issue or PR with locked comments. ok-to-test
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Shell completion
9 participants