2015-03-25 03:57:23 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
"text/template"
|
|
|
|
|
2015-03-26 19:43:00 +00:00
|
|
|
"github.com/docker/docker/api/types"
|
2015-03-25 03:57:23 +00:00
|
|
|
flag "github.com/docker/docker/pkg/mflag"
|
|
|
|
)
|
|
|
|
|
2015-03-25 17:34:41 +00:00
|
|
|
// CmdInspect displays low-level information on one or more containers or images.
|
|
|
|
//
|
|
|
|
// Usage: docker inspect [OPTIONS] CONTAINER|IMAGE [CONTAINER|IMAGE...]
|
2015-03-26 19:43:00 +00:00
|
|
|
|
2015-03-25 03:57:23 +00:00
|
|
|
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")
|
|
|
|
cmd.Require(flag.Min, 1)
|
|
|
|
|
2015-03-29 01:22:46 +00:00
|
|
|
cmd.ParseFlags(args, true)
|
2015-03-25 03:57:23 +00:00
|
|
|
|
|
|
|
var tmpl *template.Template
|
|
|
|
if *tmplStr != "" {
|
|
|
|
var err error
|
|
|
|
if tmpl, err = template.New("").Funcs(funcMap).Parse(*tmplStr); err != nil {
|
|
|
|
fmt.Fprintf(cli.err, "Template parsing error: %v\n", err)
|
2015-04-15 08:57:52 +00:00
|
|
|
return StatusError{StatusCode: 64,
|
2015-03-25 03:57:23 +00:00
|
|
|
Status: "Template parsing error: " + err.Error()}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
indented := new(bytes.Buffer)
|
|
|
|
indented.WriteByte('[')
|
|
|
|
status := 0
|
2015-03-26 19:43:00 +00:00
|
|
|
isImage := false
|
2015-03-25 03:57:23 +00:00
|
|
|
|
|
|
|
for _, name := range cmd.Args() {
|
2015-01-12 19:56:01 +00:00
|
|
|
obj, _, err := readBody(cli.call("GET", "/containers/"+name+"/json", nil, nil))
|
2015-03-25 03:57:23 +00:00
|
|
|
if err != nil {
|
2015-01-12 19:56:01 +00:00
|
|
|
obj, _, err = readBody(cli.call("GET", "/images/"+name+"/json", nil, nil))
|
2015-03-26 19:43:00 +00:00
|
|
|
isImage = true
|
2015-03-25 03:57:23 +00:00
|
|
|
if err != nil {
|
|
|
|
if strings.Contains(err.Error(), "No such") {
|
|
|
|
fmt.Fprintf(cli.err, "Error: No such image or container: %s\n", name)
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(cli.err, "%s", err)
|
|
|
|
}
|
|
|
|
status = 1
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if tmpl == nil {
|
|
|
|
if err = json.Indent(indented, obj, "", " "); err != nil {
|
|
|
|
fmt.Fprintf(cli.err, "%s\n", err)
|
|
|
|
status = 1
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
} else {
|
2015-04-23 01:28:07 +00:00
|
|
|
dec := json.NewDecoder(bytes.NewReader(obj))
|
2015-03-26 19:43:00 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
}
|
2015-03-25 03:57:23 +00:00
|
|
|
}
|
|
|
|
cli.out.Write([]byte{'\n'})
|
|
|
|
}
|
|
|
|
indented.WriteString(",")
|
|
|
|
}
|
|
|
|
|
|
|
|
if indented.Len() > 1 {
|
|
|
|
// Remove trailing ','
|
|
|
|
indented.Truncate(indented.Len() - 1)
|
|
|
|
}
|
|
|
|
indented.WriteString("]\n")
|
|
|
|
|
|
|
|
if tmpl == nil {
|
|
|
|
if _, err := io.Copy(cli.out, indented); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if status != 0 {
|
2015-04-15 08:57:52 +00:00
|
|
|
return StatusError{StatusCode: status}
|
2015-03-25 03:57:23 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|