Skip to content

Commit eaa6886

Browse files
committed
cli: SetupRootCommand, SetupPluginRootCommand: set context on root-commands
Add a context-argument to; - cli.SetupRootCommand - cli.SetupPluginRootCommand - cli-plugins/plugin.RunPlugin - cli-plugins/plugin.Run Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent bcbfcdb commit eaa6886

8 files changed

Lines changed: 44 additions & 36 deletions

File tree

cli-plugins/examples/helloworld/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import (
1212
)
1313

1414
func main() {
15-
plugin.Run(func(dockerCli command.Cli) *cobra.Command {
15+
ctx := context.Background()
16+
plugin.Run(ctx, func(dockerCli command.Cli) *cobra.Command {
1617
goodbye := &cobra.Command{
1718
Use: "goodbye",
1819
Short: "Say Goodbye instead of Hello",

cli-plugins/plugin/plugin.go

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -66,21 +66,15 @@ func closeOnCLISocketClose(cancel func()) {
6666
}
6767

6868
// RunPlugin executes the specified plugin command
69-
func RunPlugin(dockerCli *command.DockerCli, plugin *cobra.Command, meta manager.Metadata) error {
70-
tcmd := newPluginCommand(dockerCli, plugin, meta)
69+
func RunPlugin(ctx context.Context, dockerCli *command.DockerCli, plugin *cobra.Command, meta manager.Metadata) error {
70+
tcmd := newPluginCommand(ctx, dockerCli, plugin, meta)
7171

7272
var persistentPreRunOnce sync.Once
7373
PersistentPreRunE = func(cmd *cobra.Command, _ []string) error {
7474
var err error
7575
persistentPreRunOnce.Do(func() {
76-
cmdContext := cmd.Context()
77-
// TODO: revisit and make sure this check makes sense
78-
// see: https://github.com/docker/cli/pull/4599#discussion_r1422487271
79-
if cmdContext == nil {
80-
cmdContext = context.TODO()
81-
}
82-
ctx, cancel := context.WithCancel(cmdContext)
83-
cmd.SetContext(ctx)
76+
ctx2, cancel := context.WithCancel(cmd.Context())
77+
cmd.SetContext(ctx2)
8478
closeOnCLISocketClose(cancel)
8579

8680
var opts []command.CLIOption
@@ -103,16 +97,16 @@ func RunPlugin(dockerCli *command.DockerCli, plugin *cobra.Command, meta manager
10397
}
10498

10599
// Run is the top-level entry point to the CLI plugin framework. It should be called from your plugin's `main()` function.
106-
func Run(makeCmd func(command.Cli) *cobra.Command, meta manager.Metadata) {
107-
dockerCli, err := command.NewDockerCli()
100+
func Run(ctx context.Context, makeCmd func(command.Cli) *cobra.Command, meta manager.Metadata) {
101+
dockerCli, err := command.NewDockerCli(command.WithBaseContext(ctx))
108102
if err != nil {
109103
fmt.Fprintln(os.Stderr, err)
110104
os.Exit(1)
111105
}
112106

113107
plugin := makeCmd(dockerCli)
114108

115-
if err := RunPlugin(dockerCli, plugin, meta); err != nil {
109+
if err := RunPlugin(ctx, dockerCli, plugin, meta); err != nil {
116110
if sterr, ok := err.(cli.StatusError); ok {
117111
if sterr.Status != "" {
118112
fmt.Fprintln(dockerCli.Err(), sterr.Status)
@@ -161,7 +155,7 @@ func withPluginClientConn(name string) command.CLIOption {
161155
})
162156
}
163157

164-
func newPluginCommand(dockerCli *command.DockerCli, plugin *cobra.Command, meta manager.Metadata) *cli.TopLevelCommand {
158+
func newPluginCommand(ctx context.Context, dockerCli *command.DockerCli, plugin *cobra.Command, meta manager.Metadata) *cli.TopLevelCommand {
165159
name := plugin.Name()
166160
fullname := manager.NamePrefix + name
167161

@@ -182,7 +176,7 @@ func newPluginCommand(dockerCli *command.DockerCli, plugin *cobra.Command, meta
182176
DisableDescriptions: true,
183177
},
184178
}
185-
opts, _ := cli.SetupPluginRootCommand(cmd)
179+
opts, _ := cli.SetupPluginRootCommand(ctx, cmd)
186180

187181
cmd.SetIn(dockerCli.In())
188182
cmd.SetOut(dockerCli.Out())

cli/cobra.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cli
22

33
import (
4+
"context"
45
"fmt"
56
"os"
67
"path/filepath"
@@ -22,7 +23,8 @@ import (
2223

2324
// setupCommonRootCommand contains the setup common to
2425
// SetupRootCommand and SetupPluginRootCommand.
25-
func setupCommonRootCommand(rootCmd *cobra.Command) (*cliflags.ClientOptions, *cobra.Command) {
26+
func setupCommonRootCommand(ctx context.Context, rootCmd *cobra.Command) (*cliflags.ClientOptions, *cobra.Command) {
27+
rootCmd.SetContext(ctx)
2628
opts := cliflags.NewClientOptions()
2729
opts.InstallFlags(rootCmd.Flags())
2830

@@ -74,14 +76,14 @@ func setupCommonRootCommand(rootCmd *cobra.Command) (*cliflags.ClientOptions, *c
7476

7577
// SetupRootCommand sets default usage, help, and error handling for the
7678
// root command.
77-
func SetupRootCommand(rootCmd *cobra.Command) (opts *cliflags.ClientOptions, helpCmd *cobra.Command) {
79+
func SetupRootCommand(ctx context.Context, rootCmd *cobra.Command) (opts *cliflags.ClientOptions, helpCmd *cobra.Command) {
7880
rootCmd.SetVersionTemplate("Docker version {{.Version}}\n")
79-
return setupCommonRootCommand(rootCmd)
81+
return setupCommonRootCommand(ctx, rootCmd)
8082
}
8183

8284
// SetupPluginRootCommand sets default usage, help and error handling for a plugin root command.
83-
func SetupPluginRootCommand(rootCmd *cobra.Command) (*cliflags.ClientOptions, *pflag.FlagSet) {
84-
opts, _ := setupCommonRootCommand(rootCmd)
85+
func SetupPluginRootCommand(ctx context.Context, rootCmd *cobra.Command) (*cliflags.ClientOptions, *pflag.FlagSet) {
86+
opts, _ := setupCommonRootCommand(ctx, rootCmd)
8587
return opts, rootCmd.Flags()
8688
}
8789

cmd/docker/builder_test.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ echo '{"SchemaVersion":"0.1.0","Vendor":"Docker Inc.","Version":"v0.6.3","ShortD
9898
dockerCli.ConfigFile().Aliases = map[string]string{"builder": "buildx"}
9999
}
100100

101-
tcmd := newDockerCommand(dockerCli)
101+
ctx := context.TODO()
102+
tcmd := newDockerCommand(ctx, dockerCli)
102103
tcmd.SetArgs([]string{"build", "."})
103104

104105
cmd, args, err := tcmd.HandleGlobalFlags()
@@ -144,7 +145,8 @@ func TestBuildkitDisabled(t *testing.T) {
144145
assert.NilError(t, dockerCli.Initialize(flags.NewClientOptions()))
145146
dockerCli.ConfigFile().CLIPluginsExtraDirs = []string{dir.Path()}
146147

147-
tcmd := newDockerCommand(dockerCli)
148+
ctx := context.TODO()
149+
tcmd := newDockerCommand(ctx, dockerCli)
148150
tcmd.SetArgs([]string{"build", "."})
149151

150152
cmd, args, err := tcmd.HandleGlobalFlags()
@@ -179,7 +181,8 @@ func TestBuilderBroken(t *testing.T) {
179181
assert.NilError(t, dockerCli.Initialize(flags.NewClientOptions()))
180182
dockerCli.ConfigFile().CLIPluginsExtraDirs = []string{dir.Path()}
181183

182-
tcmd := newDockerCommand(dockerCli)
184+
ctx := context.TODO()
185+
tcmd := newDockerCommand(ctx, dockerCli)
183186
tcmd.SetArgs([]string{"build", "."})
184187

185188
cmd, args, err := tcmd.HandleGlobalFlags()
@@ -216,7 +219,8 @@ func TestBuilderBrokenEnforced(t *testing.T) {
216219
assert.NilError(t, dockerCli.Initialize(flags.NewClientOptions()))
217220
dockerCli.ConfigFile().CLIPluginsExtraDirs = []string{dir.Path()}
218221

219-
tcmd := newDockerCommand(dockerCli)
222+
ctx := context.TODO()
223+
tcmd := newDockerCommand(ctx, dockerCli)
220224
tcmd.SetArgs([]string{"build", "."})
221225

222226
cmd, args, err := tcmd.HandleGlobalFlags()

cmd/docker/docker.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func main() {
3636
}
3737
logrus.SetOutput(dockerCli.Err())
3838

39-
if err := runDocker(dockerCli); err != nil {
39+
if err := runDocker(ctx, dockerCli); err != nil {
4040
if sterr, ok := err.(cli.StatusError); ok {
4141
if sterr.Status != "" {
4242
fmt.Fprintln(dockerCli.Err(), sterr.Status)
@@ -53,7 +53,7 @@ func main() {
5353
}
5454
}
5555

56-
func newDockerCommand(dockerCli *command.DockerCli) *cli.TopLevelCommand {
56+
func newDockerCommand(ctx context.Context, dockerCli *command.DockerCli) *cli.TopLevelCommand {
5757
var (
5858
opts *cliflags.ClientOptions
5959
helpCmd *cobra.Command
@@ -86,7 +86,7 @@ func newDockerCommand(dockerCli *command.DockerCli) *cli.TopLevelCommand {
8686
cmd.SetOut(dockerCli.Out())
8787
cmd.SetErr(dockerCli.Err())
8888

89-
opts, helpCmd = cli.SetupRootCommand(cmd)
89+
opts, helpCmd = cli.SetupRootCommand(ctx, cmd)
9090
registerCompletionFuncForGlobalFlags(dockerCli, cmd)
9191
cmd.Flags().BoolP("version", "v", false, "Print version information and quit")
9292
setFlagErrorFunc(dockerCli, cmd)
@@ -290,8 +290,8 @@ func tryPluginRun(dockerCli command.Cli, cmd *cobra.Command, subcommand string,
290290
return nil
291291
}
292292

293-
func runDocker(dockerCli *command.DockerCli) error {
294-
tcmd := newDockerCommand(dockerCli)
293+
func runDocker(ctx context.Context, dockerCli *command.DockerCli) error {
294+
tcmd := newDockerCommand(ctx, dockerCli)
295295

296296
cmd, args, err := tcmd.HandleGlobalFlags()
297297
if err != nil {

cmd/docker/docker_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"bytes"
5+
"context"
56
"io"
67
"os"
78
"testing"
@@ -18,7 +19,8 @@ func TestClientDebugEnabled(t *testing.T) {
1819

1920
cli, err := command.NewDockerCli()
2021
assert.NilError(t, err)
21-
tcmd := newDockerCommand(cli)
22+
ctx := context.TODO()
23+
tcmd := newDockerCommand(ctx, cli)
2224
tcmd.SetFlag("debug", "true")
2325
cmd, _, err := tcmd.HandleGlobalFlags()
2426
assert.NilError(t, err)
@@ -41,7 +43,8 @@ func runCliCommand(t *testing.T, r io.ReadCloser, w io.Writer, args ...string) e
4143
}
4244
cli, err := command.NewDockerCli(command.WithInputStream(r), command.WithCombinedStreams(w))
4345
assert.NilError(t, err)
44-
tcmd := newDockerCommand(cli)
46+
ctx := context.TODO()
47+
tcmd := newDockerCommand(ctx, cli)
4548

4649
tcmd.SetArgs(args)
4750
cmd, _, err := tcmd.HandleGlobalFlags()

docs/generate/generate.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package main
77

88
import (
9+
"context"
910
"log"
1011
"os"
1112

@@ -37,7 +38,9 @@ func gen(opts *options) error {
3738
Use: "docker [OPTIONS] COMMAND [ARG...]",
3839
Short: "The base command for the Docker CLI.",
3940
}
40-
clientOpts, _ := cli.SetupRootCommand(cmd)
41+
42+
ctx := context.Background()
43+
clientOpts, _ := cli.SetupRootCommand(ctx, cmd)
4144
if err := dockerCLI.Initialize(clientOpts); err != nil {
4245
return err
4346
}

e2e/cli-plugins/plugins/nopersistentprerun/main.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"context"
45
"fmt"
56

67
"github.com/docker/cli/cli-plugins/manager"
@@ -10,8 +11,9 @@ import (
1011
)
1112

1213
func main() {
13-
plugin.Run(func(dockerCli command.Cli) *cobra.Command {
14-
cmd := &cobra.Command{
14+
ctx := context.Background()
15+
plugin.Run(ctx, func(dockerCli command.Cli) *cobra.Command {
16+
return &cobra.Command{
1517
Use: "nopersistentprerun",
1618
Short: "Testing without PersistentPreRun hooks",
1719
// PersistentPreRunE: Not specified, we need to test that it works in the absence of an explicit call
@@ -25,7 +27,6 @@ func main() {
2527
return nil
2628
},
2729
}
28-
return cmd
2930
},
3031
manager.Metadata{
3132
SchemaVersion: "0.1.0",

0 commit comments

Comments
 (0)