From ed3db9e52f81e0bf3c8ee053fc0c4b248f47d9b6 Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Tue, 31 May 2016 22:19:13 -0700 Subject: [PATCH] Convert 'docker create' to use cobra and pflag Return the correct status code on flag parsins errors. Signed-off-by: Daniel Nephin Upstream-commit: 69d30376353f1b86eed94135f47fb625be7182a5 Component: cli --- components/cli/cobraadaptor/adaptor.go | 19 ++----------------- components/cli/flagerrors.go | 21 +++++++++++++++++++++ components/cli/usage.go | 1 - 3 files changed, 23 insertions(+), 18 deletions(-) create mode 100644 components/cli/flagerrors.go diff --git a/components/cli/cobraadaptor/adaptor.go b/components/cli/cobraadaptor/adaptor.go index 7e6327ac2d..719eaf4e1d 100644 --- a/components/cli/cobraadaptor/adaptor.go +++ b/components/cli/cobraadaptor/adaptor.go @@ -1,8 +1,6 @@ package cobraadaptor import ( - "fmt" - "github.com/docker/docker/api/client" "github.com/docker/docker/api/client/container" "github.com/docker/docker/api/client/image" @@ -32,9 +30,10 @@ func NewCobraAdaptor(clientFlags *cliflags.ClientFlags) CobraAdaptor { } rootCmd.SetUsageTemplate(usageTemplate) rootCmd.SetHelpTemplate(helpTemplate) - rootCmd.SetFlagErrorFunc(flagErrorFunc) + rootCmd.SetFlagErrorFunc(cli.FlagErrorFunc) rootCmd.SetOutput(stdout) rootCmd.AddCommand( + container.NewCreateCommand(dockerCli), container.NewRunCommand(dockerCli), image.NewSearchCommand(dockerCli), volume.NewVolumeCommand(dockerCli), @@ -78,20 +77,6 @@ func (c CobraAdaptor) Command(name string) func(...string) error { return nil } -// flagErrorFunc prints an error messages which matches the format of the -// docker/docker/cli error messages -func flagErrorFunc(cmd *cobra.Command, err error) error { - if err == nil { - return err - } - - usage := "" - if cmd.HasSubCommands() { - usage = "\n\n" + cmd.UsageString() - } - return fmt.Errorf("%s\nSee '%s --help'.%s", err, cmd.CommandPath(), usage) -} - var usageTemplate = `Usage: {{if not .HasSubCommands}}{{if .HasLocalFlags}}{{appendIfNotPresent .UseLine "[OPTIONS]"}}{{else}}{{.UseLine}}{{end}}{{end}}{{if .HasSubCommands}}{{ .CommandPath}} COMMAND{{end}} {{with or .Long .Short }}{{. | trim}}{{end}}{{if gt .Aliases 0}} diff --git a/components/cli/flagerrors.go b/components/cli/flagerrors.go new file mode 100644 index 0000000000..aab8a98845 --- /dev/null +++ b/components/cli/flagerrors.go @@ -0,0 +1,21 @@ +package cli + +import ( + "fmt" + + "github.com/spf13/cobra" +) + +// FlagErrorFunc prints an error messages which matches the format of the +// docker/docker/cli error messages +func FlagErrorFunc(cmd *cobra.Command, err error) error { + if err == nil { + return err + } + + usage := "" + if cmd.HasSubCommands() { + usage = "\n\n" + cmd.UsageString() + } + return fmt.Errorf("%s\nSee '%s --help'.%s", err, cmd.CommandPath(), usage) +} diff --git a/components/cli/usage.go b/components/cli/usage.go index 324d1d92bf..d6c97c32f5 100644 --- a/components/cli/usage.go +++ b/components/cli/usage.go @@ -12,7 +12,6 @@ var DockerCommandUsage = []Command{ {"build", "Build an image from a Dockerfile"}, {"commit", "Create a new image from a container's changes"}, {"cp", "Copy files/folders between a container and the local filesystem"}, - {"create", "Create a new container"}, {"diff", "Inspect changes on a container's filesystem"}, {"events", "Get real time events from the server"}, {"exec", "Run a command in a running container"},