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