Skip to content

Commit

Permalink
Merge pull request #1069 from thaJeztah/bump-cobra-pflags
Browse files Browse the repository at this point in the history
Update Cobra and pflag, and use built-in --version feature
  • Loading branch information
vieux committed May 21, 2018
2 parents 7bdd820 + 00d0802 commit 09115e3
Show file tree
Hide file tree
Showing 18 changed files with 632 additions and 148 deletions.
14 changes: 2 additions & 12 deletions cli/cobra.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ func SetupRootCommand(rootCmd *cobra.Command) {
cobra.AddTemplateFunc("operationSubCommands", operationSubCommands)
cobra.AddTemplateFunc("managementSubCommands", managementSubCommands)
cobra.AddTemplateFunc("wrappedFlagUsages", wrappedFlagUsages)
cobra.AddTemplateFunc("useLine", UseLine)

rootCmd.SetUsageTemplate(usageTemplate)
rootCmd.SetHelpTemplate(helpTemplate)
rootCmd.SetFlagErrorFunc(FlagErrorFunc)
rootCmd.SetHelpCommand(helpCommand)
rootCmd.SetVersionTemplate("Docker version {{.Version}}\n")

rootCmd.PersistentFlags().BoolP("help", "h", false, "Print usage")
rootCmd.PersistentFlags().MarkShorthandDeprecated("help", "please use --help")
Expand Down Expand Up @@ -99,19 +99,9 @@ func managementSubCommands(cmd *cobra.Command) []*cobra.Command {
return cmds
}

// UseLine returns the usage line for a command. This implementation is different
// from the default Command.UseLine in that it does not add a `[flags]` to the
// end of the line.
func UseLine(cmd *cobra.Command) string {
if cmd.HasParent() {
return cmd.Parent().CommandPath() + " " + cmd.Use
}
return cmd.Use
}

var usageTemplate = `Usage:
{{- if not .HasSubCommands}} {{ useLine . }}{{end}}
{{- if not .HasSubCommands}} {{.UseLine}}{{end}}
{{- if .HasSubCommands}} {{ .CommandPath}} COMMAND{{end}}
{{ .Short | trim }}
Expand Down
1 change: 0 additions & 1 deletion cli/flags/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package flags
type ClientOptions struct {
Common *CommonOptions
ConfigDir string
Version bool
}

// NewClientOptions returns a new ClientOptions
Expand Down
23 changes: 11 additions & 12 deletions cmd/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,6 @@ func newDockerCommand(dockerCli *command.DockerCli) *cobra.Command {
SilenceErrors: true,
TraverseChildren: true,
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 {
// flags must be the top-level command flags, not cmd.Flags()
opts.Common.SetDefaultOptions(flags)
Expand All @@ -48,11 +41,13 @@ func newDockerCommand(dockerCli *command.DockerCli) *cobra.Command {
}
return isSupported(cmd, dockerCli)
},
Version: fmt.Sprintf("%s, build %s", cli.Version, cli.GitCommit),
DisableFlagsInUseLine: true,
}
cli.SetupRootCommand(cmd)

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")
opts.Common.InstallFlags(flags)

Expand All @@ -63,11 +58,19 @@ func newDockerCommand(dockerCli *command.DockerCli) *cobra.Command {
cmd.SetOutput(dockerCli.Out())
commands.AddCommands(cmd, dockerCli)

disableFlagsInUseLine(cmd)
setValidateArgs(dockerCli, cmd, flags, opts)

return cmd
}

func disableFlagsInUseLine(cmd *cobra.Command) {
visitAll(cmd, func(ccmd *cobra.Command) {
// do not add a `[flags]` to the end of the usage line.
ccmd.DisableFlagsInUseLine = true
})
}

func setFlagErrorFunc(dockerCli *command.DockerCli, cmd *cobra.Command, flags *pflag.FlagSet, opts *cliflags.ClientOptions) {
// When invoking `docker stack --nonsense`, we need to make sure FlagErrorFunc return appropriate
// output if the feature is not supported.
Expand Down Expand Up @@ -186,10 +189,6 @@ func contentTrustEnabled() bool {
return false
}

func showVersion() {
fmt.Printf("Docker version %s, build %s\n", cli.Version, cli.GitCommit)
}

func dockerPreRun(opts *cliflags.ClientOptions) {
cliflags.SetLogLevel(opts.Common.LogLevel)

Expand Down
3 changes: 1 addition & 2 deletions docs/yaml/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"sort"
"strings"

"github.com/docker/cli/cli"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
yaml "gopkg.in/yaml.v2"
Expand Down Expand Up @@ -96,7 +95,7 @@ func GenYamlCustom(cmd *cobra.Command, w io.Writer) error {
}

if cmd.Runnable() {
cliDoc.Usage = cli.UseLine(cmd)
cliDoc.Usage = cmd.UseLine()
}

if len(cmd.Example) > 0 {
Expand Down
4 changes: 2 additions & 2 deletions vendor.conf
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ github.com/PuerkitoBio/urlesc 5bd2802263f21d8788851d5305584c82a5c75d7e
github.com/russross/blackfriday 1d6b8e9301e720b08a8938b8c25c018285885438
github.com/shurcooL/sanitized_anchor_name 10ef21a441db47d8b13ebcc5fd2310f636973c77
github.com/sirupsen/logrus v1.0.3
github.com/spf13/cobra 34ceca591bcf34a17a8b7bad5b3ce5f9c165bee5
github.com/spf13/pflag 97afa5e7ca8a08a383cb259e06636b5e2cc7897f
github.com/spf13/cobra v0.0.3
github.com/spf13/pflag v1.0.1
github.com/theupdateframework/notary v0.6.1
github.com/tonistiigi/fsutil dea3a0da73aee887fc02142d995be764106ac5e2
github.com/xeipuuv/gojsonpointer e0fe6f68307607d540ed8eac07a342c33fa1b54a
Expand Down
71 changes: 45 additions & 26 deletions vendor/github.com/spf13/cobra/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 09115e3

Please sign in to comment.