forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
call_command_registry.go
57 lines (43 loc) · 1.19 KB
/
call_command_registry.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package rpc
import (
"fmt"
"code.cloudfoundry.org/cli/cf/commandregistry"
"code.cloudfoundry.org/cli/cf/flags"
"code.cloudfoundry.org/cli/cf/requirements"
)
//go:generate counterfeiter . CommandRunner
type CommandRunner interface {
Command([]string, commandregistry.Dependency, bool) error
}
type commandRunner struct{}
func NewCommandRunner() CommandRunner {
return &commandRunner{}
}
func (c *commandRunner) Command(args []string, deps commandregistry.Dependency, pluginApiCall bool) (err error) {
cmdRegistry := commandregistry.Commands
if cmdRegistry.CommandExists(args[0]) {
fc := flags.NewFlagContext(cmdRegistry.FindCommand(args[0]).MetaData().Flags)
err = fc.Parse(args[1:]...)
if err != nil {
return err
}
cfCmd := cmdRegistry.FindCommand(args[0])
cfCmd = cfCmd.SetDependency(deps, pluginApiCall)
reqs, reqErr := cfCmd.Requirements(requirements.NewFactory(deps.Config, deps.RepoLocator), fc)
if reqErr != nil {
return reqErr
}
for _, r := range reqs {
if err = r.Execute(); err != nil {
return err
}
}
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("command panic: %v", r)
}
}()
return cfCmd.Execute(fc)
}
return nil
}