github.com/squaremo/docker@v1.3.2-0.20150516120342-42cfc9554972/api/client/inspect.go (about) 1 package client 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "fmt" 7 "io" 8 "strings" 9 "text/template" 10 11 "github.com/docker/docker/api/types" 12 flag "github.com/docker/docker/pkg/mflag" 13 ) 14 15 // CmdInspect displays low-level information on one or more containers or images. 16 // 17 // Usage: docker inspect [OPTIONS] CONTAINER|IMAGE [CONTAINER|IMAGE...] 18 func (cli *DockerCli) CmdInspect(args ...string) error { 19 cmd := cli.Subcmd("inspect", "CONTAINER|IMAGE [CONTAINER|IMAGE...]", "Return low-level information on a container or image", true) 20 tmplStr := cmd.String([]string{"f", "#format", "-format"}, "", "Format the output using the given go template") 21 cmd.Require(flag.Min, 1) 22 23 cmd.ParseFlags(args, true) 24 25 var tmpl *template.Template 26 if *tmplStr != "" { 27 var err error 28 if tmpl, err = template.New("").Funcs(funcMap).Parse(*tmplStr); err != nil { 29 return StatusError{StatusCode: 64, 30 Status: "Template parsing error: " + err.Error()} 31 } 32 } 33 34 indented := new(bytes.Buffer) 35 indented.WriteString("[\n") 36 status := 0 37 isImage := false 38 39 for _, name := range cmd.Args() { 40 obj, _, err := readBody(cli.call("GET", "/containers/"+name+"/json", nil, nil)) 41 if err != nil { 42 obj, _, err = readBody(cli.call("GET", "/images/"+name+"/json", nil, nil)) 43 isImage = true 44 if err != nil { 45 if strings.Contains(err.Error(), "No such") { 46 fmt.Fprintf(cli.err, "Error: No such image or container: %s\n", name) 47 } else { 48 fmt.Fprintf(cli.err, "%s", err) 49 } 50 status = 1 51 continue 52 } 53 } 54 55 if tmpl == nil { 56 if err = json.Indent(indented, obj, "", " "); err != nil { 57 fmt.Fprintf(cli.err, "%s\n", err) 58 status = 1 59 continue 60 } 61 } else { 62 rdr := bytes.NewReader(obj) 63 dec := json.NewDecoder(rdr) 64 65 if isImage { 66 inspPtr := types.ImageInspect{} 67 if err := dec.Decode(&inspPtr); err != nil { 68 fmt.Fprintf(cli.err, "%s\n", err) 69 status = 1 70 continue 71 } 72 if err := tmpl.Execute(cli.out, inspPtr); err != nil { 73 rdr.Seek(0, 0) 74 var raw interface{} 75 if err := dec.Decode(&raw); err != nil { 76 return err 77 } 78 if err = tmpl.Execute(cli.out, raw); err != nil { 79 return err 80 } 81 } 82 } else { 83 inspPtr := types.ContainerJSON{} 84 if err := dec.Decode(&inspPtr); err != nil { 85 fmt.Fprintf(cli.err, "%s\n", err) 86 status = 1 87 continue 88 } 89 if err := tmpl.Execute(cli.out, inspPtr); err != nil { 90 rdr.Seek(0, 0) 91 var raw interface{} 92 if err := dec.Decode(&raw); err != nil { 93 return err 94 } 95 if err = tmpl.Execute(cli.out, raw); err != nil { 96 return err 97 } 98 } 99 } 100 cli.out.Write([]byte{'\n'}) 101 } 102 indented.WriteString(",") 103 } 104 105 if indented.Len() > 1 { 106 // Remove trailing ',' 107 indented.Truncate(indented.Len() - 1) 108 } 109 indented.WriteString("]\n") 110 111 if tmpl == nil { 112 if _, err := io.Copy(cli.out, indented); err != nil { 113 return err 114 } 115 } 116 117 if status != 0 { 118 return StatusError{StatusCode: status} 119 } 120 return nil 121 }