forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runner.go
81 lines (62 loc) · 1.71 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
package commands
import (
"github.com/cloudfoundry/cli/cf/command"
testreq "github.com/cloudfoundry/cli/testhelpers/requirements"
testterm "github.com/cloudfoundry/cli/testhelpers/terminal"
)
type RunCommandResult int
const (
RunCommandResultSuccess = iota
RunCommandResultFailed = iota
RunCommandResultRequirementsFailed = iota
)
var CommandDidPassRequirements bool
func RunCommand(cmd command.Command, args []string, requirementsFactory *testreq.FakeReqFactory) (passedRequirements bool) {
context := NewContext(cmd.Metadata().Name, args)
defer func() {
errMsg := recover()
if errMsg != nil && errMsg != testterm.QuietPanic {
panic(errMsg)
}
}()
CommandDidPassRequirements = false
requirements, err := cmd.GetRequirements(requirementsFactory, context)
if err != nil {
return
}
for _, requirement := range requirements {
success := requirement.Execute()
if !success {
return
}
}
passedRequirements = true
CommandDidPassRequirements = true
cmd.Run(context)
return
}
func RunCommandMoreBetter(cmd command.Command, requirementsFactory *testreq.FakeReqFactory, args ...string) (result RunCommandResult) {
defer func() {
errMsg := recover()
if errMsg == nil {
return
}
if errMsg != nil && errMsg != testterm.QuietPanic {
panic(errMsg)
}
result = RunCommandResultFailed
}()
context := NewContext(cmd.Metadata().Name, args)
requirements, err := cmd.GetRequirements(requirementsFactory, context)
if err != nil {
return RunCommandResultRequirementsFailed
}
for _, requirement := range requirements {
success := requirement.Execute()
if !success {
return RunCommandResultRequirementsFailed
}
}
cmd.Run(context)
return RunCommandResultSuccess
}