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