1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Return error code when volume inspect fails with a template.

Following `docker inspect` conventions:

- Keep partial info in a buffer to not print incomplete template outputs.
- Break execution when template parsing or decoding fail.

Signed-off-by: David Calavera <david.calavera@gmail.com>
This commit is contained in:
David Calavera 2015-12-02 15:24:30 -05:00
parent aa2ca5d93c
commit b9d30280f6
2 changed files with 32 additions and 22 deletions

View file

@ -124,6 +124,7 @@ func (cli *DockerCli) CmdVolumeInspect(args ...string) error {
var status = 0 var status = 0
var volumes []*types.Volume var volumes []*types.Volume
for _, name := range cmd.Args() { for _, name := range cmd.Args() {
resp, err := cli.call("GET", "/volumes/"+name, nil, nil) resp, err := cli.call("GET", "/volumes/"+name, nil, nil)
if err != nil { if err != nil {
@ -132,9 +133,9 @@ func (cli *DockerCli) CmdVolumeInspect(args ...string) error {
var volume types.Volume var volume types.Volume
if err := json.NewDecoder(resp.body).Decode(&volume); err != nil { if err := json.NewDecoder(resp.body).Decode(&volume); err != nil {
fmt.Fprintf(cli.err, "%s\n", err) fmt.Fprintf(cli.err, "Unable to read inspect data: %v\n", err)
status = 1 status = 1
continue break
} }
if tmpl == nil { if tmpl == nil {
@ -142,30 +143,29 @@ func (cli *DockerCli) CmdVolumeInspect(args ...string) error {
continue continue
} }
if err := tmpl.Execute(cli.out, &volume); err != nil { buf := bytes.NewBufferString("")
if err := tmpl.Execute(cli.out, &volume); err != nil { if err := tmpl.Execute(buf, &volume); err != nil {
fmt.Fprintf(cli.err, "%s\n", err) fmt.Fprintf(cli.err, "Template parsing error: %v\n", err)
status = 1 status = 1
continue break
} }
cli.out.Write(buf.Bytes())
cli.out.Write([]byte{'\n'})
}
if tmpl == nil {
b, err := json.MarshalIndent(volumes, "", " ")
if err != nil {
return err
}
_, err = io.Copy(cli.out, bytes.NewReader(b))
if err != nil {
return err
} }
io.WriteString(cli.out, "\n") io.WriteString(cli.out, "\n")
} }
if tmpl != nil {
return nil
}
b, err := json.MarshalIndent(volumes, "", " ")
if err != nil {
return err
}
_, err = io.Copy(cli.out, bytes.NewReader(b))
if err != nil {
return err
}
io.WriteString(cli.out, "\n")
if status != 0 { if status != 0 {
return Cli.StatusError{StatusCode: status} return Cli.StatusError{StatusCode: status}
} }

View file

@ -165,3 +165,13 @@ func (s *DockerSuite) TestVolumeCliNoArgs(c *check.C) {
c.Assert(stderr, checker.Contains, usage) c.Assert(stderr, checker.Contains, usage)
c.Assert(stderr, checker.Contains, "flag provided but not defined: --no-such-flag") c.Assert(stderr, checker.Contains, "flag provided but not defined: --no-such-flag")
} }
func (s *DockerSuite) TestVolumeCliInspectTmplError(c *check.C) {
out, _ := dockerCmd(c, "volume", "create")
name := strings.TrimSpace(out)
out, exitCode, err := dockerCmdWithError("volume", "inspect", "--format='{{ .FooBar }}'", name)
c.Assert(err, checker.NotNil, check.Commentf("Output: %s", out))
c.Assert(exitCode, checker.Equals, 1, check.Commentf("Output: %s", out))
c.Assert(out, checker.Contains, "Template parsing error")
}