diff --git a/cli/image_inspect.go b/cli/image_inspect.go index c3977b5955..344adfb1b1 100644 --- a/cli/image_inspect.go +++ b/cli/image_inspect.go @@ -25,9 +25,9 @@ func (i *ImageInspectCommand) Init(c *Cli) { Use: "inspect [OPTIONS] IMAGE", Short: "Display detailed information on one image", Long: imageInspectDescription, - Args: cobra.ExactArgs(1), + Args: cobra.MinimumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - return i.runInspect(args) + return inspect.MultiInspect(args, i.runInspect) }, Example: i.example(), } diff --git a/cli/inspect.go b/cli/inspect.go index 56b9d0d678..3a1f0061eb 100644 --- a/cli/inspect.go +++ b/cli/inspect.go @@ -25,9 +25,9 @@ func (p *InspectCommand) Init(c *Cli) { Use: "inspect [OPTIONS] CONTAINER", Short: "Get the detailed information of container", Long: inspectDescription, - Args: cobra.ExactArgs(1), + Args: cobra.MinimumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - return p.runInspect(args) + return inspect.MultiInspect(args, p.runInspect) }, Example: inspectExample(), } diff --git a/cli/inspect/inspector.go b/cli/inspect/inspector.go index 05159c6fed..7535f1f027 100644 --- a/cli/inspect/inspector.go +++ b/cli/inspect/inspector.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/json" "io" + "strings" "text/template" "github.com/alibaba/pouch/pkg/utils/templates" @@ -75,6 +76,25 @@ func Inspect(out io.Writer, ref string, tmplStr string, getRef GetRefFunc) error return nil } +// RunInspectFunc is a function which used by MultiInspect +type RunInspectFunc func(args []string) error + +// MultiInspect Apply function to every item of ref. +func MultiInspect(refs []string, inspect RunInspectFunc) error { + var inspectErrors []string + for _, ref := range refs { + err := inspect([]string{ref}) + if err != nil { + inspectErrors = append(inspectErrors, err.Error()) + } + } + if len(inspectErrors) == 0 { + return nil + } + + return errors.New(strings.Join(inspectErrors, "")) +} + // Inspect executes the inspect template. func (i *TemplateInspector) Inspect(typedElement interface{}) error { buf := new(bytes.Buffer) diff --git a/cli/network.go b/cli/network.go index eb545bfb5c..e7ea0981f0 100644 --- a/cli/network.go +++ b/cli/network.go @@ -265,9 +265,9 @@ func (n *NetworkInspectCommand) Init(c *Cli) { Use: "inspect [OPTIONS] NAME", Short: "Inspect a pouch network", Long: networkInspectDescription, - Args: cobra.ExactArgs(1), + Args: cobra.MinimumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - return n.runNetworkInspect(args) + return inspect.MultiInspect(args, n.runNetworkInspect) }, Example: networkInspectExample(), } diff --git a/cli/volume.go b/cli/volume.go index d194109ea5..cf0bfbb511 100644 --- a/cli/volume.go +++ b/cli/volume.go @@ -226,9 +226,9 @@ func (v *VolumeInspectCommand) Init(c *Cli) { Use: "inspect [OPTIONS] NAME", Short: "Inspect a pouch volume", Long: volumeInspectDescription, - Args: cobra.ExactArgs(1), + Args: cobra.MinimumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - return v.runVolumeInspect(args) + return inspect.MultiInspect(args, v.runVolumeInspect) }, Example: volumeInspectExample(), } diff --git a/test/cli_inspect_test.go b/test/cli_inspect_test.go index 877544a931..8492aaefcc 100644 --- a/test/cli_inspect_test.go +++ b/test/cli_inspect_test.go @@ -74,3 +74,26 @@ func (suite *PouchInspectSuite) TestInspectWrongFormat(c *check.C) { DelContainerForceMultyTime(c, name) } + +// TestMultiInspect is to verify inspect command with multiple args. +func (suite *PouchInspectSuite) TestMultiInspect(c *check.C) { + names := []string{ + "inspect-multi-inspect-1", + "inspect-multi-inspect-2", + } + setUp := func() { + for _, name := range names { + command.PouchRun("create", "-m", "30M", "--name", name, busyboxImage).Assert(c, icmd.Success) + } + } + cleanUp := func() { + for _, name := range names { + DelContainerForceMultyTime(c, name) + } + } + setUp() + defer cleanUp() + + output := command.PouchRun("inspect", "-f", "{{.Name}}", names[0], names[1]).Stdout() + c.Assert(output, check.Equals, fmt.Sprint("%s\n%s\n", names[0], names[1])) +}