forked from cloudfoundry-incubator/cflocal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cf.go
50 lines (44 loc) · 955 Bytes
/
cf.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
package cf
import "errors"
type CF struct {
UI UI
Help Help
Cmds []Cmd
Version string
}
type UI interface {
Prompt(prompt string) string
Output(format string, a ...interface{})
Error(err error)
}
//go:generate mockgen -package mocks -destination mocks/help.go code.cloudfoundry.org/cflocal/cf Help
type Help interface {
Short()
Long()
}
//go:generate mockgen -package mocks -destination mocks/cmd.go code.cloudfoundry.org/cflocal/cf Cmd
type Cmd interface {
Match(args []string) bool
Run(args []string) error
}
func (c *CF) Run(args []string) error {
if len(args) == 0 {
c.Help.Short()
return errors.New("command required")
}
switch args[0] {
case "help":
c.Help.Long()
return nil
case "version", "--version":
c.UI.Output("CF Local version %s", c.Version)
return nil
}
for _, cmd := range c.Cmds {
if cmd.Match(args) {
return cmd.Run(args)
}
}
c.Help.Short()
return errors.New("invalid command")
}