From c5168117af97d951013b2baf0948157fb59faf7e Mon Sep 17 00:00:00 2001 From: Ian Campbell Date: Tue, 18 Dec 2018 10:16:52 +0000 Subject: [PATCH] Push setup of opts and default flagset into SetupRootCommand I'm shortly going to add a second user (plugins) which want to share some behaviour. Signed-off-by: Ian Campbell --- cli/cobra.go | 19 ++++++++++++++++--- cmd/docker/docker.go | 13 +++++-------- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/cli/cobra.go b/cli/cobra.go index 1e83a0ef4e..ce4284a0fd 100644 --- a/cli/cobra.go +++ b/cli/cobra.go @@ -4,12 +4,21 @@ import ( "fmt" "strings" + cliconfig "github.com/docker/cli/cli/config" + cliflags "github.com/docker/cli/cli/flags" "github.com/docker/docker/pkg/term" "github.com/pkg/errors" "github.com/spf13/cobra" + "github.com/spf13/pflag" ) -func setupCommonRootCommand(rootCmd *cobra.Command) { +func setupCommonRootCommand(rootCmd *cobra.Command) (*cliflags.ClientOptions, *pflag.FlagSet) { + opts := cliflags.NewClientOptions() + flags := rootCmd.Flags() + + flags.StringVar(&opts.ConfigDir, "config", cliconfig.Dir(), "Location of client config files") + opts.Common.InstallFlags(flags) + cobra.AddTemplateFunc("hasSubCommands", hasSubCommands) cobra.AddTemplateFunc("hasManagementSubCommands", hasManagementSubCommands) cobra.AddTemplateFunc("operationSubCommands", operationSubCommands) @@ -20,18 +29,22 @@ func setupCommonRootCommand(rootCmd *cobra.Command) { rootCmd.SetHelpTemplate(helpTemplate) rootCmd.SetFlagErrorFunc(FlagErrorFunc) rootCmd.SetHelpCommand(helpCommand) + + return opts, flags } // SetupRootCommand sets default usage, help, and error handling for the // root command. -func SetupRootCommand(rootCmd *cobra.Command) { - setupCommonRootCommand(rootCmd) +func SetupRootCommand(rootCmd *cobra.Command) (*cliflags.ClientOptions, *pflag.FlagSet) { + opts, flags := setupCommonRootCommand(rootCmd) rootCmd.SetVersionTemplate("Docker version {{.Version}}\n") rootCmd.PersistentFlags().BoolP("help", "h", false, "Print usage") rootCmd.PersistentFlags().MarkShorthandDeprecated("help", "please use --help") rootCmd.PersistentFlags().Lookup("help").Hidden = true + + return opts, 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 69d773aa2e..e64fe2afd8 100644 --- a/cmd/docker/docker.go +++ b/cmd/docker/docker.go @@ -9,7 +9,6 @@ import ( "github.com/docker/cli/cli" "github.com/docker/cli/cli/command" "github.com/docker/cli/cli/command/commands" - cliconfig "github.com/docker/cli/cli/config" cliflags "github.com/docker/cli/cli/flags" "github.com/docker/docker/api/types/versions" "github.com/docker/docker/client" @@ -19,8 +18,10 @@ import ( ) func newDockerCommand(dockerCli *command.DockerCli) *cobra.Command { - opts := cliflags.NewClientOptions() - var flags *pflag.FlagSet + var ( + opts *cliflags.ClientOptions + flags *pflag.FlagSet + ) cmd := &cobra.Command{ Use: "docker [OPTIONS] COMMAND [ARG...]", @@ -43,12 +44,8 @@ func newDockerCommand(dockerCli *command.DockerCli) *cobra.Command { Version: fmt.Sprintf("%s, build %s", cli.Version, cli.GitCommit), DisableFlagsInUseLine: true, } - cli.SetupRootCommand(cmd) - - flags = cmd.Flags() + opts, flags = cli.SetupRootCommand(cmd) flags.BoolP("version", "v", false, "Print version information and quit") - flags.StringVar(&opts.ConfigDir, "config", cliconfig.Dir(), "Location of client config files") - opts.Common.InstallFlags(flags) setFlagErrorFunc(dockerCli, cmd, flags, opts)