mirror of
https://github.com/containers/podman.git
synced 2025-06-23 10:32:22 +08:00
Merge pull request #19261 from cgiradkar/podman_inspect_completion
Podman inspect completion
This commit is contained in:
@ -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
|
||||
|
@ -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")
|
||||
|
@ -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
|
||||
|
@ -25,6 +25,8 @@ history | $IMAGE
|
||||
image history | $IMAGE
|
||||
image inspect | $IMAGE
|
||||
container inspect | mycontainer
|
||||
inspect | mycontainer
|
||||
|
||||
|
||||
volume inspect | -a
|
||||
secret inspect | mysecret
|
||||
|
@ -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
|
||||
|
Reference in New Issue
Block a user