inspect.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package node
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/docker/docker/cli"
  6. "github.com/docker/docker/cli/command"
  7. "github.com/docker/docker/cli/command/formatter"
  8. "github.com/spf13/cobra"
  9. "golang.org/x/net/context"
  10. )
  11. type inspectOptions struct {
  12. nodeIds []string
  13. format string
  14. pretty bool
  15. }
  16. func newInspectCommand(dockerCli command.Cli) *cobra.Command {
  17. var opts inspectOptions
  18. cmd := &cobra.Command{
  19. Use: "inspect [OPTIONS] self|NODE [NODE...]",
  20. Short: "Display detailed information on one or more nodes",
  21. Args: cli.RequiresMinArgs(1),
  22. RunE: func(cmd *cobra.Command, args []string) error {
  23. opts.nodeIds = args
  24. return runInspect(dockerCli, opts)
  25. },
  26. }
  27. flags := cmd.Flags()
  28. flags.StringVarP(&opts.format, "format", "f", "", "Format the output using the given Go template")
  29. flags.BoolVar(&opts.pretty, "pretty", false, "Print the information in a human friendly format")
  30. return cmd
  31. }
  32. func runInspect(dockerCli command.Cli, opts inspectOptions) error {
  33. client := dockerCli.Client()
  34. ctx := context.Background()
  35. if opts.pretty {
  36. opts.format = "pretty"
  37. }
  38. getRef := func(ref string) (interface{}, []byte, error) {
  39. nodeRef, err := Reference(ctx, client, ref)
  40. if err != nil {
  41. return nil, nil, err
  42. }
  43. node, _, err := client.NodeInspectWithRaw(ctx, nodeRef)
  44. return node, nil, err
  45. }
  46. f := opts.format
  47. // check if the user is trying to apply a template to the pretty format, which
  48. // is not supported
  49. if strings.HasPrefix(f, "pretty") && f != "pretty" {
  50. return fmt.Errorf("Cannot supply extra formatting options to the pretty template")
  51. }
  52. nodeCtx := formatter.Context{
  53. Output: dockerCli.Out(),
  54. Format: formatter.NewNodeFormat(f, false),
  55. }
  56. if err := formatter.NodeInspectWrite(nodeCtx, opts.nodeIds, getRef); err != nil {
  57. return cli.StatusError{StatusCode: 1, Status: err.Error()}
  58. }
  59. return nil
  60. }