forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spaces_command.go
79 lines (64 loc) · 1.99 KB
/
spaces_command.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
package v2
import (
"code.cloudfoundry.org/cli/actor/sharedaction"
"code.cloudfoundry.org/cli/actor/v2action"
"code.cloudfoundry.org/cli/command"
"code.cloudfoundry.org/cli/command/v2/shared"
"code.cloudfoundry.org/cli/util/ui"
)
//go:generate counterfeiter . SpacesActor
type SpacesActor interface {
GetOrganizationSpaces(orgGUID string) ([]v2action.Space, v2action.Warnings, error)
}
type SpacesCommand struct {
usage interface{} `usage:"CF_NAME spaces"`
relatedCommands interface{} `related_commands:"target"`
UI command.UI
Config command.Config
SharedActor command.SharedActor
Actor SpacesActor
}
func (cmd *SpacesCommand) Setup(config command.Config, ui command.UI) error {
cmd.Config = config
cmd.UI = ui
cmd.SharedActor = sharedaction.NewActor()
ccClient, _, err := shared.NewClients(config, ui, true)
if err != nil {
return err
}
cmd.Actor = v2action.NewActor(ccClient, nil, config)
return nil
}
func (cmd SpacesCommand) Execute([]string) error {
err := cmd.SharedActor.CheckTarget(cmd.Config, true, false)
if err != nil {
return shared.HandleError(err)
}
user, err := cmd.Config.CurrentUser()
if err != nil {
return shared.HandleError(err)
}
cmd.UI.DisplayTextWithFlavor("Getting spaces in org {{.OrgName}} as {{.CurrentUser}}...", map[string]interface{}{
"OrgName": cmd.Config.TargetedOrganization().Name,
"CurrentUser": user.Name,
})
cmd.UI.DisplayNewline()
spaces, warnings, err := cmd.Actor.GetOrganizationSpaces(cmd.Config.TargetedOrganization().GUID)
cmd.UI.DisplayWarnings(warnings)
if err != nil {
return shared.HandleError(err)
}
if len(spaces) == 0 {
cmd.UI.DisplayText("No spaces found.")
} else {
cmd.displaySpaces(spaces)
}
return nil
}
func (cmd SpacesCommand) displaySpaces(spaces []v2action.Space) {
table := [][]string{{cmd.UI.TranslateText("name")}}
for _, space := range spaces {
table = append(table, []string{space.Name})
}
cmd.UI.DisplayTableWithHeader("", table, ui.DefaultTableSpacePadding)
}