Skip to content

Commit 88f44ec

Browse files
committed
cli: SetupRootCommand: remove redundant flags return
The flag-set that was returned is a pointer to the command's Flags(), which is in itself passed by reference (as it is modified / set up). This patch removes the flags return, to prevent assuming it's different than the command's flags. While SetupRootCommand is exported, a search showed that it's only used internally, so changing the signature should not be a problem. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent d2b376d commit 88f44ec

File tree

4 files changed

+12
-14
lines changed

4 files changed

+12
-14
lines changed

cli-plugins/plugin/plugin.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ func newPluginCommand(dockerCli *command.DockerCli, plugin *cobra.Command, meta
131131
DisableDescriptions: true,
132132
},
133133
}
134-
opts, flags := cli.SetupPluginRootCommand(cmd)
134+
opts, _ := cli.SetupPluginRootCommand(cmd)
135135

136136
cmd.SetIn(dockerCli.In())
137137
cmd.SetOut(dockerCli.Out())
@@ -144,7 +144,7 @@ func newPluginCommand(dockerCli *command.DockerCli, plugin *cobra.Command, meta
144144

145145
cli.DisableFlagsInUseLine(cmd)
146146

147-
return cli.NewTopLevelCommand(cmd, dockerCli, opts, flags)
147+
return cli.NewTopLevelCommand(cmd, dockerCli, opts, cmd.Flags())
148148
}
149149

150150
func newMetadataSubcommand(plugin *cobra.Command, meta manager.Metadata) *cobra.Command {

cli/cobra.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,9 @@ import (
2222

2323
// setupCommonRootCommand contains the setup common to
2424
// SetupRootCommand and SetupPluginRootCommand.
25-
func setupCommonRootCommand(rootCmd *cobra.Command) (*cliflags.ClientOptions, *pflag.FlagSet, *cobra.Command) {
26-
flags := rootCmd.Flags()
25+
func setupCommonRootCommand(rootCmd *cobra.Command) (*cliflags.ClientOptions, *cobra.Command) {
2726
opts := cliflags.NewClientOptions()
28-
opts.InstallFlags(flags)
27+
opts.InstallFlags(rootCmd.Flags())
2928

3029
cobra.AddTemplateFunc("add", func(a, b int) int { return a + b })
3130
cobra.AddTemplateFunc("hasAliases", hasAliases)
@@ -70,20 +69,20 @@ func setupCommonRootCommand(rootCmd *cobra.Command) (*cliflags.ClientOptions, *p
7069
}
7170
}
7271

73-
return opts, flags, helpCommand
72+
return opts, helpCommand
7473
}
7574

7675
// SetupRootCommand sets default usage, help, and error handling for the
7776
// root command.
78-
func SetupRootCommand(rootCmd *cobra.Command) (*cliflags.ClientOptions, *pflag.FlagSet, *cobra.Command) {
77+
func SetupRootCommand(rootCmd *cobra.Command) (opts *cliflags.ClientOptions, helpCmd *cobra.Command) {
7978
rootCmd.SetVersionTemplate("Docker version {{.Version}}\n")
8079
return setupCommonRootCommand(rootCmd)
8180
}
8281

8382
// SetupPluginRootCommand sets default usage, help and error handling for a plugin root command.
8483
func SetupPluginRootCommand(rootCmd *cobra.Command) (*cliflags.ClientOptions, *pflag.FlagSet) {
85-
opts, flags, _ := setupCommonRootCommand(rootCmd)
86-
return opts, flags
84+
opts, _ := setupCommonRootCommand(rootCmd)
85+
return opts, rootCmd.Flags()
8786
}
8887

8988
// FlagErrorFunc prints an error message which matches the format of the

cmd/docker/docker.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
func newDockerCommand(dockerCli *command.DockerCli) *cli.TopLevelCommand {
2525
var (
2626
opts *cliflags.ClientOptions
27-
flags *pflag.FlagSet
2827
helpCmd *cobra.Command
2928
)
3029

@@ -55,9 +54,9 @@ func newDockerCommand(dockerCli *command.DockerCli) *cli.TopLevelCommand {
5554
cmd.SetOut(dockerCli.Out())
5655
cmd.SetErr(dockerCli.Err())
5756

58-
opts, flags, helpCmd = cli.SetupRootCommand(cmd)
57+
opts, helpCmd = cli.SetupRootCommand(cmd)
5958
registerCompletionFuncForGlobalFlags(dockerCli, cmd)
60-
flags.BoolP("version", "v", false, "Print version information and quit")
59+
cmd.Flags().BoolP("version", "v", false, "Print version information and quit")
6160
setFlagErrorFunc(dockerCli, cmd)
6261

6362
setupHelpCommand(dockerCli, cmd, helpCmd)
@@ -70,7 +69,7 @@ func newDockerCommand(dockerCli *command.DockerCli) *cli.TopLevelCommand {
7069
setValidateArgs(dockerCli, cmd)
7170

7271
// flags must be the top-level command flags, not cmd.Flags()
73-
return cli.NewTopLevelCommand(cmd, dockerCli, opts, flags)
72+
return cli.NewTopLevelCommand(cmd, dockerCli, opts, cmd.Flags())
7473
}
7574

7675
func setFlagErrorFunc(dockerCli command.Cli, cmd *cobra.Command) {

docs/generate/generate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func gen(opts *options) error {
3737
Use: "docker [OPTIONS] COMMAND [ARG...]",
3838
Short: "The base command for the Docker CLI.",
3939
}
40-
clientOpts, _, _ := cli.SetupRootCommand(cmd)
40+
clientOpts, _ := cli.SetupRootCommand(cmd)
4141
if err := dockerCLI.Initialize(clientOpts); err != nil {
4242
return err
4343
}

0 commit comments

Comments
 (0)