From a10f0363f3a91352d6db5e7f5034f087e1d674bb Mon Sep 17 00:00:00 2001 From: Alano Terblanche <18033717+Benehiko@users.noreply.github.com> Date: Wed, 10 Apr 2024 13:42:24 +0200 Subject: [PATCH] chore: don't break plugins but keep context for cobra cli Signed-off-by: Alano Terblanche <18033717+Benehiko@users.noreply.github.com> --- cli-plugins/examples/helloworld/main.go | 3 +-- cli-plugins/plugin/plugin.go | 14 +++++++------- cli/cobra.go | 12 +++++------- cmd/docker/builder_test.go | 8 ++++---- cmd/docker/docker.go | 8 ++++---- cmd/docker/docker_test.go | 4 ++-- docs/generate/generate.go | 4 +--- e2e/cli-plugins/plugins/nopersistentprerun/main.go | 4 +--- e2e/cli-plugins/plugins/presocket/main.go | 4 +--- 9 files changed, 26 insertions(+), 35 deletions(-) diff --git a/cli-plugins/examples/helloworld/main.go b/cli-plugins/examples/helloworld/main.go index 8a836b0f2d54..0461e01c8c1d 100644 --- a/cli-plugins/examples/helloworld/main.go +++ b/cli-plugins/examples/helloworld/main.go @@ -12,8 +12,7 @@ import ( ) func main() { - ctx := context.Background() - plugin.Run(ctx, func(dockerCli command.Cli) *cobra.Command { + plugin.Run(func(dockerCli command.Cli) *cobra.Command { goodbye := &cobra.Command{ Use: "goodbye", Short: "Say Goodbye instead of Hello", diff --git a/cli-plugins/plugin/plugin.go b/cli-plugins/plugin/plugin.go index cc9a4378ff1a..b16eb0866720 100644 --- a/cli-plugins/plugin/plugin.go +++ b/cli-plugins/plugin/plugin.go @@ -29,8 +29,8 @@ import ( var PersistentPreRunE func(*cobra.Command, []string) error // RunPlugin executes the specified plugin command -func RunPlugin(ctx context.Context, dockerCli *command.DockerCli, plugin *cobra.Command, meta manager.Metadata) error { - tcmd := newPluginCommand(ctx, dockerCli, plugin, meta) +func RunPlugin(dockerCli *command.DockerCli, plugin *cobra.Command, meta manager.Metadata) error { + tcmd := newPluginCommand(dockerCli, plugin, meta) var persistentPreRunOnce sync.Once PersistentPreRunE = func(cmd *cobra.Command, _ []string) error { @@ -61,10 +61,10 @@ func RunPlugin(ctx context.Context, dockerCli *command.DockerCli, plugin *cobra. } // Run is the top-level entry point to the CLI plugin framework. It should be called from your plugin's `main()` function. -func Run(ctx context.Context, makeCmd func(command.Cli) *cobra.Command, meta manager.Metadata) { +func Run(makeCmd func(command.Cli) *cobra.Command, meta manager.Metadata) { otel.SetErrorHandler(debug.OTELErrorHandler) - dockerCli, err := command.NewDockerCli(command.WithBaseContext(ctx)) + dockerCli, err := command.NewDockerCli() if err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) @@ -72,7 +72,7 @@ func Run(ctx context.Context, makeCmd func(command.Cli) *cobra.Command, meta man plugin := makeCmd(dockerCli) - if err := RunPlugin(ctx, dockerCli, plugin, meta); err != nil { + if err := RunPlugin(dockerCli, plugin, meta); err != nil { if sterr, ok := err.(cli.StatusError); ok { if sterr.Status != "" { fmt.Fprintln(dockerCli.Err(), sterr.Status) @@ -121,7 +121,7 @@ func withPluginClientConn(name string) command.CLIOption { }) } -func newPluginCommand(ctx context.Context, dockerCli *command.DockerCli, plugin *cobra.Command, meta manager.Metadata) *cli.TopLevelCommand { +func newPluginCommand(dockerCli *command.DockerCli, plugin *cobra.Command, meta manager.Metadata) *cli.TopLevelCommand { name := plugin.Name() fullname := manager.NamePrefix + name @@ -142,7 +142,7 @@ func newPluginCommand(ctx context.Context, dockerCli *command.DockerCli, plugin DisableDescriptions: true, }, } - opts, _ := cli.SetupPluginRootCommand(ctx, cmd) + opts, _ := cli.SetupPluginRootCommand(cmd) cmd.SetIn(dockerCli.In()) cmd.SetOut(dockerCli.Out()) diff --git a/cli/cobra.go b/cli/cobra.go index 74a0a622ebff..d07a3b218a22 100644 --- a/cli/cobra.go +++ b/cli/cobra.go @@ -1,7 +1,6 @@ package cli import ( - "context" "fmt" "os" "path/filepath" @@ -23,8 +22,7 @@ import ( // setupCommonRootCommand contains the setup common to // SetupRootCommand and SetupPluginRootCommand. -func setupCommonRootCommand(ctx context.Context, rootCmd *cobra.Command) (*cliflags.ClientOptions, *cobra.Command) { - rootCmd.SetContext(ctx) +func setupCommonRootCommand(rootCmd *cobra.Command) (*cliflags.ClientOptions, *cobra.Command) { opts := cliflags.NewClientOptions() opts.InstallFlags(rootCmd.Flags()) @@ -76,14 +74,14 @@ func setupCommonRootCommand(ctx context.Context, rootCmd *cobra.Command) (*clifl // SetupRootCommand sets default usage, help, and error handling for the // root command. -func SetupRootCommand(ctx context.Context, rootCmd *cobra.Command) (opts *cliflags.ClientOptions, helpCmd *cobra.Command) { +func SetupRootCommand(rootCmd *cobra.Command) (opts *cliflags.ClientOptions, helpCmd *cobra.Command) { rootCmd.SetVersionTemplate("Docker version {{.Version}}\n") - return setupCommonRootCommand(ctx, rootCmd) + return setupCommonRootCommand(rootCmd) } // SetupPluginRootCommand sets default usage, help and error handling for a plugin root command. -func SetupPluginRootCommand(ctx context.Context, rootCmd *cobra.Command) (*cliflags.ClientOptions, *pflag.FlagSet) { - opts, _ := setupCommonRootCommand(ctx, rootCmd) +func SetupPluginRootCommand(rootCmd *cobra.Command) (*cliflags.ClientOptions, *pflag.FlagSet) { + opts, _ := setupCommonRootCommand(rootCmd) return opts, rootCmd.Flags() } diff --git a/cmd/docker/builder_test.go b/cmd/docker/builder_test.go index 505bcd610e0f..797d42140c9e 100644 --- a/cmd/docker/builder_test.go +++ b/cmd/docker/builder_test.go @@ -105,7 +105,7 @@ echo '{"SchemaVersion":"0.1.0","Vendor":"Docker Inc.","Version":"v0.6.3","ShortD dockerCli.ConfigFile().Aliases = map[string]string{"builder": "buildx"} } - tcmd := newDockerCommand(ctx2, dockerCli) + tcmd := newDockerCommand(dockerCli) tcmd.SetArgs([]string{"build", "."}) cmd, args, err := tcmd.HandleGlobalFlags() @@ -155,7 +155,7 @@ func TestBuildkitDisabled(t *testing.T) { assert.NilError(t, dockerCli.Initialize(flags.NewClientOptions())) dockerCli.ConfigFile().CLIPluginsExtraDirs = []string{dir.Path()} - tcmd := newDockerCommand(ctx, dockerCli) + tcmd := newDockerCommand(dockerCli) tcmd.SetArgs([]string{"build", "."}) cmd, args, err := tcmd.HandleGlobalFlags() @@ -194,7 +194,7 @@ func TestBuilderBroken(t *testing.T) { assert.NilError(t, dockerCli.Initialize(flags.NewClientOptions())) dockerCli.ConfigFile().CLIPluginsExtraDirs = []string{dir.Path()} - tcmd := newDockerCommand(ctx, dockerCli) + tcmd := newDockerCommand(dockerCli) tcmd.SetArgs([]string{"build", "."}) cmd, args, err := tcmd.HandleGlobalFlags() @@ -234,7 +234,7 @@ func TestBuilderBrokenEnforced(t *testing.T) { assert.NilError(t, dockerCli.Initialize(flags.NewClientOptions())) dockerCli.ConfigFile().CLIPluginsExtraDirs = []string{dir.Path()} - tcmd := newDockerCommand(ctx, dockerCli) + tcmd := newDockerCommand(dockerCli) tcmd.SetArgs([]string{"build", "."}) cmd, args, err := tcmd.HandleGlobalFlags() diff --git a/cmd/docker/docker.go b/cmd/docker/docker.go index 58827427bf96..e8be12569ce9 100644 --- a/cmd/docker/docker.go +++ b/cmd/docker/docker.go @@ -58,7 +58,7 @@ func main() { } } -func newDockerCommand(ctx context.Context, dockerCli *command.DockerCli) *cli.TopLevelCommand { +func newDockerCommand(dockerCli *command.DockerCli) *cli.TopLevelCommand { var ( opts *cliflags.ClientOptions helpCmd *cobra.Command @@ -91,7 +91,7 @@ func newDockerCommand(ctx context.Context, dockerCli *command.DockerCli) *cli.To cmd.SetOut(dockerCli.Out()) cmd.SetErr(dockerCli.Err()) - opts, helpCmd = cli.SetupRootCommand(ctx, cmd) + opts, helpCmd = cli.SetupRootCommand(cmd) _ = registerCompletionFuncForGlobalFlags(dockerCli.ContextStore(), cmd) cmd.Flags().BoolP("version", "v", false, "Print version information and quit") setFlagErrorFunc(dockerCli, cmd) @@ -297,7 +297,7 @@ func tryPluginRun(dockerCli command.Cli, cmd *cobra.Command, subcommand string, //nolint:gocyclo func runDocker(ctx context.Context, dockerCli *command.DockerCli) error { - tcmd := newDockerCommand(ctx, dockerCli) + tcmd := newDockerCommand(dockerCli) cmd, args, err := tcmd.HandleGlobalFlags() if err != nil { @@ -353,7 +353,7 @@ func runDocker(ctx context.Context, dockerCli *command.DockerCli) error { // We've parsed global args already, so reset args to those // which remain. cmd.SetArgs(args) - err = cmd.Execute() + err = cmd.ExecuteContext(ctx) if err != nil { return err } diff --git a/cmd/docker/docker_test.go b/cmd/docker/docker_test.go index 5b331a2423b4..8fc7ade3a44f 100644 --- a/cmd/docker/docker_test.go +++ b/cmd/docker/docker_test.go @@ -21,7 +21,7 @@ func TestClientDebugEnabled(t *testing.T) { cli, err := command.NewDockerCli(command.WithBaseContext(ctx)) assert.NilError(t, err) - tcmd := newDockerCommand(ctx, cli) + tcmd := newDockerCommand(cli) tcmd.SetFlag("debug", "true") cmd, _, err := tcmd.HandleGlobalFlags() assert.NilError(t, err) @@ -50,7 +50,7 @@ func runCliCommand(t *testing.T, r io.ReadCloser, w io.Writer, args ...string) e command.WithInputStream(r), command.WithCombinedStreams(w)) assert.NilError(t, err) - tcmd := newDockerCommand(ctx, cli) + tcmd := newDockerCommand(cli) tcmd.SetArgs(args) cmd, _, err := tcmd.HandleGlobalFlags() diff --git a/docs/generate/generate.go b/docs/generate/generate.go index fd30c93e25b8..a12c7d4ccf95 100644 --- a/docs/generate/generate.go +++ b/docs/generate/generate.go @@ -6,7 +6,6 @@ package main import ( - "context" "log" "os" @@ -39,8 +38,7 @@ func gen(opts *options) error { Short: "The base command for the Docker CLI.", } - ctx := context.Background() - clientOpts, _ := cli.SetupRootCommand(ctx, cmd) + clientOpts, _ := cli.SetupRootCommand(cmd) if err := dockerCLI.Initialize(clientOpts); err != nil { return err } diff --git a/e2e/cli-plugins/plugins/nopersistentprerun/main.go b/e2e/cli-plugins/plugins/nopersistentprerun/main.go index da84e30bee1f..b90cc67aee97 100644 --- a/e2e/cli-plugins/plugins/nopersistentprerun/main.go +++ b/e2e/cli-plugins/plugins/nopersistentprerun/main.go @@ -1,7 +1,6 @@ package main import ( - "context" "fmt" "github.com/docker/cli/cli-plugins/manager" @@ -11,8 +10,7 @@ import ( ) func main() { - ctx := context.Background() - plugin.Run(ctx, func(dockerCli command.Cli) *cobra.Command { + plugin.Run(func(dockerCli command.Cli) *cobra.Command { return &cobra.Command{ Use: "nopersistentprerun", Short: "Testing without PersistentPreRun hooks", diff --git a/e2e/cli-plugins/plugins/presocket/main.go b/e2e/cli-plugins/plugins/presocket/main.go index 1ce85eafc13a..6cdf87a42402 100644 --- a/e2e/cli-plugins/plugins/presocket/main.go +++ b/e2e/cli-plugins/plugins/presocket/main.go @@ -1,7 +1,6 @@ package main import ( - "context" "fmt" "os" "os/signal" @@ -15,8 +14,7 @@ import ( ) func main() { - ctx := context.Background() - plugin.Run(ctx, RootCmd, manager.Metadata{ + plugin.Run(RootCmd, manager.Metadata{ SchemaVersion: "0.1.0", Vendor: "Docker Inc.", Version: "test",