mirror of https://github.com/docker/cli.git
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 <ijc@docker.com>
This commit is contained in:
parent
38645ca44a
commit
c5168117af
19
cli/cobra.go
19
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
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
Loading…
Reference in New Issue