mirror of https://github.com/docker/cli.git
Use Cobra built-in --version feature
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
parent
0acb2522bd
commit
a3fe7d62b8
|
@ -23,6 +23,7 @@ func SetupRootCommand(rootCmd *cobra.Command) {
|
||||||
rootCmd.SetHelpTemplate(helpTemplate)
|
rootCmd.SetHelpTemplate(helpTemplate)
|
||||||
rootCmd.SetFlagErrorFunc(FlagErrorFunc)
|
rootCmd.SetFlagErrorFunc(FlagErrorFunc)
|
||||||
rootCmd.SetHelpCommand(helpCommand)
|
rootCmd.SetHelpCommand(helpCommand)
|
||||||
|
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")
|
||||||
|
|
|
@ -4,7 +4,6 @@ package flags
|
||||||
type ClientOptions struct {
|
type ClientOptions struct {
|
||||||
Common *CommonOptions
|
Common *CommonOptions
|
||||||
ConfigDir string
|
ConfigDir string
|
||||||
Version bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewClientOptions returns a new ClientOptions
|
// NewClientOptions returns a new ClientOptions
|
||||||
|
|
|
@ -32,13 +32,6 @@ func newDockerCommand(dockerCli *command.DockerCli) *cobra.Command {
|
||||||
SilenceErrors: true,
|
SilenceErrors: true,
|
||||||
TraverseChildren: true,
|
TraverseChildren: true,
|
||||||
Args: noArgs,
|
Args: noArgs,
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
|
||||||
if opts.Version {
|
|
||||||
showVersion()
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return command.ShowHelp(dockerCli.Err())(cmd, args)
|
|
||||||
},
|
|
||||||
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
||||||
// flags must be the top-level command flags, not cmd.Flags()
|
// flags must be the top-level command flags, not cmd.Flags()
|
||||||
opts.Common.SetDefaultOptions(flags)
|
opts.Common.SetDefaultOptions(flags)
|
||||||
|
@ -48,11 +41,12 @@ func newDockerCommand(dockerCli *command.DockerCli) *cobra.Command {
|
||||||
}
|
}
|
||||||
return isSupported(cmd, dockerCli)
|
return isSupported(cmd, dockerCli)
|
||||||
},
|
},
|
||||||
|
Version: fmt.Sprintf("%s, build %s", cli.Version, cli.GitCommit),
|
||||||
}
|
}
|
||||||
cli.SetupRootCommand(cmd)
|
cli.SetupRootCommand(cmd)
|
||||||
|
|
||||||
flags = cmd.Flags()
|
flags = cmd.Flags()
|
||||||
flags.BoolVarP(&opts.Version, "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")
|
flags.StringVar(&opts.ConfigDir, "config", cliconfig.Dir(), "Location of client config files")
|
||||||
opts.Common.InstallFlags(flags)
|
opts.Common.InstallFlags(flags)
|
||||||
|
|
||||||
|
@ -186,10 +180,6 @@ func contentTrustEnabled() bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func showVersion() {
|
|
||||||
fmt.Printf("Docker version %s, build %s\n", cli.Version, cli.GitCommit)
|
|
||||||
}
|
|
||||||
|
|
||||||
func dockerPreRun(opts *cliflags.ClientOptions) {
|
func dockerPreRun(opts *cliflags.ClientOptions) {
|
||||||
cliflags.SetLogLevel(opts.Common.LogLevel)
|
cliflags.SetLogLevel(opts.Common.LogLevel)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue