Skip to content

Commit

Permalink
chore: don't break plugins but keep context for cobra cli
Browse files Browse the repository at this point in the history
Signed-off-by: Alano Terblanche <[email protected]>
  • Loading branch information
Benehiko committed Apr 24, 2024
1 parent 15865fc commit 0aad14d
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 35 deletions.
3 changes: 1 addition & 2 deletions cli-plugins/examples/helloworld/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
14 changes: 7 additions & 7 deletions cli-plugins/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -61,18 +61,18 @@ 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)
}

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)
Expand Down Expand Up @@ -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

Expand All @@ -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())
Expand Down
12 changes: 5 additions & 7 deletions cli/cobra.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cli

import (
"context"
"fmt"
"os"
"path/filepath"
Expand All @@ -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())

Expand Down Expand Up @@ -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()
}

Expand Down
8 changes: 4 additions & 4 deletions cmd/docker/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
11 changes: 7 additions & 4 deletions cmd/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -353,7 +353,10 @@ 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
}

// If the command is being executed in an interactive terminal
// and hook are enabled, run the plugin hooks.
Expand Down
4 changes: 2 additions & 2 deletions cmd/docker/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down
4 changes: 1 addition & 3 deletions docs/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package main

import (
"context"
"log"
"os"

Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 1 addition & 3 deletions e2e/cli-plugins/plugins/nopersistentprerun/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"context"
"fmt"

"github.com/docker/cli/cli-plugins/manager"
Expand All @@ -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",
Expand Down
4 changes: 1 addition & 3 deletions e2e/cli-plugins/plugins/presocket/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"context"
"fmt"
"os"
"os/signal"
Expand All @@ -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",
Expand Down

0 comments on commit 0aad14d

Please sign in to comment.