Skip to content

Commit

Permalink
support multiple inspection
Browse files Browse the repository at this point in the history
Signed-off-by: 程飞 <fay.cheng.cn@gmail.com>
  • Loading branch information
faycheng committed Mar 28, 2018
1 parent 2f15bb9 commit 46a4ff2
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 8 deletions.
4 changes: 2 additions & 2 deletions cli/image_inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
Expand Down
4 changes: 2 additions & 2 deletions cli/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
Expand Down
20 changes: 20 additions & 0 deletions cli/inspect/inspector.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"io"
"strings"
"text/template"

"github.com/alibaba/pouch/pkg/utils/templates"
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions cli/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
Expand Down
4 changes: 2 additions & 2 deletions cli/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
Expand Down
23 changes: 23 additions & 0 deletions test/cli_inspect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.Sprintf("%s\n%s\n", names[0], names[1]))
}

0 comments on commit 46a4ff2

Please sign in to comment.