Merge pull request #23253 from vdemeester/carry-pr-23159

Carry #23159 : Use spf13/cobra for `docker run` and `docker create`
This commit is contained in:
Sebastiaan van Stijn 2016-06-04 18:55:23 +02:00
commit 8ad0a0ff95
3 changed files with 27 additions and 21 deletions

View File

@ -1,9 +1,8 @@
package cobraadaptor
import (
"fmt"
"github.com/docker/docker/api/client"
"github.com/docker/docker/api/client/container"
"github.com/docker/docker/api/client/image"
"github.com/docker/docker/api/client/volume"
"github.com/docker/docker/cli"
@ -31,11 +30,13 @@ func NewCobraAdaptor(clientFlags *cliflags.ClientFlags) CobraAdaptor {
}
rootCmd.SetUsageTemplate(usageTemplate)
rootCmd.SetHelpTemplate(helpTemplate)
rootCmd.SetFlagErrorFunc(flagErrorFunc)
rootCmd.SetFlagErrorFunc(cli.FlagErrorFunc)
rootCmd.SetOutput(stdout)
rootCmd.AddCommand(
volume.NewVolumeCommand(dockerCli),
container.NewCreateCommand(dockerCli),
container.NewRunCommand(dockerCli),
image.NewSearchCommand(dockerCli),
volume.NewVolumeCommand(dockerCli),
)
rootCmd.PersistentFlags().BoolP("help", "h", false, "Print usage")
@ -52,7 +53,7 @@ func NewCobraAdaptor(clientFlags *cliflags.ClientFlags) CobraAdaptor {
func (c CobraAdaptor) Usage() []cli.Command {
cmds := []cli.Command{}
for _, cmd := range c.rootCmd.Commands() {
cmds = append(cmds, cli.Command{Name: cmd.Use, Description: cmd.Short})
cmds = append(cmds, cli.Command{Name: cmd.Name(), Description: cmd.Short})
}
return cmds
}
@ -76,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}}

21
flagerrors.go Normal file
View File

@ -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)
}

View File

@ -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"},
@ -37,7 +36,6 @@ var DockerCommandUsage = []Command{
{"restart", "Restart a container"},
{"rm", "Remove one or more containers"},
{"rmi", "Remove one or more images"},
{"run", "Run a command in a new container"},
{"save", "Save one or more images to a tar archive"},
{"start", "Start one or more stopped containers"},
{"stats", "Display a live stream of container(s) resource usage statistics"},