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"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
cliconfig "github.com/docker/cli/cli/config"
|
||||||
|
cliflags "github.com/docker/cli/cli/flags"
|
||||||
"github.com/docker/docker/pkg/term"
|
"github.com/docker/docker/pkg/term"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/spf13/cobra"
|
"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("hasSubCommands", hasSubCommands)
|
||||||
cobra.AddTemplateFunc("hasManagementSubCommands", hasManagementSubCommands)
|
cobra.AddTemplateFunc("hasManagementSubCommands", hasManagementSubCommands)
|
||||||
cobra.AddTemplateFunc("operationSubCommands", operationSubCommands)
|
cobra.AddTemplateFunc("operationSubCommands", operationSubCommands)
|
||||||
|
@ -20,18 +29,22 @@ func setupCommonRootCommand(rootCmd *cobra.Command) {
|
||||||
rootCmd.SetHelpTemplate(helpTemplate)
|
rootCmd.SetHelpTemplate(helpTemplate)
|
||||||
rootCmd.SetFlagErrorFunc(FlagErrorFunc)
|
rootCmd.SetFlagErrorFunc(FlagErrorFunc)
|
||||||
rootCmd.SetHelpCommand(helpCommand)
|
rootCmd.SetHelpCommand(helpCommand)
|
||||||
|
|
||||||
|
return opts, flags
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetupRootCommand sets default usage, help, and error handling for the
|
// SetupRootCommand sets default usage, help, and error handling for the
|
||||||
// root command.
|
// root command.
|
||||||
func SetupRootCommand(rootCmd *cobra.Command) {
|
func SetupRootCommand(rootCmd *cobra.Command) (*cliflags.ClientOptions, *pflag.FlagSet) {
|
||||||
setupCommonRootCommand(rootCmd)
|
opts, flags := setupCommonRootCommand(rootCmd)
|
||||||
|
|
||||||
rootCmd.SetVersionTemplate("Docker version {{.Version}}\n")
|
rootCmd.SetVersionTemplate("Docker version {{.Version}}\n")
|
||||||
|
|
||||||
rootCmd.PersistentFlags().BoolP("help", "h", false, "Print usage")
|
rootCmd.PersistentFlags().BoolP("help", "h", false, "Print usage")
|
||||||
rootCmd.PersistentFlags().MarkShorthandDeprecated("help", "please use --help")
|
rootCmd.PersistentFlags().MarkShorthandDeprecated("help", "please use --help")
|
||||||
rootCmd.PersistentFlags().Lookup("help").Hidden = true
|
rootCmd.PersistentFlags().Lookup("help").Hidden = true
|
||||||
|
|
||||||
|
return opts, flags
|
||||||
}
|
}
|
||||||
|
|
||||||
// FlagErrorFunc prints an error message which matches the format of the
|
// 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"
|
||||||
"github.com/docker/cli/cli/command"
|
"github.com/docker/cli/cli/command"
|
||||||
"github.com/docker/cli/cli/command/commands"
|
"github.com/docker/cli/cli/command/commands"
|
||||||
cliconfig "github.com/docker/cli/cli/config"
|
|
||||||
cliflags "github.com/docker/cli/cli/flags"
|
cliflags "github.com/docker/cli/cli/flags"
|
||||||
"github.com/docker/docker/api/types/versions"
|
"github.com/docker/docker/api/types/versions"
|
||||||
"github.com/docker/docker/client"
|
"github.com/docker/docker/client"
|
||||||
|
@ -19,8 +18,10 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func newDockerCommand(dockerCli *command.DockerCli) *cobra.Command {
|
func newDockerCommand(dockerCli *command.DockerCli) *cobra.Command {
|
||||||
opts := cliflags.NewClientOptions()
|
var (
|
||||||
var flags *pflag.FlagSet
|
opts *cliflags.ClientOptions
|
||||||
|
flags *pflag.FlagSet
|
||||||
|
)
|
||||||
|
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Use: "docker [OPTIONS] COMMAND [ARG...]",
|
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),
|
Version: fmt.Sprintf("%s, build %s", cli.Version, cli.GitCommit),
|
||||||
DisableFlagsInUseLine: true,
|
DisableFlagsInUseLine: true,
|
||||||
}
|
}
|
||||||
cli.SetupRootCommand(cmd)
|
opts, flags = cli.SetupRootCommand(cmd)
|
||||||
|
|
||||||
flags = cmd.Flags()
|
|
||||||
flags.BoolP("version", "v", false, "Print version information and quit")
|
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)
|
setFlagErrorFunc(dockerCli, cmd, flags, opts)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue