2016-04-19 16:59:48 +00:00
|
|
|
package cobraadaptor
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/docker/docker/api/client"
|
2016-05-31 23:49:32 +00:00
|
|
|
"github.com/docker/docker/api/client/container"
|
2016-06-03 17:50:01 +00:00
|
|
|
"github.com/docker/docker/api/client/image"
|
2016-06-06 08:28:52 +00:00
|
|
|
"github.com/docker/docker/api/client/network"
|
2016-06-14 02:56:23 +00:00
|
|
|
"github.com/docker/docker/api/client/node"
|
2016-05-16 15:50:55 +00:00
|
|
|
"github.com/docker/docker/api/client/plugin"
|
2016-06-10 10:04:29 +00:00
|
|
|
"github.com/docker/docker/api/client/registry"
|
2016-06-14 02:56:23 +00:00
|
|
|
"github.com/docker/docker/api/client/service"
|
2016-06-08 17:47:46 +00:00
|
|
|
"github.com/docker/docker/api/client/stack"
|
2016-06-14 02:56:23 +00:00
|
|
|
"github.com/docker/docker/api/client/swarm"
|
2016-06-07 02:15:37 +00:00
|
|
|
"github.com/docker/docker/api/client/system"
|
2016-04-19 16:59:48 +00: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 21:20:29 +00:00
|
|
|
var rootCmd = &cobra.Command{
|
|
|
|
Use: "docker",
|
|
|
|
SilenceUsage: true,
|
|
|
|
SilenceErrors: true,
|
|
|
|
}
|
|
|
|
rootCmd.SetUsageTemplate(usageTemplate)
|
|
|
|
rootCmd.SetHelpTemplate(helpTemplate)
|
2016-06-01 05:19:13 +00:00
|
|
|
rootCmd.SetFlagErrorFunc(cli.FlagErrorFunc)
|
2016-05-16 21:20:29 +00:00
|
|
|
rootCmd.SetOutput(stdout)
|
2016-04-19 16:59:48 +00:00
|
|
|
rootCmd.AddCommand(
|
2016-06-14 02:56:23 +00:00
|
|
|
node.NewNodeCommand(dockerCli),
|
|
|
|
service.NewServiceCommand(dockerCli),
|
2016-06-08 17:47:46 +00:00
|
|
|
stack.NewStackCommand(dockerCli),
|
|
|
|
stack.NewTopLevelDeployCommand(dockerCli),
|
2016-06-14 02:56:23 +00:00
|
|
|
swarm.NewSwarmCommand(dockerCli),
|
2016-06-05 16:17:39 +00:00
|
|
|
container.NewAttachCommand(dockerCli),
|
2016-06-06 14:57:44 +00:00
|
|
|
container.NewCommitCommand(dockerCli),
|
2016-06-14 15:16:59 +00:00
|
|
|
container.NewCopyCommand(dockerCli),
|
2016-06-01 05:19:13 +00:00
|
|
|
container.NewCreateCommand(dockerCli),
|
2016-06-05 22:13:55 +00:00
|
|
|
container.NewDiffCommand(dockerCli),
|
2016-06-05 14:42:19 +00:00
|
|
|
container.NewExportCommand(dockerCli),
|
2016-06-06 07:38:20 +00:00
|
|
|
container.NewKillCommand(dockerCli),
|
2016-06-05 22:50:48 +00:00
|
|
|
container.NewLogsCommand(dockerCli),
|
2016-06-05 23:12:59 +00:00
|
|
|
container.NewPauseCommand(dockerCli),
|
2016-06-05 23:48:26 +00:00
|
|
|
container.NewPortCommand(dockerCli),
|
2016-06-08 12:56:44 +00:00
|
|
|
container.NewPsCommand(dockerCli),
|
2016-06-06 02:16:26 +00:00
|
|
|
container.NewRenameCommand(dockerCli),
|
2016-06-06 14:15:46 +00:00
|
|
|
container.NewRestartCommand(dockerCli),
|
2016-06-06 15:32:38 +00:00
|
|
|
container.NewRmCommand(dockerCli),
|
2016-05-31 23:49:32 +00:00
|
|
|
container.NewRunCommand(dockerCli),
|
2016-06-05 15:03:58 +00:00
|
|
|
container.NewStartCommand(dockerCli),
|
2016-06-06 16:47:18 +00:00
|
|
|
container.NewStatsCommand(dockerCli),
|
2016-06-05 17:25:21 +00:00
|
|
|
container.NewStopCommand(dockerCli),
|
2016-06-06 13:38:43 +00:00
|
|
|
container.NewTopCommand(dockerCli),
|
2016-06-05 18:43:07 +00:00
|
|
|
container.NewUnpauseCommand(dockerCli),
|
2016-06-05 20:55:00 +00:00
|
|
|
container.NewUpdateCommand(dockerCli),
|
2016-06-06 00:25:22 +00:00
|
|
|
container.NewWaitCommand(dockerCli),
|
2016-06-07 16:15:44 +00:00
|
|
|
image.NewBuildCommand(dockerCli),
|
2016-06-06 02:54:33 +00:00
|
|
|
image.NewHistoryCommand(dockerCli),
|
2016-06-06 03:55:47 +00:00
|
|
|
image.NewImagesCommand(dockerCli),
|
2016-06-09 15:28:33 +00:00
|
|
|
image.NewLoadCommand(dockerCli),
|
2016-06-05 20:40:35 +00:00
|
|
|
image.NewRemoveCommand(dockerCli),
|
2016-06-09 15:38:20 +00:00
|
|
|
image.NewSaveCommand(dockerCli),
|
2016-06-10 10:07:23 +00:00
|
|
|
image.NewPullCommand(dockerCli),
|
2016-06-10 10:07:28 +00:00
|
|
|
image.NewPushCommand(dockerCli),
|
2016-06-03 17:50:01 +00:00
|
|
|
image.NewSearchCommand(dockerCli),
|
2016-06-06 11:58:23 +00:00
|
|
|
image.NewImportCommand(dockerCli),
|
2016-06-05 18:05:35 +00:00
|
|
|
image.NewTagCommand(dockerCli),
|
2016-06-06 08:28:52 +00:00
|
|
|
network.NewNetworkCommand(dockerCli),
|
2016-06-06 16:47:18 +00:00
|
|
|
system.NewEventsCommand(dockerCli),
|
2016-06-10 10:04:29 +00:00
|
|
|
registry.NewLoginCommand(dockerCli),
|
|
|
|
registry.NewLogoutCommand(dockerCli),
|
2016-06-07 02:15:37 +00:00
|
|
|
system.NewVersionCommand(dockerCli),
|
2016-05-31 23:49:32 +00:00
|
|
|
volume.NewVolumeCommand(dockerCli),
|
2016-06-08 21:42:25 +00:00
|
|
|
system.NewInfoCommand(dockerCli),
|
2016-04-19 16:59:48 +00:00
|
|
|
)
|
2016-05-16 15:50:55 +00:00
|
|
|
plugin.NewPluginCommand(rootCmd, dockerCli)
|
2016-05-16 21:20:29 +00:00
|
|
|
|
2016-05-31 21:47:51 +00:00
|
|
|
rootCmd.PersistentFlags().BoolP("help", "h", false, "Print usage")
|
|
|
|
rootCmd.PersistentFlags().MarkShorthandDeprecated("help", "please use --help")
|
|
|
|
|
2016-04-19 16:59:48 +00: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-06-08 17:47:46 +00:00
|
|
|
if cmd.Name() != "" {
|
|
|
|
cmds = append(cmds, cli.Command{Name: cmd.Name(), Description: cmd.Short})
|
|
|
|
}
|
2016-04-19 16:59:48 +00:00
|
|
|
}
|
|
|
|
return cmds
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c CobraAdaptor) run(cmd string, args []string) error {
|
2016-06-06 18:17:04 +00:00
|
|
|
if err := c.dockerCli.Initialize(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-04-19 16:59:48 +00: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 21:20:29 +00: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 16:59:48 +00:00
|
|
|
|
|
|
|
Aliases:
|
2016-05-16 21:20:29 +00:00
|
|
|
{{.NameAndAliases}}{{end}}{{if .HasExample}}
|
2016-04-19 16:59:48 +00:00
|
|
|
|
|
|
|
Examples:
|
2016-05-16 21:20:29 +00:00
|
|
|
{{ .Example }}{{end}}{{if .HasFlags}}
|
2016-04-19 16:59:48 +00:00
|
|
|
|
|
|
|
Options:
|
2016-05-16 21:20:29 +00:00
|
|
|
{{.Flags.FlagUsages | trimRightSpace}}{{end}}{{ if .HasAvailableSubCommands}}
|
2016-04-19 16:59:48 +00: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 21:20:29 +00:00
|
|
|
|
|
|
|
var helpTemplate = `
|
|
|
|
{{if or .Runnable .HasSubCommands}}{{.UsageString}}{{end}}`
|