2016-04-19 12:59:48 -04:00
|
|
|
package cobraadaptor
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/docker/docker/api/client"
|
2016-05-31 19:49:32 -04:00
|
|
|
"github.com/docker/docker/api/client/container"
|
2016-06-03 13:50:01 -04:00
|
|
|
"github.com/docker/docker/api/client/image"
|
2016-06-06 04:28:52 -04:00
|
|
|
"github.com/docker/docker/api/client/network"
|
2016-06-13 22:56:23 -04:00
|
|
|
"github.com/docker/docker/api/client/node"
|
2016-06-10 06:04:29 -04:00
|
|
|
"github.com/docker/docker/api/client/registry"
|
2016-06-13 22:56:23 -04:00
|
|
|
"github.com/docker/docker/api/client/service"
|
|
|
|
"github.com/docker/docker/api/client/swarm"
|
2016-06-06 22:15:37 -04:00
|
|
|
"github.com/docker/docker/api/client/system"
|
2016-04-19 12:59:48 -04:00
|
|
|
"github.com/docker/docker/api/client/volume"
|
|
|
|
"github.com/docker/docker/cli"
|
|
|
|
cliflags "github.com/docker/docker/cli/flags"
|
|
|
|
"github.com/docker/docker/pkg/term"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CobraAdaptor is an adaptor for supporting spf13/cobra commands in the
|
|
|
|
// docker/cli framework
|
|
|
|
type CobraAdaptor struct {
|
|
|
|
rootCmd *cobra.Command
|
|
|
|
dockerCli *client.DockerCli
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewCobraAdaptor returns a new handler
|
|
|
|
func NewCobraAdaptor(clientFlags *cliflags.ClientFlags) CobraAdaptor {
|
|
|
|
stdin, stdout, stderr := term.StdStreams()
|
|
|
|
dockerCli := client.NewDockerCli(stdin, stdout, stderr, clientFlags)
|
|
|
|
|
2016-05-16 17:20:29 -04:00
|
|
|
var rootCmd = &cobra.Command{
|
|
|
|
Use: "docker",
|
|
|
|
SilenceUsage: true,
|
|
|
|
SilenceErrors: true,
|
|
|
|
}
|
|
|
|
rootCmd.SetUsageTemplate(usageTemplate)
|
|
|
|
rootCmd.SetHelpTemplate(helpTemplate)
|
2016-06-01 01:19:13 -04:00
|
|
|
rootCmd.SetFlagErrorFunc(cli.FlagErrorFunc)
|
2016-05-16 17:20:29 -04:00
|
|
|
rootCmd.SetOutput(stdout)
|
2016-04-19 12:59:48 -04:00
|
|
|
rootCmd.AddCommand(
|
2016-06-13 22:56:23 -04:00
|
|
|
node.NewNodeCommand(dockerCli),
|
|
|
|
service.NewServiceCommand(dockerCli),
|
|
|
|
swarm.NewSwarmCommand(dockerCli),
|
2016-06-05 12:17:39 -04:00
|
|
|
container.NewAttachCommand(dockerCli),
|
2016-06-06 10:57:44 -04:00
|
|
|
container.NewCommitCommand(dockerCli),
|
2016-06-14 11:16:59 -04:00
|
|
|
container.NewCopyCommand(dockerCli),
|
2016-06-01 01:19:13 -04:00
|
|
|
container.NewCreateCommand(dockerCli),
|
2016-06-05 18:13:55 -04:00
|
|
|
container.NewDiffCommand(dockerCli),
|
2016-06-05 10:42:19 -04:00
|
|
|
container.NewExportCommand(dockerCli),
|
2016-06-06 03:38:20 -04:00
|
|
|
container.NewKillCommand(dockerCli),
|
2016-06-05 18:50:48 -04:00
|
|
|
container.NewLogsCommand(dockerCli),
|
2016-06-05 19:12:59 -04:00
|
|
|
container.NewPauseCommand(dockerCli),
|
2016-06-05 19:48:26 -04:00
|
|
|
container.NewPortCommand(dockerCli),
|
2016-06-08 08:56:44 -04:00
|
|
|
container.NewPsCommand(dockerCli),
|
2016-06-05 22:16:26 -04:00
|
|
|
container.NewRenameCommand(dockerCli),
|
2016-06-06 10:15:46 -04:00
|
|
|
container.NewRestartCommand(dockerCli),
|
2016-06-06 11:32:38 -04:00
|
|
|
container.NewRmCommand(dockerCli),
|
2016-05-31 19:49:32 -04:00
|
|
|
container.NewRunCommand(dockerCli),
|
2016-06-05 11:03:58 -04:00
|
|
|
container.NewStartCommand(dockerCli),
|
2016-06-06 12:47:18 -04:00
|
|
|
container.NewStatsCommand(dockerCli),
|
2016-06-05 13:25:21 -04:00
|
|
|
container.NewStopCommand(dockerCli),
|
2016-06-06 09:38:43 -04:00
|
|
|
container.NewTopCommand(dockerCli),
|
2016-06-05 14:43:07 -04:00
|
|
|
container.NewUnpauseCommand(dockerCli),
|
2016-06-05 20:25:22 -04:00
|
|
|
container.NewWaitCommand(dockerCli),
|
2016-06-07 12:15:44 -04:00
|
|
|
image.NewBuildCommand(dockerCli),
|
2016-06-05 22:54:33 -04:00
|
|
|
image.NewHistoryCommand(dockerCli),
|
2016-06-05 23:55:47 -04:00
|
|
|
image.NewImagesCommand(dockerCli),
|
2016-06-09 11:28:33 -04:00
|
|
|
image.NewLoadCommand(dockerCli),
|
2016-06-05 16:40:35 -04:00
|
|
|
image.NewRemoveCommand(dockerCli),
|
2016-06-09 11:38:20 -04:00
|
|
|
image.NewSaveCommand(dockerCli),
|
2016-06-10 06:07:23 -04:00
|
|
|
image.NewPullCommand(dockerCli),
|
2016-06-10 06:07:28 -04:00
|
|
|
image.NewPushCommand(dockerCli),
|
2016-06-03 13:50:01 -04:00
|
|
|
image.NewSearchCommand(dockerCli),
|
2016-06-06 07:58:23 -04:00
|
|
|
image.NewImportCommand(dockerCli),
|
2016-06-05 14:05:35 -04:00
|
|
|
image.NewTagCommand(dockerCli),
|
2016-06-06 04:28:52 -04:00
|
|
|
network.NewNetworkCommand(dockerCli),
|
2016-06-06 12:47:18 -04:00
|
|
|
system.NewEventsCommand(dockerCli),
|
2016-06-10 06:04:29 -04:00
|
|
|
registry.NewLoginCommand(dockerCli),
|
|
|
|
registry.NewLogoutCommand(dockerCli),
|
2016-06-06 22:15:37 -04:00
|
|
|
system.NewVersionCommand(dockerCli),
|
2016-05-31 19:49:32 -04:00
|
|
|
volume.NewVolumeCommand(dockerCli),
|
2016-04-19 12:59:48 -04:00
|
|
|
)
|
2016-05-16 17:20:29 -04:00
|
|
|
|
2016-05-31 17:47:51 -04:00
|
|
|
rootCmd.PersistentFlags().BoolP("help", "h", false, "Print usage")
|
|
|
|
rootCmd.PersistentFlags().MarkShorthandDeprecated("help", "please use --help")
|
|
|
|
|
2016-04-19 12:59:48 -04:00
|
|
|
return CobraAdaptor{
|
|
|
|
rootCmd: rootCmd,
|
|
|
|
dockerCli: dockerCli,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Usage returns the list of commands and their short usage string for
|
|
|
|
// all top level cobra commands.
|
|
|
|
func (c CobraAdaptor) Usage() []cli.Command {
|
|
|
|
cmds := []cli.Command{}
|
|
|
|
for _, cmd := range c.rootCmd.Commands() {
|
2016-05-31 19:49:32 -04:00
|
|
|
cmds = append(cmds, cli.Command{Name: cmd.Name(), Description: cmd.Short})
|
2016-04-19 12:59:48 -04:00
|
|
|
}
|
|
|
|
return cmds
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c CobraAdaptor) run(cmd string, args []string) error {
|
2016-06-06 14:17:04 -04:00
|
|
|
if err := c.dockerCli.Initialize(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-04-19 12:59:48 -04:00
|
|
|
// Prepend the command name to support normal cobra command delegation
|
|
|
|
c.rootCmd.SetArgs(append([]string{cmd}, args...))
|
|
|
|
return c.rootCmd.Execute()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Command returns a cli command handler if one exists
|
|
|
|
func (c CobraAdaptor) Command(name string) func(...string) error {
|
|
|
|
for _, cmd := range c.rootCmd.Commands() {
|
|
|
|
if cmd.Name() == name {
|
|
|
|
return func(args ...string) error {
|
|
|
|
return c.run(name, args)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-16 17:20:29 -04:00
|
|
|
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}}
|
2016-04-19 12:59:48 -04:00
|
|
|
|
|
|
|
Aliases:
|
2016-05-16 17:20:29 -04:00
|
|
|
{{.NameAndAliases}}{{end}}{{if .HasExample}}
|
2016-04-19 12:59:48 -04:00
|
|
|
|
|
|
|
Examples:
|
2016-05-16 17:20:29 -04:00
|
|
|
{{ .Example }}{{end}}{{if .HasFlags}}
|
2016-04-19 12:59:48 -04:00
|
|
|
|
|
|
|
Options:
|
2016-05-16 17:20:29 -04:00
|
|
|
{{.Flags.FlagUsages | trimRightSpace}}{{end}}{{ if .HasAvailableSubCommands}}
|
2016-04-19 12:59:48 -04:00
|
|
|
|
|
|
|
Commands:{{range .Commands}}{{if .IsAvailableCommand}}
|
|
|
|
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{ if .HasSubCommands }}
|
|
|
|
|
|
|
|
Run '{{.CommandPath}} COMMAND --help' for more information on a command.{{end}}
|
|
|
|
`
|
2016-05-16 17:20:29 -04:00
|
|
|
|
|
|
|
var helpTemplate = `
|
|
|
|
{{if or .Runnable .HasSubCommands}}{{.UsageString}}{{end}}`
|