2015-05-05 00:18:28 -04:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
flag "github.com/docker/docker/pkg/mflag"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Cli represents a command line interface.
|
|
|
|
type Cli struct {
|
|
|
|
Stderr io.Writer
|
|
|
|
handlers []Handler
|
|
|
|
Usage func()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handler holds the different commands Cli will call
|
|
|
|
// It should have methods with names starting with `Cmd` like:
|
|
|
|
// func (h myHandler) CmdFoo(args ...string) error
|
2016-04-29 11:16:34 -04:00
|
|
|
type Handler interface {
|
|
|
|
Command(name string) func(...string) error
|
|
|
|
}
|
2015-05-05 00:18:28 -04:00
|
|
|
|
|
|
|
// Initializer can be optionally implemented by a Handler to
|
|
|
|
// initialize before each call to one of its commands.
|
|
|
|
type Initializer interface {
|
|
|
|
Initialize() error
|
|
|
|
}
|
|
|
|
|
|
|
|
// New instantiates a ready-to-use Cli.
|
|
|
|
func New(handlers ...Handler) *Cli {
|
|
|
|
// make the generic Cli object the first cli handler
|
|
|
|
// in order to handle `docker help` appropriately
|
|
|
|
cli := new(Cli)
|
|
|
|
cli.handlers = append([]Handler{cli}, handlers...)
|
|
|
|
return cli
|
|
|
|
}
|
|
|
|
|
2016-05-15 21:38:04 -04:00
|
|
|
var errCommandNotFound = errors.New("command not found")
|
2015-05-05 00:18:28 -04:00
|
|
|
|
|
|
|
func (cli *Cli) command(args ...string) (func(...string) error, error) {
|
|
|
|
for _, c := range cli.handlers {
|
|
|
|
if c == nil {
|
|
|
|
continue
|
|
|
|
}
|
2016-04-29 11:16:34 -04:00
|
|
|
if cmd := c.Command(strings.Join(args, " ")); cmd != nil {
|
|
|
|
if ci, ok := c.(Initializer); ok {
|
|
|
|
if err := ci.Initialize(); err != nil {
|
2016-05-15 21:38:04 -04:00
|
|
|
return nil, err
|
2015-05-05 00:18:28 -04:00
|
|
|
}
|
|
|
|
}
|
2016-04-29 11:16:34 -04:00
|
|
|
return cmd, nil
|
2015-05-05 00:18:28 -04:00
|
|
|
}
|
|
|
|
}
|
2016-05-15 21:38:04 -04:00
|
|
|
return nil, errCommandNotFound
|
2015-05-05 00:18:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run executes the specified command.
|
|
|
|
func (cli *Cli) Run(args ...string) error {
|
|
|
|
if len(args) > 1 {
|
|
|
|
command, err := cli.command(args[:2]...)
|
2016-05-15 21:38:04 -04:00
|
|
|
if err == nil {
|
2015-05-05 00:18:28 -04:00
|
|
|
return command(args[2:]...)
|
2016-05-15 21:38:04 -04:00
|
|
|
}
|
|
|
|
if err != errCommandNotFound {
|
|
|
|
return err
|
2015-05-05 00:18:28 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(args) > 0 {
|
|
|
|
command, err := cli.command(args[0])
|
2016-05-15 21:38:04 -04:00
|
|
|
if err != nil {
|
|
|
|
if err == errCommandNotFound {
|
|
|
|
cli.noSuchCommand(args[0])
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
2015-05-05 00:18:28 -04:00
|
|
|
}
|
2016-05-15 21:38:04 -04:00
|
|
|
return command(args[1:]...)
|
2015-05-05 00:18:28 -04:00
|
|
|
}
|
|
|
|
return cli.CmdHelp()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cli *Cli) noSuchCommand(command string) {
|
|
|
|
if cli.Stderr == nil {
|
|
|
|
cli.Stderr = os.Stderr
|
|
|
|
}
|
|
|
|
fmt.Fprintf(cli.Stderr, "docker: '%s' is not a docker command.\nSee 'docker --help'.\n", command)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2016-04-29 11:16:34 -04:00
|
|
|
// Command returns a command handler, or nil if the command does not exist
|
|
|
|
func (cli *Cli) Command(name string) func(...string) error {
|
|
|
|
return map[string]func(...string) error{
|
|
|
|
"help": cli.CmdHelp,
|
|
|
|
}[name]
|
|
|
|
}
|
|
|
|
|
2015-05-05 00:18:28 -04:00
|
|
|
// CmdHelp displays information on a Docker command.
|
|
|
|
//
|
|
|
|
// If more than one command is specified, information is only shown for the first command.
|
|
|
|
//
|
|
|
|
// Usage: docker help COMMAND or docker COMMAND --help
|
|
|
|
func (cli *Cli) CmdHelp(args ...string) error {
|
|
|
|
if len(args) > 1 {
|
|
|
|
command, err := cli.command(args[:2]...)
|
2016-05-15 21:38:04 -04:00
|
|
|
if err == nil {
|
2015-05-05 00:18:28 -04:00
|
|
|
command("--help")
|
|
|
|
return nil
|
2016-05-15 21:38:04 -04:00
|
|
|
}
|
|
|
|
if err != errCommandNotFound {
|
|
|
|
return err
|
2015-05-05 00:18:28 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(args) > 0 {
|
|
|
|
command, err := cli.command(args[0])
|
2016-05-15 21:38:04 -04:00
|
|
|
if err != nil {
|
|
|
|
if err == errCommandNotFound {
|
|
|
|
cli.noSuchCommand(args[0])
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
2015-05-05 00:18:28 -04:00
|
|
|
}
|
2016-05-15 21:38:04 -04:00
|
|
|
command("--help")
|
|
|
|
return nil
|
2015-05-05 00:18:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if cli.Usage == nil {
|
|
|
|
flag.Usage()
|
|
|
|
} else {
|
|
|
|
cli.Usage()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Subcmd is a subcommand of the main "docker" command.
|
|
|
|
// A subcommand represents an action that can be performed
|
|
|
|
// from the Docker command line client.
|
|
|
|
//
|
|
|
|
// To see all available subcommands, run "docker --help".
|
|
|
|
func Subcmd(name string, synopses []string, description string, exitOnError bool) *flag.FlagSet {
|
|
|
|
var errorHandling flag.ErrorHandling
|
|
|
|
if exitOnError {
|
|
|
|
errorHandling = flag.ExitOnError
|
|
|
|
} else {
|
|
|
|
errorHandling = flag.ContinueOnError
|
|
|
|
}
|
|
|
|
flags := flag.NewFlagSet(name, errorHandling)
|
|
|
|
flags.Usage = func() {
|
|
|
|
flags.ShortUsage()
|
|
|
|
flags.PrintDefaults()
|
|
|
|
}
|
|
|
|
|
|
|
|
flags.ShortUsage = func() {
|
|
|
|
if len(synopses) == 0 {
|
|
|
|
synopses = []string{""}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allow for multiple command usage synopses.
|
|
|
|
for i, synopsis := range synopses {
|
|
|
|
lead := "\t"
|
|
|
|
if i == 0 {
|
|
|
|
// First line needs the word 'Usage'.
|
|
|
|
lead = "Usage:\t"
|
|
|
|
}
|
|
|
|
|
|
|
|
if synopsis != "" {
|
|
|
|
synopsis = " " + synopsis
|
|
|
|
}
|
|
|
|
|
2016-07-16 10:44:10 -04:00
|
|
|
fmt.Fprintf(flags.Out(), "\n%sdocker %s%s", lead, name, synopsis)
|
2015-05-05 00:18:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintf(flags.Out(), "\n\n%s\n", description)
|
|
|
|
}
|
|
|
|
|
|
|
|
return flags
|
|
|
|
}
|
|
|
|
|
2016-03-26 10:06:45 -04:00
|
|
|
// StatusError reports an unsuccessful exit by a command.
|
2015-05-05 00:18:28 -04:00
|
|
|
type StatusError struct {
|
|
|
|
Status string
|
|
|
|
StatusCode int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e StatusError) Error() string {
|
|
|
|
return fmt.Sprintf("Status: %s, Code: %d", e.Status, e.StatusCode)
|
|
|
|
}
|