inspect.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package container
  2. import (
  3. "golang.org/x/net/context"
  4. "github.com/docker/docker/cli"
  5. "github.com/docker/docker/cli/command"
  6. "github.com/docker/docker/cli/command/inspect"
  7. "github.com/spf13/cobra"
  8. )
  9. type inspectOptions struct {
  10. format string
  11. size bool
  12. refs []string
  13. }
  14. // newInspectCommand creates a new cobra.Command for `docker container inspect`
  15. func newInspectCommand(dockerCli *command.DockerCli) *cobra.Command {
  16. var opts inspectOptions
  17. cmd := &cobra.Command{
  18. Use: "inspect [OPTIONS] CONTAINER [CONTAINER...]",
  19. Short: "Display detailed information on one or more containers",
  20. Args: cli.RequiresMinArgs(1),
  21. RunE: func(cmd *cobra.Command, args []string) error {
  22. opts.refs = args
  23. return runInspect(dockerCli, opts)
  24. },
  25. }
  26. flags := cmd.Flags()
  27. flags.StringVarP(&opts.format, "format", "f", "", "Format the output using the given Go template")
  28. flags.BoolVarP(&opts.size, "size", "s", false, "Display total file sizes")
  29. return cmd
  30. }
  31. func runInspect(dockerCli *command.DockerCli, opts inspectOptions) error {
  32. client := dockerCli.Client()
  33. ctx := context.Background()
  34. getRefFunc := func(ref string) (interface{}, []byte, error) {
  35. return client.ContainerInspectWithRaw(ctx, ref, opts.size)
  36. }
  37. return inspect.Inspect(dockerCli.Out(), opts.refs, opts.format, getRefFunc)
  38. }