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

Podman inspect completion #19261

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions cmd/podman/common/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/containers/podman/v4/libpod/define"
"github.com/containers/podman/v4/libpod/events"
"github.com/containers/podman/v4/pkg/domain/entities"
"github.com/containers/podman/v4/pkg/inspect"
"github.com/containers/podman/v4/pkg/signal"
systemdDefine "github.com/containers/podman/v4/pkg/systemd/define"
"github.com/containers/podman/v4/pkg/util"
Expand Down Expand Up @@ -1178,6 +1179,16 @@ func AutocompleteFormat(o interface{}) func(cmd *cobra.Command, args []string, t
if strings.HasPrefix("json", toComplete) {
return []string{"json"}, cobra.ShellCompDirectiveNoFileComp
}

// special(expensive) flow for "podman inspect"
if cmd != nil && cmd.Name() == "inspect" && cmd.Parent() == cmd.Root() {
if len(args) == 0 {
o = &define.InspectContainerData{}
} else {
o = getEntityType(cmd, args, o)
}
}

// no input struct we cannot provide completion return nothing
if o == nil {
return nil, cobra.ShellCompDirectiveNoFileComp
Expand Down Expand Up @@ -1248,6 +1259,30 @@ func AutocompleteFormat(o interface{}) func(cmd *cobra.Command, args []string, t
}
}

func getEntityType(cmd *cobra.Command, args []string, o interface{}) interface{} {
// container logic
if containers, _ := getContainers(cmd, args[0], completeDefault); len(containers) > 0 {
return &define.InspectContainerData{}
}
// image logic
if images, _ := getImages(cmd, args[0]); len(images) > 0 {
return &inspect.ImageData{}
}
// volume logic
if volumes, _ := getVolumes(cmd, args[0]); len(volumes) > 0 {
return &define.InspectVolumeData{}
}
// pod logic
if pods, _ := getPods(cmd, args[0], completeDefault); len(pods) > 0 {
return &entities.PodInspectReport{}
}
// network logic
if networks, _ := getNetworks(cmd, args[0], completeDefault); len(networks) > 0 {
return &types.Network{}
}
return o
}

// actualReflectValue takes the value,
// if it is pointer it will dereference it and when it is nil,
// it will create a new value from it
Expand Down
3 changes: 1 addition & 2 deletions cmd/podman/inspect/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"regexp"
"strings"

"github.com/containers/common/pkg/completion"
"github.com/containers/common/pkg/report"
"github.com/containers/podman/v4/cmd/podman/common"
"github.com/containers/podman/v4/cmd/podman/registry"
Expand All @@ -28,7 +27,7 @@ func AddInspectFlagSet(cmd *cobra.Command) *entities.InspectOptions {

formatFlagName := "format"
flags.StringVarP(&opts.Format, formatFlagName, "f", "json", "Format the output to a Go template or json")
_ = cmd.RegisterFlagCompletionFunc(formatFlagName, completion.AutocompleteNone)
_ = cmd.RegisterFlagCompletionFunc(formatFlagName, common.AutocompleteFormat(nil)) // passing nil as the type selection logic is in AutocompleteFormat function

typeFlagName := "type"
flags.StringVarP(&opts.Type, typeFlagName, "t", common.AllType, "Specify inspect-object type")
Expand Down
19 changes: 19 additions & 0 deletions test/system/600-completion.bats
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,25 @@ function _check_no_suggestions() {
# recurse for any subcommands.
check_shell_completion

# check inspect with format flag
run_completion inspect -f "{{."
assert "$output" =~ ".*^\{\{\.Args\}\}\$.*" "Defaulting to container type is completed"

run_completion inspect created-$random_container_name -f "{{."
assert "$output" =~ ".*^\{\{\.Args\}\}\$.*" "Container type is completed"

run_completion inspect $random_image_name -f "{{."
assert "$output" =~ ".*^\{\{\.Digest\}\}\$.*" "Image type is completed"

run_completion inspect $random_volume_name -f "{{."
assert "$output" =~ ".*^\{\{\.Anonymous\}\}\$.*" "Volume type is completed"

run_completion inspect created-$random_pod_name -f "{{."
assert "$output" =~ ".*^\{\{\.BlkioDeviceReadBps\}\}\$.*" "Pod type is completed"

run_completion inspect $random_network_name -f "{{."
assert "$output" =~ ".*^\{\{\.DNSEnabled\}\}\$.*" "Network type is completed"

# cleanup
run_podman secret rm $random_secret_name
rm -f $secret_file
Expand Down
2 changes: 2 additions & 0 deletions test/system/610-format.bats
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ history | $IMAGE
image history | $IMAGE
image inspect | $IMAGE
container inspect | mycontainer
inspect | mycontainer
volume inspect | -a
secret inspect | mysecret
Expand Down
10 changes: 9 additions & 1 deletion test/system/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,15 @@ the function or perhaps just a substring.
Requirements
============

The `jq` tool is needed for parsing JSON output.
- bats
- jq
- skopeo
- nmap-ncat
- httpd-tools
- openssl
- socat
- buildah
- gnupg


Further Details
Expand Down