inspect.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package system
  2. import (
  3. "fmt"
  4. "strings"
  5. "golang.org/x/net/context"
  6. "github.com/docker/docker/cli"
  7. "github.com/docker/docker/cli/command"
  8. "github.com/docker/docker/cli/command/inspect"
  9. apiclient "github.com/docker/docker/client"
  10. "github.com/spf13/cobra"
  11. )
  12. type inspectOptions struct {
  13. format string
  14. inspectType string
  15. size bool
  16. ids []string
  17. }
  18. // NewInspectCommand creates a new cobra.Command for `docker inspect`
  19. func NewInspectCommand(dockerCli *command.DockerCli) *cobra.Command {
  20. var opts inspectOptions
  21. cmd := &cobra.Command{
  22. Use: "inspect [OPTIONS] NAME|ID [NAME|ID...]",
  23. Short: "Return low-level information on Docker objects",
  24. Args: cli.RequiresMinArgs(1),
  25. RunE: func(cmd *cobra.Command, args []string) error {
  26. opts.ids = args
  27. return runInspect(dockerCli, opts)
  28. },
  29. }
  30. flags := cmd.Flags()
  31. flags.StringVarP(&opts.format, "format", "f", "", "Format the output using the given Go template")
  32. flags.StringVar(&opts.inspectType, "type", "", "Return JSON for specified type")
  33. flags.BoolVarP(&opts.size, "size", "s", false, "Display total file sizes if the type is container")
  34. return cmd
  35. }
  36. func runInspect(dockerCli *command.DockerCli, opts inspectOptions) error {
  37. var elementSearcher inspect.GetRefFunc
  38. switch opts.inspectType {
  39. case "", "container", "image", "node", "network", "service", "volume", "task", "plugin":
  40. elementSearcher = inspectAll(context.Background(), dockerCli, opts.size, opts.inspectType)
  41. default:
  42. return fmt.Errorf("%q is not a valid value for --type", opts.inspectType)
  43. }
  44. return inspect.Inspect(dockerCli.Out(), opts.ids, opts.format, elementSearcher)
  45. }
  46. func inspectContainers(ctx context.Context, dockerCli *command.DockerCli, getSize bool) inspect.GetRefFunc {
  47. return func(ref string) (interface{}, []byte, error) {
  48. return dockerCli.Client().ContainerInspectWithRaw(ctx, ref, getSize)
  49. }
  50. }
  51. func inspectImages(ctx context.Context, dockerCli *command.DockerCli) inspect.GetRefFunc {
  52. return func(ref string) (interface{}, []byte, error) {
  53. return dockerCli.Client().ImageInspectWithRaw(ctx, ref)
  54. }
  55. }
  56. func inspectNetwork(ctx context.Context, dockerCli *command.DockerCli) inspect.GetRefFunc {
  57. return func(ref string) (interface{}, []byte, error) {
  58. return dockerCli.Client().NetworkInspectWithRaw(ctx, ref)
  59. }
  60. }
  61. func inspectNode(ctx context.Context, dockerCli *command.DockerCli) inspect.GetRefFunc {
  62. return func(ref string) (interface{}, []byte, error) {
  63. return dockerCli.Client().NodeInspectWithRaw(ctx, ref)
  64. }
  65. }
  66. func inspectService(ctx context.Context, dockerCli *command.DockerCli) inspect.GetRefFunc {
  67. return func(ref string) (interface{}, []byte, error) {
  68. return dockerCli.Client().ServiceInspectWithRaw(ctx, ref)
  69. }
  70. }
  71. func inspectTasks(ctx context.Context, dockerCli *command.DockerCli) inspect.GetRefFunc {
  72. return func(ref string) (interface{}, []byte, error) {
  73. return dockerCli.Client().TaskInspectWithRaw(ctx, ref)
  74. }
  75. }
  76. func inspectVolume(ctx context.Context, dockerCli *command.DockerCli) inspect.GetRefFunc {
  77. return func(ref string) (interface{}, []byte, error) {
  78. return dockerCli.Client().VolumeInspectWithRaw(ctx, ref)
  79. }
  80. }
  81. func inspectPlugin(ctx context.Context, dockerCli *command.DockerCli) inspect.GetRefFunc {
  82. return func(ref string) (interface{}, []byte, error) {
  83. return dockerCli.Client().PluginInspectWithRaw(ctx, ref)
  84. }
  85. }
  86. func inspectAll(ctx context.Context, dockerCli *command.DockerCli, getSize bool, typeConstraint string) inspect.GetRefFunc {
  87. var inspectAutodetect = []struct {
  88. ObjectType string
  89. IsSizeSupported bool
  90. ObjectInspector func(string) (interface{}, []byte, error)
  91. }{
  92. {"container", true, inspectContainers(ctx, dockerCli, getSize)},
  93. {"image", false, inspectImages(ctx, dockerCli)},
  94. {"network", false, inspectNetwork(ctx, dockerCli)},
  95. {"volume", false, inspectVolume(ctx, dockerCli)},
  96. {"service", false, inspectService(ctx, dockerCli)},
  97. {"task", false, inspectTasks(ctx, dockerCli)},
  98. {"node", false, inspectNode(ctx, dockerCli)},
  99. {"plugin", false, inspectPlugin(ctx, dockerCli)},
  100. }
  101. isErrNotSwarmManager := func(err error) bool {
  102. return strings.Contains(err.Error(), "This node is not a swarm manager")
  103. }
  104. isErrNotSupported := func(err error) bool {
  105. return strings.Contains(err.Error(), "not supported")
  106. }
  107. return func(ref string) (interface{}, []byte, error) {
  108. for _, inspectData := range inspectAutodetect {
  109. if typeConstraint != "" && inspectData.ObjectType != typeConstraint {
  110. continue
  111. }
  112. v, raw, err := inspectData.ObjectInspector(ref)
  113. if err != nil {
  114. if typeConstraint == "" && (apiclient.IsErrNotFound(err) || isErrNotSwarmManager(err) || isErrNotSupported(err)) {
  115. continue
  116. }
  117. return v, raw, err
  118. }
  119. if getSize && !inspectData.IsSizeSupported {
  120. fmt.Fprintf(dockerCli.Err(), "WARNING: --size ignored for %s\n", inspectData.ObjectType)
  121. }
  122. return v, raw, err
  123. }
  124. return nil, nil, fmt.Errorf("Error: No such object: %s", ref)
  125. }
  126. }