forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
space.go
132 lines (109 loc) · 4.26 KB
/
space.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package space
import (
"fmt"
"strings"
. "github.com/cloudfoundry/cli/cf/i18n"
"github.com/cloudfoundry/cli/cf/models"
"github.com/cloudfoundry/cli/cf/api/space_quotas"
"github.com/cloudfoundry/cli/cf/command_metadata"
"github.com/cloudfoundry/cli/cf/configuration/core_config"
"github.com/cloudfoundry/cli/cf/formatters"
"github.com/cloudfoundry/cli/cf/requirements"
"github.com/cloudfoundry/cli/cf/terminal"
"github.com/codegangsta/cli"
)
type ShowSpace struct {
ui terminal.UI
config core_config.Reader
spaceReq requirements.SpaceRequirement
quotaRepo space_quotas.SpaceQuotaRepository
}
func NewShowSpace(ui terminal.UI, config core_config.Reader, quotaRepo space_quotas.SpaceQuotaRepository) (cmd *ShowSpace) {
cmd = new(ShowSpace)
cmd.ui = ui
cmd.config = config
cmd.quotaRepo = quotaRepo
return
}
func (cmd *ShowSpace) Metadata() command_metadata.CommandMetadata {
return command_metadata.CommandMetadata{
Name: "space",
Description: T("Show space info"),
Usage: T("CF_NAME space SPACE"),
}
}
func (cmd *ShowSpace) GetRequirements(requirementsFactory requirements.Factory, c *cli.Context) (reqs []requirements.Requirement, err error) {
if len(c.Args()) != 1 {
cmd.ui.FailWithUsage(c)
}
cmd.spaceReq = requirementsFactory.NewSpaceRequirement(c.Args()[0])
reqs = []requirements.Requirement{
requirementsFactory.NewLoginRequirement(),
requirementsFactory.NewTargetedOrgRequirement(),
cmd.spaceReq,
}
return
}
func (cmd *ShowSpace) Run(c *cli.Context) {
space := cmd.spaceReq.GetSpace()
cmd.ui.Say(T("Getting info for space {{.TargetSpace}} in org {{.OrgName}} as {{.CurrentUser}}...",
map[string]interface{}{
"TargetSpace": terminal.EntityNameColor(space.Name),
"OrgName": terminal.EntityNameColor(space.Organization.Name),
"CurrentUser": terminal.EntityNameColor(cmd.config.Username()),
}))
quotaString := cmd.quotaString(space)
cmd.ui.Ok()
cmd.ui.Say("")
table := terminal.NewTable(cmd.ui, []string{terminal.EntityNameColor(space.Name), "", ""})
table.Add("", T("Org:"), terminal.EntityNameColor(space.Organization.Name))
apps := []string{}
for _, app := range space.Applications {
apps = append(apps, terminal.EntityNameColor(app.Name))
}
table.Add("", T("Apps:"), strings.Join(apps, ", "))
domains := []string{}
for _, domain := range space.Domains {
domains = append(domains, terminal.EntityNameColor(domain.Name))
}
table.Add("", T("Domains:"), strings.Join(domains, ", "))
services := []string{}
for _, service := range space.ServiceInstances {
services = append(services, terminal.EntityNameColor(service.Name))
}
table.Add("", T("Services:"), strings.Join(services, ", "))
securityGroups := []string{}
for _, group := range space.SecurityGroups {
securityGroups = append(securityGroups, terminal.EntityNameColor(group.Name))
}
table.Add("", T("Security Groups:"), strings.Join(securityGroups, ", "))
table.Add("", T("Space Quota:"), quotaString)
table.Print()
}
func (cmd *ShowSpace) quotaString(space models.Space) string {
var instance_memory string
if space.SpaceQuotaGuid == "" {
return ""
}
quota, err := cmd.quotaRepo.FindByGuid(space.SpaceQuotaGuid)
if err != nil {
cmd.ui.Failed(err.Error())
return ""
}
if quota.InstanceMemoryLimit == -1 {
instance_memory = "-1"
} else {
instance_memory = formatters.ByteSize(quota.InstanceMemoryLimit * formatters.MEGABYTE)
}
memory := formatters.ByteSize(quota.MemoryLimit * formatters.MEGABYTE)
spaceQuota := fmt.Sprintf("%s (%s memory limit, %s instance memory limit, %d routes, %d services, paid services %s)", quota.Name, memory, instance_memory, quota.RoutesLimit, quota.ServicesLimit, formatters.Allowed(quota.NonBasicServicesAllowed))
// spaceQuota := fmt.Sprintf(T("{{.QuotaName}} ({{.MemoryLimit}} memory limit, {{.InstanceMemoryLimit}} instance memory limit, {{.RoutesLimit}} routes, {{.ServicesLimit}} services, paid services {{.NonBasicServicesAllowed}})",
// map[string]interface{}{
// "QuotaName": quota.Name,
// "MemoryLimit": memory,
// "InstanceMemoryLimit": instance_memory,
// "RoutesLimit": quota.RoutesLimit,
// "ServicesLimit": quota.ServicesLimit,
// "NonBasicServicesAllowed": formatters.Allowed(quota.NonBasicServicesAllowed)}))
return spaceQuota
}