RunPlugin executes the specified plugin command
(dockerCli *command.DockerCli, plugin *cobra.Command, meta metadata.Metadata)
| 31 | |
| 32 | // RunPlugin executes the specified plugin command |
| 33 | func RunPlugin(dockerCli *command.DockerCli, plugin *cobra.Command, meta metadata.Metadata) error { |
| 34 | tcmd := newPluginCommand(dockerCli, plugin, meta) |
| 35 | |
| 36 | var persistentPreRunOnce sync.Once |
| 37 | PersistentPreRunE = func(cmd *cobra.Command, _ []string) error { |
| 38 | var retErr error |
| 39 | persistentPreRunOnce.Do(func() { |
| 40 | ctx, cancel := context.WithCancel(cmd.Context()) |
| 41 | cmd.SetContext(ctx) |
| 42 | // Set up the context to cancel based on signalling via CLI socket. |
| 43 | socket.ConnectAndWait(cancel) |
| 44 | |
| 45 | var opts []command.CLIOption |
| 46 | if os.Getenv("DOCKER_CLI_PLUGIN_USE_DIAL_STDIO") != "" { |
| 47 | opts = append(opts, withPluginClientConn(plugin.Name())) |
| 48 | } |
| 49 | opts = append(opts, command.WithEnableGlobalMeterProvider(), command.WithEnableGlobalTracerProvider()) |
| 50 | retErr = tcmd.Initialize(opts...) |
| 51 | ogRunE := cmd.RunE |
| 52 | if ogRunE == nil { |
| 53 | ogRun := cmd.Run |
| 54 | // necessary because error will always be nil here |
| 55 | // see: https://github.com/golangci/golangci-lint/issues/1379 |
| 56 | //nolint:unparam |
| 57 | ogRunE = func(cmd *cobra.Command, args []string) error { |
| 58 | ogRun(cmd, args) |
| 59 | return nil |
| 60 | } |
| 61 | cmd.Run = nil |
| 62 | } |
| 63 | cmd.RunE = func(cmd *cobra.Command, args []string) error { |
| 64 | stopInstrumentation := dockerCli.StartInstrumentation(cmd) |
| 65 | err := ogRunE(cmd, args) |
| 66 | stopInstrumentation(err) |
| 67 | return err |
| 68 | } |
| 69 | }) |
| 70 | return retErr |
| 71 | } |
| 72 | |
| 73 | cmd, args, err := tcmd.HandleGlobalFlags() |
| 74 | if err != nil { |
| 75 | return err |
| 76 | } |
| 77 | // We've parsed global args already, so reset args to those |
| 78 | // which remain. |
| 79 | cmd.SetArgs(args) |
| 80 | return cmd.Execute() |
| 81 | } |
| 82 | |
| 83 | // Run is the top-level entry point to the CLI plugin framework. It should |
| 84 | // be called from the plugin's "main()" function. It initializes a new |
no test coverage detected
searching dependent graphs…