Run is the top-level entry point to the CLI plugin framework. It should be called from the plugin's "main()" function. It initializes a new [command.DockerCli] instance with the given options before calling makeCmd to construct the plugin command, then invokes the plugin command using [RunPlugin].
(makeCmd func(command.Cli) *cobra.Command, meta metadata.Metadata, ops ...command.CLIOption)
| 86 | // makeCmd to construct the plugin command, then invokes the plugin command |
| 87 | // using [RunPlugin]. |
| 88 | func Run(makeCmd func(command.Cli) *cobra.Command, meta metadata.Metadata, ops ...command.CLIOption) { |
| 89 | otel.SetErrorHandler(debug.OTELErrorHandler) |
| 90 | |
| 91 | dockerCLI, err := command.NewDockerCli(ops...) |
| 92 | if err != nil { |
| 93 | _, _ = fmt.Fprintln(os.Stderr, err) |
| 94 | os.Exit(1) |
| 95 | } |
| 96 | |
| 97 | plugin := makeCmd(dockerCLI) |
| 98 | |
| 99 | if err := RunPlugin(dockerCLI, plugin, meta); err != nil { |
| 100 | var stErr cli.StatusError |
| 101 | if errors.As(err, &stErr) { |
| 102 | // StatusError should only be used for errors, and all errors should |
| 103 | // have a non-zero exit status, so never exit with 0 |
| 104 | if stErr.StatusCode == 0 { // FIXME(thaJeztah): this should never be used with a zero status-code. Check if we do this anywhere. |
| 105 | stErr.StatusCode = 1 |
| 106 | } |
| 107 | _, _ = fmt.Fprintln(dockerCLI.Err(), stErr) |
| 108 | os.Exit(stErr.StatusCode) |
| 109 | } |
| 110 | _, _ = fmt.Fprintln(dockerCLI.Err(), err) |
| 111 | os.Exit(1) |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | func withPluginClientConn(name string) command.CLIOption { |
| 116 | return func(cli *command.DockerCli) error { |