github.com/guilhermebr/docker@v1.4.2-0.20150428121140-67da055cebca/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 19 func (cli *DockerCli) CmdInspect(args ...string) error { 20 cmd := cli.Subcmd("inspect", "CONTAINER|IMAGE [CONTAINER|IMAGE...]", "Return low-level information on a container or image", true) 21 tmplStr := cmd.String([]string{"f", "#format", "-format"}, "", "Format the output using the given go template") 22 cmd.Require(flag.Min, 1) 23 24 cmd.ParseFlags(args, true) 25 26 var tmpl *template.Template 27 if *tmplStr != "" { 28 var err error 29 if tmpl, err = template.New("").Funcs(funcMap).Parse(*tmplStr); err != nil { 30 fmt.Fprintf(cli.err, "Template parsing error: %v\n", err) 31 return StatusError{StatusCode: 64, 32 Status: "Template parsing error: " + err.Error()} 33 } 34 } 35 36 indented := new(bytes.Buffer) 37 indented.WriteByte('[') 38 status := 0 39 isImage := false 40 41 for _, name := range cmd.Args() { 42 obj, _, err := readBody(cli.call("GET", "/containers/"+name+"/json", nil, nil)) 43 if err != nil { 44 obj, _, err = readBody(cli.call("GET", "/images/"+name+"/json", nil, nil)) 45 isImage = true 46 if err != nil { 47 if strings.Contains(err.Error(), "No such") { 48 fmt.Fprintf(cli.err, "Error: No such image or container: %s\n", name) 49 } else { 50 fmt.Fprintf(cli.err, "%s", err) 51 } 52 status = 1 53 continue 54 } 55 } 56 57 if tmpl == nil { 58 if err = json.Indent(indented, obj, "", " "); err != nil { 59 fmt.Fprintf(cli.err, "%s\n", err) 60 status = 1 61 continue 62 } 63 } else { 64 dec := json.NewDecoder(bytes.NewReader(obj)) 65 66 if isImage { 67 inspPtr := types.ImageInspect{} 68 if err := dec.Decode(&inspPtr); err != nil { 69 fmt.Fprintf(cli.err, "%s\n", err) 70 status = 1 71 continue 72 } 73 if err := tmpl.Execute(cli.out, inspPtr); err != nil { 74 return err 75 } 76 } else { 77 inspPtr := types.ContainerJSON{} 78 if err := dec.Decode(&inspPtr); err != nil { 79 fmt.Fprintf(cli.err, "%s\n", err) 80 status = 1 81 continue 82 } 83 if err := tmpl.Execute(cli.out, inspPtr); err != nil { 84 return err 85 86 } 87 } 88 cli.out.Write([]byte{'\n'}) 89 } 90 indented.WriteString(",") 91 } 92 93 if indented.Len() > 1 { 94 // Remove trailing ',' 95 indented.Truncate(indented.Len() - 1) 96 } 97 indented.WriteString("]\n") 98 99 if tmpl == nil { 100 if _, err := io.Copy(cli.out, indented); err != nil { 101 return err 102 } 103 } 104 105 if status != 0 { 106 return StatusError{StatusCode: status} 107 } 108 return nil 109 }