2015-03-25 03:57:23 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2015-09-25 12:49:02 +00:00
|
|
|
"net/url"
|
2015-03-25 03:57:23 +00:00
|
|
|
"strings"
|
|
|
|
"text/template"
|
|
|
|
|
2015-03-26 19:43:00 +00:00
|
|
|
"github.com/docker/docker/api/types"
|
2015-05-05 04:18:28 +00:00
|
|
|
Cli "github.com/docker/docker/cli"
|
2015-03-25 03:57:23 +00:00
|
|
|
flag "github.com/docker/docker/pkg/mflag"
|
|
|
|
)
|
|
|
|
|
2015-05-05 04:18:28 +00:00
|
|
|
var funcMap = template.FuncMap{
|
|
|
|
"json": func(v interface{}) string {
|
|
|
|
a, _ := json.Marshal(v)
|
|
|
|
return string(a)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
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-25 03:57:23 +00:00
|
|
|
func (cli *DockerCli) CmdInspect(args ...string) error {
|
2015-10-08 12:46:21 +00:00
|
|
|
cmd := Cli.Subcmd("inspect", []string{"CONTAINER|IMAGE [CONTAINER|IMAGE...]"}, Cli.DockerCommands["inspect"].Description, true)
|
2015-11-09 14:37:24 +00:00
|
|
|
tmplStr := cmd.String([]string{"f", "-format"}, "", "Format the output using the given go template")
|
2015-06-26 14:47:31 +00:00
|
|
|
inspectType := cmd.String([]string{"-type"}, "", "Return JSON for specified type, (e.g image or container)")
|
2015-09-25 12:49:02 +00:00
|
|
|
size := cmd.Bool([]string{"s", "-size"}, false, "Display total file sizes if the type is container")
|
2015-03-25 03:57:23 +00:00
|
|
|
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
|
2015-06-26 14:47:31 +00:00
|
|
|
var err error
|
|
|
|
var obj []byte
|
|
|
|
|
2015-03-25 03:57:23 +00:00
|
|
|
if *tmplStr != "" {
|
|
|
|
if tmpl, err = template.New("").Funcs(funcMap).Parse(*tmplStr); err != nil {
|
2015-05-05 04:18:28 +00:00
|
|
|
return Cli.StatusError{StatusCode: 64,
|
2015-03-25 03:57:23 +00:00
|
|
|
Status: "Template parsing error: " + err.Error()}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-26 14:47:31 +00:00
|
|
|
if *inspectType != "" && *inspectType != "container" && *inspectType != "image" {
|
|
|
|
return fmt.Errorf("%q is not a valid value for --type", *inspectType)
|
|
|
|
}
|
|
|
|
|
2015-03-25 03:57:23 +00:00
|
|
|
indented := new(bytes.Buffer)
|
2015-04-28 10:51:04 +00:00
|
|
|
indented.WriteString("[\n")
|
2015-03-25 03:57:23 +00:00
|
|
|
status := 0
|
2015-03-26 19:43:00 +00:00
|
|
|
isImage := false
|
2015-03-25 03:57:23 +00:00
|
|
|
|
2015-09-25 12:49:02 +00:00
|
|
|
v := url.Values{}
|
|
|
|
if *size {
|
|
|
|
v.Set("size", "1")
|
|
|
|
}
|
|
|
|
|
2015-03-25 03:57:23 +00:00
|
|
|
for _, name := range cmd.Args() {
|
2015-06-26 14:47:31 +00:00
|
|
|
if *inspectType == "" || *inspectType == "container" {
|
2015-09-25 12:49:02 +00:00
|
|
|
obj, _, err = readBody(cli.call("GET", "/containers/"+name+"/json?"+v.Encode(), nil, nil))
|
2015-11-05 02:17:05 +00:00
|
|
|
if err != nil {
|
|
|
|
if err == errConnectionFailed {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if *inspectType == "container" {
|
|
|
|
if strings.Contains(err.Error(), "No such") {
|
|
|
|
fmt.Fprintf(cli.err, "Error: No such container: %s\n", name)
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(cli.err, "%s", err)
|
|
|
|
}
|
|
|
|
status = 1
|
|
|
|
continue
|
2015-06-26 14:47:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if obj == nil && (*inspectType == "" || *inspectType == "image") {
|
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 {
|
2015-11-05 02:17:05 +00:00
|
|
|
if err == errConnectionFailed {
|
|
|
|
return err
|
|
|
|
}
|
2015-03-25 03:57:23 +00:00
|
|
|
if strings.Contains(err.Error(), "No such") {
|
2015-06-26 14:47:31 +00:00
|
|
|
if *inspectType == "" {
|
|
|
|
fmt.Fprintf(cli.err, "Error: No such image or container: %s\n", name)
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(cli.err, "Error: No such image: %s\n", name)
|
|
|
|
}
|
2015-03-25 03:57:23 +00:00
|
|
|
} else {
|
|
|
|
fmt.Fprintf(cli.err, "%s", err)
|
|
|
|
}
|
|
|
|
status = 1
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if tmpl == nil {
|
2015-06-26 14:47:31 +00:00
|
|
|
if err := json.Indent(indented, obj, "", " "); err != nil {
|
2015-03-25 03:57:23 +00:00
|
|
|
fmt.Fprintf(cli.err, "%s\n", err)
|
|
|
|
status = 1
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
} else {
|
2015-05-12 00:39:21 +00:00
|
|
|
rdr := bytes.NewReader(obj)
|
|
|
|
dec := json.NewDecoder(rdr)
|
2015-10-22 22:42:19 +00:00
|
|
|
buf := bytes.NewBufferString("")
|
2015-03-26 19:43:00 +00:00
|
|
|
|
|
|
|
if isImage {
|
|
|
|
inspPtr := types.ImageInspect{}
|
|
|
|
if err := dec.Decode(&inspPtr); err != nil {
|
2015-10-22 22:42:19 +00:00
|
|
|
fmt.Fprintf(cli.err, "Unable to read inspect data: %v\n", err)
|
2015-03-26 19:43:00 +00:00
|
|
|
status = 1
|
2015-10-22 22:42:19 +00:00
|
|
|
break
|
2015-03-26 19:43:00 +00:00
|
|
|
}
|
2015-10-22 22:42:19 +00:00
|
|
|
if err := tmpl.Execute(buf, inspPtr); err != nil {
|
2015-05-12 00:39:21 +00:00
|
|
|
rdr.Seek(0, 0)
|
2015-10-22 22:42:19 +00:00
|
|
|
var ok bool
|
|
|
|
|
|
|
|
if buf, ok = cli.decodeRawInspect(tmpl, dec); !ok {
|
|
|
|
fmt.Fprintf(cli.err, "Template parsing error: %v\n", err)
|
|
|
|
status = 1
|
|
|
|
break
|
2015-05-12 00:39:21 +00:00
|
|
|
}
|
2015-03-26 19:43:00 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
inspPtr := types.ContainerJSON{}
|
|
|
|
if err := dec.Decode(&inspPtr); err != nil {
|
2015-10-22 22:42:19 +00:00
|
|
|
fmt.Fprintf(cli.err, "Unable to read inspect data: %v\n", err)
|
2015-03-26 19:43:00 +00:00
|
|
|
status = 1
|
2015-10-22 22:42:19 +00:00
|
|
|
break
|
2015-03-26 19:43:00 +00:00
|
|
|
}
|
2015-10-22 22:42:19 +00:00
|
|
|
if err := tmpl.Execute(buf, inspPtr); err != nil {
|
2015-05-12 00:39:21 +00:00
|
|
|
rdr.Seek(0, 0)
|
2015-10-22 22:42:19 +00:00
|
|
|
var ok bool
|
|
|
|
|
|
|
|
if buf, ok = cli.decodeRawInspect(tmpl, dec); !ok {
|
|
|
|
fmt.Fprintf(cli.err, "Template parsing error: %v\n", err)
|
|
|
|
status = 1
|
|
|
|
break
|
2015-05-12 00:39:21 +00:00
|
|
|
}
|
2015-03-26 19:43:00 +00:00
|
|
|
}
|
2015-03-25 03:57:23 +00:00
|
|
|
}
|
2015-10-22 22:42:19 +00:00
|
|
|
|
|
|
|
cli.out.Write(buf.Bytes())
|
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 {
|
2015-06-03 09:35:57 +00:00
|
|
|
// Note that we will always write "[]" when "-f" isn't specified,
|
|
|
|
// to make sure the output would always be array, see
|
|
|
|
// https://github.com/docker/docker/pull/9500#issuecomment-65846734
|
2015-03-25 03:57:23 +00:00
|
|
|
if _, err := io.Copy(cli.out, indented); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if status != 0 {
|
2015-05-05 04:18:28 +00:00
|
|
|
return Cli.StatusError{StatusCode: status}
|
2015-03-25 03:57:23 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2015-10-22 22:42:19 +00:00
|
|
|
|
|
|
|
// decodeRawInspect executes the inspect template with a raw interface.
|
|
|
|
// This allows docker cli to parse inspect structs injected with Swarm fields.
|
|
|
|
// Unfortunately, go 1.4 doesn't fail executing invalid templates when the input is an interface.
|
|
|
|
// It doesn't allow to modify this behavior either, sending <no value> messages to the output.
|
|
|
|
// We assume that the template is invalid when there is a <no value>, if the template was valid
|
|
|
|
// we'd get <nil> or "" values. In that case we fail with the original error raised executing the
|
|
|
|
// template with the typed input.
|
|
|
|
//
|
|
|
|
// TODO: Go 1.5 allows to customize the error behavior, we can probably get rid of this as soon as
|
|
|
|
// we build Docker with that version:
|
|
|
|
// https://golang.org/pkg/text/template/#Template.Option
|
|
|
|
func (cli *DockerCli) decodeRawInspect(tmpl *template.Template, dec *json.Decoder) (*bytes.Buffer, bool) {
|
|
|
|
var raw interface{}
|
|
|
|
buf := bytes.NewBufferString("")
|
|
|
|
|
|
|
|
if rawErr := dec.Decode(&raw); rawErr != nil {
|
|
|
|
fmt.Fprintf(cli.err, "Unable to read inspect data: %v\n", rawErr)
|
|
|
|
return buf, false
|
|
|
|
}
|
|
|
|
|
|
|
|
if rawErr := tmpl.Execute(buf, raw); rawErr != nil {
|
|
|
|
return buf, false
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.Contains(buf.String(), "<no value>") {
|
|
|
|
return buf, false
|
|
|
|
}
|
|
|
|
return buf, true
|
|
|
|
}
|