-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.go
58 lines (50 loc) · 1.44 KB
/
app.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
package main
import (
"fmt"
"github.com/igm/cf"
)
type AppList []cf.App
func (a AppList) Len() int { return len(a) }
func (a AppList) Title() string {
return fmt.Sprintf("%-20s%-12s%-15s%-30s%-13s", "Name", "Status", "Usage", "SpaceOrg", "Routes")
}
func (a AppList) Render(i int) string {
instances := fmt.Sprintf("%d x %dM", a[i].Instances, a[i].Memory)
return fmt.Sprintf("%-20s%-12s%-15s%-30s%-13s", a[i].Name, a[i].State, instances, a[i].Space, a[i].Routes)
}
func (a AppList) Selection() string { return "Select application: " }
func (a AppList) FindOrPick(name, space, org string) (app cf.App, err error) {
filtered := a.
Filter(func(app cf.App) bool { return app.Name == name || name == "" }).
Filter(func(app cf.App) bool { return app.Space.Name == space || space == "" }).
Filter(func(app cf.App) bool { return app.Space.Organization.Name == org || org == "" })
if len(filtered) == 0 {
filtered = a
}
if len(filtered) != 1 {
i, err := choose(filtered)
if err != nil {
return filtered[0], err
}
app = filtered[i]
return app, err
}
return filtered[0], nil
}
func (a AppList) Filter(fn func(cf.App) bool) AppList {
var res AppList
for _, val := range a {
if fn(val) {
res = append(res, val)
}
}
return res
}
func (target *Target) AppFind(name, space, org string) (app cf.App, err error) {
apps, err := target.AppsGet()
if err != nil {
return
}
app, err = AppList(apps).FindOrPick(name, space, org)
return
}