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 5791df4
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 13 deletions.
6 changes: 3 additions & 3 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),
RunE: func(cmd *cobra.Command, args []string) error {
return i.runInspect(args)
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
inspect.MultiInspect(args, i.runInspect)
},
Example: i.example(),
}
Expand Down
6 changes: 3 additions & 3 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),
RunE: func(cmd *cobra.Command, args []string) error {
return p.runInspect(args)
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
inspect.MultiInspect(args, p.runInspect)
},
Example: inspectExample(),
}
Expand Down
14 changes: 14 additions & 0 deletions cli/inspect/inspector.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/alibaba/pouch/pkg/utils/templates"

"fmt"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -75,6 +76,19 @@ 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) {
for _, ref := range refs {
err := inspect([]string{ref})
if err != nil {
fmt.Printf("Error: %s\n", err.Error())
}
}
}

// Inspect executes the inspect template.
func (i *TemplateInspector) Inspect(typedElement interface{}) error {
buf := new(bytes.Buffer)
Expand Down
6 changes: 3 additions & 3 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),
RunE: func(cmd *cobra.Command, args []string) error {
return n.runNetworkInspect(args)
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
inspect.MultiInspect(args, n.runNetworkInspect)
},
Example: networkInspectExample(),
}
Expand Down
6 changes: 3 additions & 3 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),
RunE: func(cmd *cobra.Command, args []string) error {
return v.runVolumeInspect(args)
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
inspect.MultiInspect(args, v.runVolumeInspect)
},
Example: volumeInspectExample(),
}
Expand Down
25 changes: 24 additions & 1 deletion test/cli_inspect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (suite *PouchInspectSuite) TestInspectWrongFormat(c *check.C) {
command.PouchRun("create", "-m", "30M", "--name", name, busyboxImage).Assert(c, icmd.Success)

res := command.PouchRun("inspect", "-f", "{{.NotExists}}", name)
c.Assert(res.Error, check.NotNil)
c.Assert(res.Error, check.IsNil)

expectString := "Template parsing error"
if out := res.Combined(); !strings.Contains(out, expectString) {
Expand All @@ -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-1",
}
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]))
}

0 comments on commit 5791df4

Please sign in to comment.