inspect.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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":
  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 inspectAll(ctx context.Context, dockerCli *command.DockerCli, getSize bool, typeConstraint string) inspect.GetRefFunc {
  82. var inspectAutodetect = []struct {
  83. ObjectType string
  84. IsSizeSupported bool
  85. ObjectInspector func(string) (interface{}, []byte, error)
  86. }{
  87. {"container", true, inspectContainers(ctx, dockerCli, getSize)},
  88. {"image", false, inspectImages(ctx, dockerCli)},
  89. {"network", false, inspectNetwork(ctx, dockerCli)},
  90. {"volume", false, inspectVolume(ctx, dockerCli)},
  91. {"service", false, inspectService(ctx, dockerCli)},
  92. {"task", false, inspectTasks(ctx, dockerCli)},
  93. {"node", false, inspectNode(ctx, dockerCli)},
  94. }
  95. isErrNotSwarmManager := func(err error) bool {
  96. return strings.Contains(err.Error(), "This node is not a swarm manager")
  97. }
  98. return func(ref string) (interface{}, []byte, error) {
  99. for _, inspectData := range inspectAutodetect {
  100. if typeConstraint != "" && inspectData.ObjectType != typeConstraint {
  101. continue
  102. }
  103. v, raw, err := inspectData.ObjectInspector(ref)
  104. if err != nil {
  105. if typeConstraint == "" && (apiclient.IsErrNotFound(err) || isErrNotSwarmManager(err)) {
  106. continue
  107. }
  108. return v, raw, err
  109. }
  110. if getSize && !inspectData.IsSizeSupported {
  111. fmt.Fprintf(dockerCli.Err(), "WARNING: --size ignored for %s\n", inspectData.ObjectType)
  112. }
  113. return v, raw, err
  114. }
  115. return nil, nil, fmt.Errorf("Error: No such object: %s", ref)
  116. }
  117. }