Allow go template to work properly with inspect

Closes #11641

Signed-off-by: Srini Brahmaroutu <srbrahma@us.ibm.com>
This commit is contained in:
Srini Brahmaroutu
2015-04-23 18:25:18 +00:00
committed by Srini Brahmaroutu
parent eae272f90e
commit 231d362db7
8 changed files with 120 additions and 32 deletions
+26 -13
View File
@@ -8,12 +8,14 @@ import (
"strings"
"text/template"
"github.com/docker/docker/api/types"
flag "github.com/docker/docker/pkg/mflag"
)
// CmdInspect displays low-level information on one or more containers or images.
//
// Usage: docker inspect [OPTIONS] CONTAINER|IMAGE [CONTAINER|IMAGE...]
func (cli *DockerCli) CmdInspect(args ...string) error {
cmd := cli.Subcmd("inspect", "CONTAINER|IMAGE [CONTAINER|IMAGE...]", "Return low-level information on a container or image", true)
tmplStr := cmd.String([]string{"f", "#format", "-format"}, "", "Format the output using the given go template")
@@ -34,11 +36,13 @@ func (cli *DockerCli) CmdInspect(args ...string) error {
indented := new(bytes.Buffer)
indented.WriteByte('[')
status := 0
isImage := false
for _, name := range cmd.Args() {
obj, _, err := readBody(cli.call("GET", "/containers/"+name+"/json", nil, nil))
if err != nil {
obj, _, err = readBody(cli.call("GET", "/images/"+name+"/json", nil, nil))
isImage = true
if err != nil {
if strings.Contains(err.Error(), "No such") {
fmt.Fprintf(cli.err, "Error: No such image or container: %s\n", name)
@@ -57,20 +61,29 @@ func (cli *DockerCli) CmdInspect(args ...string) error {
continue
}
} else {
var value interface{}
// Do not use `json.Unmarshal()` because unmarshal JSON into
// an interface value, Unmarshal stores JSON numbers in
// float64, which is different from `json.Indent()` does.
dec := json.NewDecoder(bytes.NewReader(obj))
dec.UseNumber()
if err := dec.Decode(&value); err != nil {
fmt.Fprintf(cli.err, "%s\n", err)
status = 1
continue
}
if err := tmpl.Execute(cli.out, value); err != nil {
return err
if isImage {
inspPtr := types.ImageInspect{}
if err := dec.Decode(&inspPtr); err != nil {
fmt.Fprintf(cli.err, "%s\n", err)
status = 1
continue
}
if err := tmpl.Execute(cli.out, inspPtr); err != nil {
return err
}
} else {
inspPtr := types.ContainerJSON{}
if err := dec.Decode(&inspPtr); err != nil {
fmt.Fprintf(cli.err, "%s\n", err)
status = 1
continue
}
if err := tmpl.Execute(cli.out, inspPtr); err != nil {
return err
}
}
cli.out.Write([]byte{'\n'})
}