Skip to content

Commit a4f985c

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 c3dfbd5 commit a4f985c

File tree

9 files changed

+47
-37
lines changed

9 files changed

+47
-37
lines changed

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
@@ -27,21 +27,15 @@ import (
2727
var PersistentPreRunE func(*cobra.Command, []string) error
2828

2929
// RunPlugin executes the specified plugin command
30-
func RunPlugin(dockerCli *command.DockerCli, plugin *cobra.Command, meta manager.Metadata) error {
31-
tcmd := newPluginCommand(dockerCli, plugin, meta)
30+
func RunPlugin(ctx context.Context, dockerCli *command.DockerCli, plugin *cobra.Command, meta manager.Metadata) error {
31+
tcmd := newPluginCommand(ctx, dockerCli, plugin, meta)
3232

3333
var persistentPreRunOnce sync.Once
3434
PersistentPreRunE = func(cmd *cobra.Command, _ []string) error {
3535
var err error
3636
persistentPreRunOnce.Do(func() {
37-
cmdContext := cmd.Context()
38-
// TODO: revisit and make sure this check makes sense
39-
// see: https://github.com/docker/cli/pull/4599#discussion_r1422487271
40-
if cmdContext == nil {
41-
cmdContext = context.TODO()
42-
}
43-
ctx, cancel := context.WithCancel(cmdContext)
44-
cmd.SetContext(ctx)
37+
ctx2, cancel := context.WithCancel(cmd.Context())
38+
cmd.SetContext(ctx2)
4539
// Set up the context to cancel based on signalling via CLI socket.
4640
socket.ConnectAndWait(cancel)
4741

@@ -65,16 +59,16 @@ func RunPlugin(dockerCli *command.DockerCli, plugin *cobra.Command, meta manager
6559
}
6660

6761
// Run is the top-level entry point to the CLI plugin framework. It should be called from your plugin's `main()` function.
68-
func Run(makeCmd func(command.Cli) *cobra.Command, meta manager.Metadata) {
69-
dockerCli, err := command.NewDockerCli()
62+
func Run(ctx context.Context, makeCmd func(command.Cli) *cobra.Command, meta manager.Metadata) {
63+
dockerCli, err := command.NewDockerCli(command.WithBaseContext(ctx))
7064
if err != nil {
7165
fmt.Fprintln(os.Stderr, err)
7266
os.Exit(1)
7367
}
7468

7569
plugin := makeCmd(dockerCli)
7670

77-
if err := RunPlugin(dockerCli, plugin, meta); err != nil {
71+
if err := RunPlugin(ctx, dockerCli, plugin, meta); err != nil {
7872
if sterr, ok := err.(cli.StatusError); ok {
7973
if sterr.Status != "" {
8074
fmt.Fprintln(dockerCli.Err(), sterr.Status)
@@ -123,7 +117,7 @@ func withPluginClientConn(name string) command.CLIOption {
123117
})
124118
}
125119

126-
func newPluginCommand(dockerCli *command.DockerCli, plugin *cobra.Command, meta manager.Metadata) *cli.TopLevelCommand {
120+
func newPluginCommand(ctx context.Context, dockerCli *command.DockerCli, plugin *cobra.Command, meta manager.Metadata) *cli.TopLevelCommand {
127121
name := plugin.Name()
128122
fullname := manager.NamePrefix + name
129123

@@ -144,7 +138,7 @@ func newPluginCommand(dockerCli *command.DockerCli, plugin *cobra.Command, meta
144138
DisableDescriptions: true,
145139
},
146140
}
147-
opts, _ := cli.SetupPluginRootCommand(cmd)
141+
opts, _ := cli.SetupPluginRootCommand(ctx, cmd)
148142

149143
cmd.SetIn(dockerCli.In())
150144
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
@@ -35,7 +35,7 @@ func main() {
3535
}
3636
logrus.SetOutput(dockerCli.Err())
3737

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

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

88-
opts, helpCmd = cli.SetupRootCommand(cmd)
88+
opts, helpCmd = cli.SetupRootCommand(ctx, cmd)
8989
_ = registerCompletionFuncForGlobalFlags(dockerCli.ContextStore(), cmd)
9090
cmd.Flags().BoolP("version", "v", false, "Print version information and quit")
9191
setFlagErrorFunc(dockerCli, cmd)
@@ -281,8 +281,8 @@ func tryPluginRun(dockerCli command.Cli, cmd *cobra.Command, subcommand string,
281281
return nil
282282
}
283283

284-
func runDocker(dockerCli *command.DockerCli) error {
285-
tcmd := newDockerCommand(dockerCli)
284+
func runDocker(ctx context.Context, dockerCli *command.DockerCli) error {
285+
tcmd := newDockerCommand(ctx, dockerCli)
286286

287287
cmd, args, err := tcmd.HandleGlobalFlags()
288288
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",

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

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

33
import (
4+
"context"
45
"fmt"
56
"os"
67
"os/signal"
@@ -14,7 +15,8 @@ import (
1415
)
1516

1617
func main() {
17-
plugin.Run(RootCmd, manager.Metadata{
18+
ctx := context.Background()
19+
plugin.Run(ctx, RootCmd, manager.Metadata{
1820
SchemaVersion: "0.1.0",
1921
Vendor: "Docker Inc.",
2022
Version: "test",

0 commit comments

Comments
 (0)