github.com/hustcat/docker@v1.3.3-0.20160314103604-901c67a8eeab/api/client/inspect.go (about) 1 package client 2 3 import ( 4 "fmt" 5 6 "github.com/docker/docker/api/client/inspect" 7 Cli "github.com/docker/docker/cli" 8 flag "github.com/docker/docker/pkg/mflag" 9 "github.com/docker/docker/utils/templates" 10 "github.com/docker/engine-api/client" 11 ) 12 13 // CmdInspect displays low-level information on one or more containers or images. 14 // 15 // Usage: docker inspect [OPTIONS] CONTAINER|IMAGE [CONTAINER|IMAGE...] 16 func (cli *DockerCli) CmdInspect(args ...string) error { 17 cmd := Cli.Subcmd("inspect", []string{"CONTAINER|IMAGE [CONTAINER|IMAGE...]"}, Cli.DockerCommands["inspect"].Description, true) 18 tmplStr := cmd.String([]string{"f", "-format"}, "", "Format the output using the given go template") 19 inspectType := cmd.String([]string{"-type"}, "", "Return JSON for specified type, (e.g image or container)") 20 size := cmd.Bool([]string{"s", "-size"}, false, "Display total file sizes if the type is container") 21 cmd.Require(flag.Min, 1) 22 23 cmd.ParseFlags(args, true) 24 25 if *inspectType != "" && *inspectType != "container" && *inspectType != "image" { 26 return fmt.Errorf("%q is not a valid value for --type", *inspectType) 27 } 28 29 var elementSearcher inspectSearcher 30 switch *inspectType { 31 case "container": 32 elementSearcher = cli.inspectContainers(*size) 33 case "image": 34 elementSearcher = cli.inspectImages(*size) 35 default: 36 elementSearcher = cli.inspectAll(*size) 37 } 38 39 return cli.inspectElements(*tmplStr, cmd.Args(), elementSearcher) 40 } 41 42 func (cli *DockerCli) inspectContainers(getSize bool) inspectSearcher { 43 return func(ref string) (interface{}, []byte, error) { 44 return cli.client.ContainerInspectWithRaw(ref, getSize) 45 } 46 } 47 48 func (cli *DockerCli) inspectImages(getSize bool) inspectSearcher { 49 return func(ref string) (interface{}, []byte, error) { 50 return cli.client.ImageInspectWithRaw(ref, getSize) 51 } 52 } 53 54 func (cli *DockerCli) inspectAll(getSize bool) inspectSearcher { 55 return func(ref string) (interface{}, []byte, error) { 56 c, rawContainer, err := cli.client.ContainerInspectWithRaw(ref, getSize) 57 if err != nil { 58 // Search for image with that id if a container doesn't exist. 59 if client.IsErrContainerNotFound(err) { 60 i, rawImage, err := cli.client.ImageInspectWithRaw(ref, getSize) 61 if err != nil { 62 if client.IsErrImageNotFound(err) { 63 return nil, nil, fmt.Errorf("Error: No such image or container: %s", ref) 64 } 65 return nil, nil, err 66 } 67 return i, rawImage, err 68 } 69 return nil, nil, err 70 } 71 return c, rawContainer, err 72 } 73 } 74 75 type inspectSearcher func(ref string) (interface{}, []byte, error) 76 77 func (cli *DockerCli) inspectElements(tmplStr string, references []string, searchByReference inspectSearcher) error { 78 elementInspector, err := cli.newInspectorWithTemplate(tmplStr) 79 if err != nil { 80 return Cli.StatusError{StatusCode: 64, Status: err.Error()} 81 } 82 83 var inspectErr error 84 for _, ref := range references { 85 element, raw, err := searchByReference(ref) 86 if err != nil { 87 inspectErr = err 88 break 89 } 90 91 if err := elementInspector.Inspect(element, raw); err != nil { 92 inspectErr = err 93 break 94 } 95 } 96 97 if err := elementInspector.Flush(); err != nil { 98 cli.inspectErrorStatus(err) 99 } 100 101 if status := cli.inspectErrorStatus(inspectErr); status != 0 { 102 return Cli.StatusError{StatusCode: status} 103 } 104 return nil 105 } 106 107 func (cli *DockerCli) inspectErrorStatus(err error) (status int) { 108 if err != nil { 109 fmt.Fprintf(cli.err, "%s\n", err) 110 status = 1 111 } 112 return 113 } 114 115 func (cli *DockerCli) newInspectorWithTemplate(tmplStr string) (inspect.Inspector, error) { 116 elementInspector := inspect.NewIndentedInspector(cli.out) 117 if tmplStr != "" { 118 tmpl, err := templates.Parse(tmplStr) 119 if err != nil { 120 return nil, fmt.Errorf("Template parsing error: %s", err) 121 } 122 elementInspector = inspect.NewTemplateInspector(cli.out, tmpl) 123 } 124 return elementInspector, nil 125 }