From 88f44ec15972cdcf9a3600eee974a1f34375e55f Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Wed, 28 Jun 2023 16:06:19 +0200 Subject: [PATCH] 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 --- cli-plugins/plugin/plugin.go | 4 ++-- cli/cobra.go | 13 ++++++------- cmd/docker/docker.go | 7 +++---- docs/generate/generate.go | 2 +- 4 files changed, 12 insertions(+), 14 deletions(-) diff --git a/cli-plugins/plugin/plugin.go b/cli-plugins/plugin/plugin.go index bdb5a64875..73059578f9 100644 --- a/cli-plugins/plugin/plugin.go +++ b/cli-plugins/plugin/plugin.go @@ -131,7 +131,7 @@ func newPluginCommand(dockerCli *command.DockerCli, plugin *cobra.Command, meta DisableDescriptions: true, }, } - opts, flags := cli.SetupPluginRootCommand(cmd) + opts, _ := cli.SetupPluginRootCommand(cmd) cmd.SetIn(dockerCli.In()) cmd.SetOut(dockerCli.Out()) @@ -144,7 +144,7 @@ func newPluginCommand(dockerCli *command.DockerCli, plugin *cobra.Command, meta cli.DisableFlagsInUseLine(cmd) - return cli.NewTopLevelCommand(cmd, dockerCli, opts, flags) + return cli.NewTopLevelCommand(cmd, dockerCli, opts, cmd.Flags()) } func newMetadataSubcommand(plugin *cobra.Command, meta manager.Metadata) *cobra.Command { diff --git a/cli/cobra.go b/cli/cobra.go index 178e820fd0..5ab1f0b2b8 100644 --- a/cli/cobra.go +++ b/cli/cobra.go @@ -22,10 +22,9 @@ import ( // setupCommonRootCommand contains the setup common to // SetupRootCommand and SetupPluginRootCommand. -func setupCommonRootCommand(rootCmd *cobra.Command) (*cliflags.ClientOptions, *pflag.FlagSet, *cobra.Command) { - flags := rootCmd.Flags() +func setupCommonRootCommand(rootCmd *cobra.Command) (*cliflags.ClientOptions, *cobra.Command) { opts := cliflags.NewClientOptions() - opts.InstallFlags(flags) + opts.InstallFlags(rootCmd.Flags()) cobra.AddTemplateFunc("add", func(a, b int) int { return a + b }) cobra.AddTemplateFunc("hasAliases", hasAliases) @@ -70,20 +69,20 @@ func setupCommonRootCommand(rootCmd *cobra.Command) (*cliflags.ClientOptions, *p } } - return opts, flags, helpCommand + return opts, helpCommand } // SetupRootCommand sets default usage, help, and error handling for the // root command. -func SetupRootCommand(rootCmd *cobra.Command) (*cliflags.ClientOptions, *pflag.FlagSet, *cobra.Command) { +func SetupRootCommand(rootCmd *cobra.Command) (opts *cliflags.ClientOptions, helpCmd *cobra.Command) { rootCmd.SetVersionTemplate("Docker version {{.Version}}\n") return setupCommonRootCommand(rootCmd) } // SetupPluginRootCommand sets default usage, help and error handling for a plugin root command. func SetupPluginRootCommand(rootCmd *cobra.Command) (*cliflags.ClientOptions, *pflag.FlagSet) { - opts, flags, _ := setupCommonRootCommand(rootCmd) - return opts, flags + opts, _ := setupCommonRootCommand(rootCmd) + return opts, rootCmd.Flags() } // FlagErrorFunc prints an error message which matches the format of the diff --git a/cmd/docker/docker.go b/cmd/docker/docker.go index d18125a634..30c48b40d1 100644 --- a/cmd/docker/docker.go +++ b/cmd/docker/docker.go @@ -24,7 +24,6 @@ import ( func newDockerCommand(dockerCli *command.DockerCli) *cli.TopLevelCommand { var ( opts *cliflags.ClientOptions - flags *pflag.FlagSet helpCmd *cobra.Command ) @@ -55,9 +54,9 @@ func newDockerCommand(dockerCli *command.DockerCli) *cli.TopLevelCommand { cmd.SetOut(dockerCli.Out()) cmd.SetErr(dockerCli.Err()) - opts, flags, helpCmd = cli.SetupRootCommand(cmd) + opts, helpCmd = cli.SetupRootCommand(cmd) registerCompletionFuncForGlobalFlags(dockerCli, cmd) - flags.BoolP("version", "v", false, "Print version information and quit") + cmd.Flags().BoolP("version", "v", false, "Print version information and quit") setFlagErrorFunc(dockerCli, cmd) setupHelpCommand(dockerCli, cmd, helpCmd) @@ -70,7 +69,7 @@ func newDockerCommand(dockerCli *command.DockerCli) *cli.TopLevelCommand { setValidateArgs(dockerCli, cmd) // flags must be the top-level command flags, not cmd.Flags() - return cli.NewTopLevelCommand(cmd, dockerCli, opts, flags) + return cli.NewTopLevelCommand(cmd, dockerCli, opts, cmd.Flags()) } func setFlagErrorFunc(dockerCli command.Cli, cmd *cobra.Command) { diff --git a/docs/generate/generate.go b/docs/generate/generate.go index c9187c2c0d..a24aba1813 100644 --- a/docs/generate/generate.go +++ b/docs/generate/generate.go @@ -37,7 +37,7 @@ func gen(opts *options) error { Use: "docker [OPTIONS] COMMAND [ARG...]", Short: "The base command for the Docker CLI.", } - clientOpts, _, _ := cli.SetupRootCommand(cmd) + clientOpts, _ := cli.SetupRootCommand(cmd) if err := dockerCLI.Initialize(clientOpts); err != nil { return err }