forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 1
/
runner.go
87 lines (71 loc) · 1.9 KB
/
runner.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package commands
import (
"fmt"
"os"
"code.cloudfoundry.org/cli/cf/commandregistry"
"code.cloudfoundry.org/cli/cf/flags"
"code.cloudfoundry.org/cli/cf/requirements"
testterm "code.cloudfoundry.org/cli/cf/util/testhelpers/terminal"
)
type RunCommandResult int
const (
RunCommandResultSuccess = iota
)
func RunCLICommand(cmdName string, args []string, requirementsFactory requirements.Factory, updateFunc func(bool), pluginCall bool, ui *testterm.FakeUI) bool {
updateFunc(pluginCall)
cmd := commandregistry.Commands.FindCommand(cmdName)
context := flags.NewFlagContext(cmd.MetaData().Flags)
context.SkipFlagParsing(cmd.MetaData().SkipFlagParsing)
err := context.Parse(args...)
if err != nil {
fmt.Println("ERROR:", err)
os.Exit(1)
}
var requirements []requirements.Requirement
requirements, err = cmd.Requirements(requirementsFactory, context)
if err != nil {
return false
}
for _, requirement := range requirements {
if err = requirement.Execute(); err != nil {
ui.Failed(err.Error())
return false
}
}
err = cmd.Execute(context)
if err != nil {
ui.Failed(err.Error())
return false
}
return true
}
func RunCLICommandWithoutDependency(cmdName string, args []string, requirementsFactory requirements.Factory, ui *testterm.FakeUI) bool {
cmd := commandregistry.Commands.FindCommand(cmdName)
context := flags.NewFlagContext(cmd.MetaData().Flags)
context.SkipFlagParsing(cmd.MetaData().SkipFlagParsing)
err := context.Parse(args...)
if err != nil {
fmt.Println("ERROR:", err)
os.Exit(1)
}
defer func() {
errMsg := recover()
if errMsg != nil {
panic(errMsg)
}
}()
requirements, err := cmd.Requirements(requirementsFactory, context)
if err != nil {
return false
}
for _, requirement := range requirements {
if err = requirement.Execute(); err != nil {
return false
}
}
err = cmd.Execute(context)
if err != nil {
ui.Failed(err.Error())
}
return true
}