Use Cobra built-in --version feature

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2018-05-19 03:15:08 +02:00
parent 9411856536
commit 0c3192da8c
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C
3 changed files with 3 additions and 10 deletions

View file

@ -22,6 +22,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")

View file

@ -24,19 +24,16 @@ func newDaemonCommand() *cobra.Command {
SilenceErrors: true, SilenceErrors: true,
Args: cli.NoArgs, Args: cli.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
if opts.version {
showVersion()
return nil
}
opts.flags = cmd.Flags() opts.flags = cmd.Flags()
return runDaemon(opts) return runDaemon(opts)
}, },
DisableFlagsInUseLine: true, DisableFlagsInUseLine: true,
Version: fmt.Sprintf("%s, build %s", dockerversion.Version, dockerversion.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.configFile, "config-file", defaultDaemonConfigFile, "Daemon configuration file") flags.StringVar(&opts.configFile, "config-file", defaultDaemonConfigFile, "Daemon configuration file")
opts.InstallFlags(flags) opts.InstallFlags(flags)
installConfigFlags(opts.daemonConfig, flags) installConfigFlags(opts.daemonConfig, flags)
@ -45,10 +42,6 @@ func newDaemonCommand() *cobra.Command {
return cmd return cmd
} }
func showVersion() {
fmt.Printf("Docker version %s, build %s\n", dockerversion.Version, dockerversion.GitCommit)
}
func main() { func main() {
if reexec.Init() { if reexec.Init() {
return return

View file

@ -30,7 +30,6 @@ var (
) )
type daemonOptions struct { type daemonOptions struct {
version bool
configFile string configFile string
daemonConfig *config.Config daemonConfig *config.Config
flags *pflag.FlagSet flags *pflag.FlagSet