-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Update bash-completion for podman inspect #19181
Update bash-completion for podman inspect #19181
Conversation
Signed-off-by: Chetan Giradkar <[email protected]>
The command: ```podman inspect <ConatinerID> -f "{{."``` was not giving any output in console for bash-completion but the same was working fine for the command: ```podman container inspect <ConatinerID> -f "{{."``` So added a flow to handle the former command by defaulting to type CONTAINER and restrict the flow ofbash-completion if other types (eg. IMAGE) is encountered. Signed-off-by: Chetan Giradkar <[email protected]>
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: cgiradkar The full list of commands accepted by this bot can be found here.
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
…ontainer_inspection_completion
cmd/podman/common/completion.go
Outdated
switch reflect.TypeOf(o).String() { | ||
case "*define.InspectContainerData": |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this can work, this type depends on what you pass in AutocompleteFormat()
. If we want to support more than container than you need to have more than one type and you cannot know that until this function is called.
You need to basically invert the order first check getContainers() if the arg is a container then you can set
o = &define.InspectContainerData{}
same for the other types and so on.
Because AutocompleteFormat() is used by a lot of different callers we need to have a check to only do that for the podman inspect command. One way to do that without adding a new parameter is to check for
cmd.Name()
== inspect
and cmd.Parent() == cmd.Root()
. Only then we want to check the arg and overwrite the o
value.
cmd/podman/common/completion.go
Outdated
// add suggestions for: podman inspect (it will default to container inspect) | ||
switch reflect.TypeOf(o).String() { | ||
case "*define.InspectContainerData": | ||
if containers, _ := getContainers(cmd, args[0], completeDefault); len(containers) == 0 { // there might be a panic here when args length is 0. Is it assured that args will not be empty? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you have to make a len(args) > 0 check before, if args == 0 we can assume the container type.
```podman inspect <ConatinerID> -f "{{."``` was not giving any output in console for bash-completion but the same was working fine for the command: ```podman container inspect <ConatinerID> -f "{{."``` So added a flow to handle the former command and additionally handled all types of entities namely container, image, volume, pod and network. This would ESSENTIALLY make the sub-commands such as follows OBSOLETE: podman container inspect podman image inspect podman volume inspect podman pod inspect podman network inspect The following command now enabled with tab-completion: ```podman inspect {CONTAINER | IMAGE | VOLUME | POD | NETWORK} -f "{{." <tab>``` Signed-off-by: Chetan Giradkar <[email protected]>
cmd/podman/common/completion.go
Outdated
// special(expensive) flow for "podman inspect" | ||
if cmd != nil && cmd.Name() == "inspect" && cmd.Parent() == cmd.Root() { | ||
if len(args) == 0 { | ||
return nil, cobra.ShellCompDirectiveNoFileComp |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if no args we should default to &define.InspectContainerData{}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Luap99 the arg here is the name/Id of the container, image, etc. Without it, we cannot proceed to display corresponding suggestions. Is the explanation good or am I missing something?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should assume container, consider typing podman inspect --format {{.
then we do not yet know what the arg is but I think in most cases it is a container so defaulting to that makes sense to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, the use case is somewhat vague but it can be integrated, thanks!
cmd/podman/common/completion.go
Outdated
found := false | ||
// container logic | ||
if containers, _ := getContainers(cmd, args[0], completeDefault); len(containers) > 0 { | ||
o = &define.InspectContainerData{} | ||
found = true | ||
} | ||
|
||
// image logic | ||
if images, _ := getImages(cmd, args[0]); len(images) > 0 && !found { | ||
o = &inspect.ImageData{} | ||
found = true | ||
} | ||
|
||
// volume logic | ||
if volumes, _ := getVolumes(cmd, args[0]); len(volumes) > 0 && !found { | ||
o = &define.InspectVolumeData{} | ||
found = true | ||
} | ||
|
||
// pod logic | ||
if pods, _ := getPods(cmd, args[0], completeDefault); len(pods) > 0 && !found { | ||
o = &entities.PodInspectReport{} | ||
found = true | ||
} | ||
|
||
// network logic | ||
if networks, _ := getNetworks(cmd, args[0], completeDefault); len(networks) > 0 && !found { | ||
o = &types.Network{} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if you split this out to a new function then you do not need the extra found
var and can just return early which would be much more readable
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will implement this logic to another function
As for testing you can add tests here: podman/test/system/600-completion.bats Lines 310 to 311 in e6dbb42
Basically you can call |
Signed-off-by: Chetan Giradkar <[email protected]>
89e11b8
to
4dabbb8
Compare
closing as git history is polluted due to multiple rebases. Opened a new PR here: #19261 |
The command:
podman inspect <ContainerID> -f "{{."
was not giving any output in console for bash-completion but the same was working fine for the command:
podman container inspect <ContainerID> -f "{{."
So added a flow to handle the former command and additionally handled all types of entities namely container, image, volume, pod and network.
This would ESSENTIALLY make the sub-commands such as follows OBSOLETE:
podman container inspect
podman image inspect
podman volume inspect
podman pod inspect
podman network inspect
Does this PR introduce a user-facing change?
Closes #18672
Signed-off-by: Chetan Giradkar [email protected]