forked from cloudfoundry/cli
/
services_command.go
137 lines (114 loc) · 3.19 KB
/
services_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
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
133
134
135
136
137
package v7
import (
"strings"
"github.com/LukasHeimann/cloudfoundrycli/v8/resources"
"github.com/LukasHeimann/cloudfoundrycli/v8/actor/v7action"
"github.com/LukasHeimann/cloudfoundrycli/v8/types"
"github.com/LukasHeimann/cloudfoundrycli/v8/util/ui"
)
type ServicesCommand struct {
BaseCommand
OmitApps bool `long:"no-apps" description:"Do not retrieve bound apps information."`
relatedCommands interface{} `related_commands:"create-service, marketplace"`
}
func (cmd ServicesCommand) Execute(args []string) error {
if err := cmd.SharedActor.CheckTarget(true, true); err != nil {
return err
}
if err := cmd.displayMessage(); err != nil {
return err
}
instances, warnings, err := cmd.Actor.GetServiceInstancesForSpace(cmd.Config.TargetedSpace().GUID, cmd.OmitApps)
cmd.UI.DisplayWarnings(warnings)
if err != nil {
return err
}
cmd.displayTable(instances)
return nil
}
func (cmd ServicesCommand) Usage() string {
return "CF_NAME services"
}
func (cmd ServicesCommand) displayMessage() error {
user, err := cmd.Actor.GetCurrentUser()
if err != nil {
return err
}
cmd.UI.DisplayTextWithFlavor("Getting service instances in org {{.OrgName}} / space {{.SpaceName}} as {{.UserName}}...", map[string]interface{}{
"OrgName": cmd.Config.TargetedOrganization().Name,
"SpaceName": cmd.Config.TargetedSpace().Name,
"UserName": user.Name,
})
cmd.UI.DisplayNewline()
return nil
}
func (cmd ServicesCommand) displayTable(instances []v7action.ServiceInstance) {
if len(instances) == 0 {
cmd.UI.DisplayText("No service instances found.")
return
}
table := NewServicesTable(false, cmd.OmitApps)
for _, si := range instances {
table.AppendRow(si)
}
cmd.UI.DisplayTableWithHeader("", table.table, ui.DefaultTableSpacePadding)
}
func upgradeAvailableString(u types.OptionalBoolean) string {
switch {
case u.IsSet && u.Value:
return "yes"
case u.IsSet:
return "no"
default:
return ""
}
}
func serviceOfferingName(si v7action.ServiceInstance) string {
if si.Type == resources.UserProvidedServiceInstance {
return "user-provided"
}
return si.ServiceOfferingName
}
type ServicesTable struct {
table [][]string
short bool
showApps bool
}
func NewServicesTable(short bool, omitApps bool) *ServicesTable {
t := &ServicesTable{
short: short,
showApps: !omitApps,
}
return t.withHeaders()
}
func (t *ServicesTable) withHeaders() *ServicesTable {
headers := []string{"name"}
if t.short {
if t.showApps {
headers = append(headers, "bound apps")
}
} else {
headers = append(headers, "offering", "plan")
if t.showApps {
headers = append(headers, "bound apps")
}
headers = append(headers, "last operation", "broker", "upgrade available")
}
t.table = [][]string{headers}
return t
}
func (t *ServicesTable) AppendRow(si v7action.ServiceInstance) {
row := []string{si.Name}
if t.short {
if t.showApps {
row = append(row, strings.Join(si.BoundApps, ", "))
}
} else {
row = append(row, serviceOfferingName(si), si.ServicePlanName)
if t.showApps {
row = append(row, strings.Join(si.BoundApps, ", "))
}
row = append(row, si.LastOperation, si.ServiceBrokerName, upgradeAvailableString(si.UpgradeAvailable))
}
t.table = append(t.table, row)
}