mirror of https://github.com/docker/cli.git
cli: remove unnecessary initErr type
Signed-off-by: ZhangHang <stevezhang2014@gmail.com> Signed-off-by: Alexander Morozov <lk4d4@docker.com>
This commit is contained in:
parent
3d58ff580f
commit
79b8543b54
53
cli.go
53
cli.go
|
@ -39,12 +39,7 @@ func New(handlers ...Handler) *Cli {
|
||||||
return cli
|
return cli
|
||||||
}
|
}
|
||||||
|
|
||||||
// initErr is an error returned upon initialization of a handler implementing Initializer.
|
var errCommandNotFound = errors.New("command not found")
|
||||||
type initErr struct{ error }
|
|
||||||
|
|
||||||
func (err initErr) Error() string {
|
|
||||||
return err.Error()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cli *Cli) command(args ...string) (func(...string) error, error) {
|
func (cli *Cli) command(args ...string) (func(...string) error, error) {
|
||||||
for _, c := range cli.handlers {
|
for _, c := range cli.handlers {
|
||||||
|
@ -54,35 +49,36 @@ func (cli *Cli) command(args ...string) (func(...string) error, error) {
|
||||||
if cmd := c.Command(strings.Join(args, " ")); cmd != nil {
|
if cmd := c.Command(strings.Join(args, " ")); cmd != nil {
|
||||||
if ci, ok := c.(Initializer); ok {
|
if ci, ok := c.(Initializer); ok {
|
||||||
if err := ci.Initialize(); err != nil {
|
if err := ci.Initialize(); err != nil {
|
||||||
return nil, initErr{err}
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return cmd, nil
|
return cmd, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil, errors.New("command not found")
|
return nil, errCommandNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run executes the specified command.
|
// Run executes the specified command.
|
||||||
func (cli *Cli) Run(args ...string) error {
|
func (cli *Cli) Run(args ...string) error {
|
||||||
if len(args) > 1 {
|
if len(args) > 1 {
|
||||||
command, err := cli.command(args[:2]...)
|
command, err := cli.command(args[:2]...)
|
||||||
switch err := err.(type) {
|
if err == nil {
|
||||||
case nil:
|
|
||||||
return command(args[2:]...)
|
return command(args[2:]...)
|
||||||
case initErr:
|
}
|
||||||
return err.error
|
if err != errCommandNotFound {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if len(args) > 0 {
|
if len(args) > 0 {
|
||||||
command, err := cli.command(args[0])
|
command, err := cli.command(args[0])
|
||||||
switch err := err.(type) {
|
if err != nil {
|
||||||
case nil:
|
if err == errCommandNotFound {
|
||||||
return command(args[1:]...)
|
|
||||||
case initErr:
|
|
||||||
return err.error
|
|
||||||
}
|
|
||||||
cli.noSuchCommand(args[0])
|
cli.noSuchCommand(args[0])
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return command(args[1:]...)
|
||||||
}
|
}
|
||||||
return cli.CmdHelp()
|
return cli.CmdHelp()
|
||||||
}
|
}
|
||||||
|
@ -110,24 +106,25 @@ func (cli *Cli) Command(name string) func(...string) error {
|
||||||
func (cli *Cli) CmdHelp(args ...string) error {
|
func (cli *Cli) CmdHelp(args ...string) error {
|
||||||
if len(args) > 1 {
|
if len(args) > 1 {
|
||||||
command, err := cli.command(args[:2]...)
|
command, err := cli.command(args[:2]...)
|
||||||
switch err := err.(type) {
|
if err == nil {
|
||||||
case nil:
|
|
||||||
command("--help")
|
command("--help")
|
||||||
return nil
|
return nil
|
||||||
case initErr:
|
}
|
||||||
return err.error
|
if err != errCommandNotFound {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if len(args) > 0 {
|
if len(args) > 0 {
|
||||||
command, err := cli.command(args[0])
|
command, err := cli.command(args[0])
|
||||||
switch err := err.(type) {
|
if err != nil {
|
||||||
case nil:
|
if err == errCommandNotFound {
|
||||||
|
cli.noSuchCommand(args[0])
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
command("--help")
|
command("--help")
|
||||||
return nil
|
return nil
|
||||||
case initErr:
|
|
||||||
return err.error
|
|
||||||
}
|
|
||||||
cli.noSuchCommand(args[0])
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if cli.Usage == nil {
|
if cli.Usage == nil {
|
||||||
|
|
Loading…
Reference in New Issue