inspect.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package client
  2. import (
  3. "fmt"
  4. "golang.org/x/net/context"
  5. "github.com/docker/docker/api/client/inspect"
  6. Cli "github.com/docker/docker/cli"
  7. flag "github.com/docker/docker/pkg/mflag"
  8. "github.com/docker/engine-api/client"
  9. )
  10. // CmdInspect displays low-level information on one or more containers or images.
  11. //
  12. // Usage: docker inspect [OPTIONS] CONTAINER|IMAGE [CONTAINER|IMAGE...]
  13. func (cli *DockerCli) CmdInspect(args ...string) error {
  14. cmd := Cli.Subcmd("inspect", []string{"CONTAINER|IMAGE [CONTAINER|IMAGE...]"}, Cli.DockerCommands["inspect"].Description, true)
  15. tmplStr := cmd.String([]string{"f", "-format"}, "", "Format the output using the given go template")
  16. inspectType := cmd.String([]string{"-type"}, "", "Return JSON for specified type, (e.g image or container)")
  17. size := cmd.Bool([]string{"s", "-size"}, false, "Display total file sizes if the type is container")
  18. cmd.Require(flag.Min, 1)
  19. cmd.ParseFlags(args, true)
  20. if *inspectType != "" && *inspectType != "container" && *inspectType != "image" {
  21. return fmt.Errorf("%q is not a valid value for --type", *inspectType)
  22. }
  23. ctx := context.Background()
  24. var elementSearcher inspect.GetRefFunc
  25. switch *inspectType {
  26. case "container":
  27. elementSearcher = cli.inspectContainers(ctx, *size)
  28. case "image":
  29. elementSearcher = cli.inspectImages(ctx, *size)
  30. default:
  31. elementSearcher = cli.inspectAll(ctx, *size)
  32. }
  33. return inspect.Inspect(cli.out, cmd.Args(), *tmplStr, elementSearcher)
  34. }
  35. func (cli *DockerCli) inspectContainers(ctx context.Context, getSize bool) inspect.GetRefFunc {
  36. return func(ref string) (interface{}, []byte, error) {
  37. return cli.client.ContainerInspectWithRaw(ctx, ref, getSize)
  38. }
  39. }
  40. func (cli *DockerCli) inspectImages(ctx context.Context, getSize bool) inspect.GetRefFunc {
  41. return func(ref string) (interface{}, []byte, error) {
  42. return cli.client.ImageInspectWithRaw(ctx, ref, getSize)
  43. }
  44. }
  45. func (cli *DockerCli) inspectAll(ctx context.Context, getSize bool) inspect.GetRefFunc {
  46. return func(ref string) (interface{}, []byte, error) {
  47. c, rawContainer, err := cli.client.ContainerInspectWithRaw(ctx, ref, getSize)
  48. if err != nil {
  49. // Search for image with that id if a container doesn't exist.
  50. if client.IsErrContainerNotFound(err) {
  51. i, rawImage, err := cli.client.ImageInspectWithRaw(ctx, ref, getSize)
  52. if err != nil {
  53. if client.IsErrImageNotFound(err) {
  54. return nil, nil, fmt.Errorf("Error: No such image or container: %s", ref)
  55. }
  56. return nil, nil, err
  57. }
  58. return i, rawImage, err
  59. }
  60. return nil, nil, err
  61. }
  62. return c, rawContainer, err
  63. }
  64. }