2015-03-24 23:57:23 -04:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"text/template"
|
|
|
|
|
2015-12-06 18:34:25 -05:00
|
|
|
"github.com/docker/docker/api/client/inspect"
|
|
|
|
"github.com/docker/docker/api/client/lib"
|
2015-05-05 00:18:28 -04:00
|
|
|
Cli "github.com/docker/docker/cli"
|
2015-03-24 23:57:23 -04:00
|
|
|
flag "github.com/docker/docker/pkg/mflag"
|
|
|
|
)
|
|
|
|
|
2015-05-05 00:18:28 -04:00
|
|
|
var funcMap = template.FuncMap{
|
|
|
|
"json": func(v interface{}) string {
|
|
|
|
a, _ := json.Marshal(v)
|
|
|
|
return string(a)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-03-25 13:34:41 -04:00
|
|
|
// CmdInspect displays low-level information on one or more containers or images.
|
|
|
|
//
|
|
|
|
// Usage: docker inspect [OPTIONS] CONTAINER|IMAGE [CONTAINER|IMAGE...]
|
2015-03-24 23:57:23 -04:00
|
|
|
func (cli *DockerCli) CmdInspect(args ...string) error {
|
2015-10-08 08:46:21 -04:00
|
|
|
cmd := Cli.Subcmd("inspect", []string{"CONTAINER|IMAGE [CONTAINER|IMAGE...]"}, Cli.DockerCommands["inspect"].Description, true)
|
2015-11-09 09:37:24 -05:00
|
|
|
tmplStr := cmd.String([]string{"f", "-format"}, "", "Format the output using the given go template")
|
2015-06-26 10:47:31 -04:00
|
|
|
inspectType := cmd.String([]string{"-type"}, "", "Return JSON for specified type, (e.g image or container)")
|
2015-09-25 08:49:02 -04:00
|
|
|
size := cmd.Bool([]string{"s", "-size"}, false, "Display total file sizes if the type is container")
|
2015-03-24 23:57:23 -04:00
|
|
|
cmd.Require(flag.Min, 1)
|
|
|
|
|
2015-03-28 21:22:46 -04:00
|
|
|
cmd.ParseFlags(args, true)
|
2015-03-24 23:57:23 -04:00
|
|
|
|
2015-12-06 18:34:25 -05:00
|
|
|
if *inspectType != "" && *inspectType != "container" && *inspectType != "image" {
|
|
|
|
return fmt.Errorf("%q is not a valid value for --type", *inspectType)
|
|
|
|
}
|
|
|
|
|
2015-12-07 22:04:38 -05:00
|
|
|
var elementSearcher inspectSearcher
|
2015-12-06 18:34:25 -05:00
|
|
|
switch *inspectType {
|
|
|
|
case "container":
|
2015-12-07 22:04:38 -05:00
|
|
|
elementSearcher = cli.inspectContainers(*size)
|
|
|
|
case "image":
|
|
|
|
elementSearcher = cli.inspectImages(*size)
|
2015-12-06 18:34:25 -05:00
|
|
|
default:
|
2015-12-07 22:04:38 -05:00
|
|
|
elementSearcher = cli.inspectAll(*size)
|
2015-12-06 18:34:25 -05:00
|
|
|
}
|
2015-03-24 23:57:23 -04:00
|
|
|
|
2015-12-07 22:04:38 -05:00
|
|
|
return cli.inspectElements(*tmplStr, cmd.Args(), elementSearcher)
|
2015-12-06 18:34:25 -05:00
|
|
|
}
|
2015-09-25 08:49:02 -04:00
|
|
|
|
2015-12-07 22:04:38 -05:00
|
|
|
func (cli *DockerCli) inspectContainers(getSize bool) inspectSearcher {
|
|
|
|
return func(ref string) (interface{}, []byte, error) {
|
|
|
|
return cli.client.ContainerInspectWithRaw(ref, getSize)
|
2015-12-06 18:34:25 -05:00
|
|
|
}
|
|
|
|
}
|
2015-06-26 10:47:31 -04:00
|
|
|
|
2015-12-07 22:04:38 -05:00
|
|
|
func (cli *DockerCli) inspectImages(getSize bool) inspectSearcher {
|
|
|
|
return func(ref string) (interface{}, []byte, error) {
|
|
|
|
return cli.client.ImageInspectWithRaw(ref, getSize)
|
2015-12-06 18:34:25 -05:00
|
|
|
}
|
|
|
|
}
|
2015-03-24 23:57:23 -04:00
|
|
|
|
2015-12-07 22:04:38 -05:00
|
|
|
func (cli *DockerCli) inspectAll(getSize bool) inspectSearcher {
|
|
|
|
return func(ref string) (interface{}, []byte, error) {
|
|
|
|
c, rawContainer, err := cli.client.ContainerInspectWithRaw(ref, getSize)
|
|
|
|
if err != nil {
|
2015-12-06 18:34:25 -05:00
|
|
|
// Search for image with that id if a container doesn't exist.
|
|
|
|
if lib.IsErrContainerNotFound(err) {
|
2015-12-07 22:04:38 -05:00
|
|
|
i, rawImage, err := cli.client.ImageInspectWithRaw(ref, getSize)
|
|
|
|
if err != nil {
|
2015-12-06 18:34:25 -05:00
|
|
|
if lib.IsErrImageNotFound(err) {
|
2015-12-07 22:04:38 -05:00
|
|
|
return nil, nil, fmt.Errorf("Error: No such image or container: %s", ref)
|
2015-05-11 20:39:21 -04:00
|
|
|
}
|
2015-12-07 22:04:38 -05:00
|
|
|
return nil, nil, err
|
2015-03-26 15:43:00 -04:00
|
|
|
}
|
2015-12-07 22:04:38 -05:00
|
|
|
return i, rawImage, err
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
2015-12-07 22:04:38 -05:00
|
|
|
return nil, nil, err
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
2015-12-07 22:04:38 -05:00
|
|
|
return c, rawContainer, err
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
|
|
|
}
|
2015-10-22 18:42:19 -04:00
|
|
|
|
2015-12-07 22:04:38 -05:00
|
|
|
type inspectSearcher func(ref string) (interface{}, []byte, error)
|
|
|
|
|
|
|
|
func (cli *DockerCli) inspectElements(tmplStr string, references []string, searchByReference inspectSearcher) error {
|
|
|
|
elementInspector, err := cli.newInspectorWithTemplate(tmplStr)
|
2015-12-06 18:34:25 -05:00
|
|
|
if err != nil {
|
2015-12-07 22:04:38 -05:00
|
|
|
return Cli.StatusError{StatusCode: 64, Status: err.Error()}
|
|
|
|
}
|
|
|
|
|
|
|
|
var inspectErr error
|
|
|
|
for _, ref := range references {
|
|
|
|
element, raw, err := searchByReference(ref)
|
|
|
|
if err != nil {
|
|
|
|
inspectErr = err
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := elementInspector.Inspect(element, raw); err != nil {
|
|
|
|
inspectErr = err
|
|
|
|
break
|
|
|
|
}
|
2015-10-22 18:42:19 -04:00
|
|
|
}
|
|
|
|
|
2015-12-07 22:04:38 -05:00
|
|
|
if err := elementInspector.Flush(); err != nil {
|
|
|
|
cli.inspectErrorStatus(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if status := cli.inspectErrorStatus(inspectErr); status != 0 {
|
|
|
|
return Cli.StatusError{StatusCode: status}
|
|
|
|
}
|
|
|
|
return nil
|
2015-12-06 18:34:25 -05:00
|
|
|
}
|
2015-10-22 18:42:19 -04:00
|
|
|
|
2015-12-07 22:04:38 -05:00
|
|
|
func (cli *DockerCli) inspectErrorStatus(err error) (status int) {
|
2015-12-06 18:34:25 -05:00
|
|
|
if err != nil {
|
2015-12-07 22:04:38 -05:00
|
|
|
fmt.Fprintf(cli.err, "%s\n", err)
|
|
|
|
status = 1
|
2015-10-22 18:42:19 -04:00
|
|
|
}
|
2015-12-07 22:04:38 -05:00
|
|
|
return
|
|
|
|
}
|
2015-12-06 18:34:25 -05:00
|
|
|
|
2015-12-07 22:04:38 -05:00
|
|
|
func (cli *DockerCli) newInspectorWithTemplate(tmplStr string) (inspect.Inspector, error) {
|
|
|
|
elementInspector := inspect.NewIndentedInspector(cli.out)
|
|
|
|
if tmplStr != "" {
|
|
|
|
tmpl, err := template.New("").Funcs(funcMap).Parse(tmplStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Template parsing error: %s", err)
|
|
|
|
}
|
|
|
|
elementInspector = inspect.NewTemplateInspector(cli.out, tmpl)
|
|
|
|
}
|
|
|
|
return elementInspector, nil
|
2015-10-22 18:42:19 -04:00
|
|
|
}
|