diff --git a/cmd/podman/common/completion.go b/cmd/podman/common/completion.go index 45bf00a4ba..819f1807d8 100644 --- a/cmd/podman/common/completion.go +++ b/cmd/podman/common/completion.go @@ -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" @@ -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 @@ -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 diff --git a/cmd/podman/inspect/inspect.go b/cmd/podman/inspect/inspect.go index 575080163d..4866ac0e81 100644 --- a/cmd/podman/inspect/inspect.go +++ b/cmd/podman/inspect/inspect.go @@ -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" @@ -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") diff --git a/test/system/600-completion.bats b/test/system/600-completion.bats index cf0380485c..1060cde46f 100644 --- a/test/system/600-completion.bats +++ b/test/system/600-completion.bats @@ -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 diff --git a/test/system/610-format.bats b/test/system/610-format.bats index 3ee95bdff3..1862301552 100644 --- a/test/system/610-format.bats +++ b/test/system/610-format.bats @@ -25,6 +25,8 @@ history | $IMAGE image history | $IMAGE image inspect | $IMAGE container inspect | mycontainer +inspect | mycontainer + volume inspect | -a secret inspect | mysecret diff --git a/test/system/README.md b/test/system/README.md index 76626b6dd2..c4d73d2df5 100644 --- a/test/system/README.md +++ b/test/system/README.md @@ -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