inspect.go 4.0 KB

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