-
Notifications
You must be signed in to change notification settings - Fork 926
/
marketplace_command.go
205 lines (173 loc) · 6.01 KB
/
marketplace_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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package v6
import (
"errors"
"fmt"
"strings"
"code.cloudfoundry.org/cli/actor/sharedaction"
"code.cloudfoundry.org/cli/actor/v2action"
"code.cloudfoundry.org/cli/command"
"code.cloudfoundry.org/cli/command/translatableerror"
"code.cloudfoundry.org/cli/command/v6/shared"
"code.cloudfoundry.org/cli/util/ui"
)
//go:generate counterfeiter . ServicesSummariesActor
type ServicesSummariesActor interface {
GetServicesSummaries() ([]v2action.ServiceSummary, v2action.Warnings, error)
GetServicesSummariesForSpace(spaceGUID string) ([]v2action.ServiceSummary, v2action.Warnings, error)
GetServiceSummaryByName(serviceName string) (v2action.ServiceSummary, v2action.Warnings, error)
GetServiceSummaryForSpaceByName(spaceGUID, serviceName string) (v2action.ServiceSummary, v2action.Warnings, error)
}
type MarketplaceCommand struct {
ServiceName string `short:"s" description:"Show plan details for a particular service offering"`
NoPlans bool `long:"no-plans" description:"Hide plan information for service offerings"`
usage interface{} `usage:"CF_NAME marketplace [-s SERVICE] [--no-plans]"`
relatedCommands interface{} `related_commands:"create-service, services"`
UI command.UI
SharedActor command.SharedActor
Actor ServicesSummariesActor
Config command.Config
}
func (cmd *MarketplaceCommand) Setup(config command.Config, ui command.UI) error {
cmd.Config = config
cmd.UI = ui
cmd.SharedActor = sharedaction.NewActor(config)
ccClient, uaaClient, err := shared.NewClients(config, ui, true)
if err != nil {
return err
}
cmd.Actor = v2action.NewActor(ccClient, uaaClient, config)
return nil
}
func (cmd *MarketplaceCommand) Execute(args []string) error {
if len(args) > 0 {
return translatableerror.TooManyArgumentsError{
ExtraArgument: args[0],
}
}
if !cmd.SharedActor.IsLoggedIn() {
return cmd.publicMarketplace()
}
return cmd.marketplace()
}
func (cmd *MarketplaceCommand) marketplace() error {
user, err := cmd.Config.CurrentUser()
if err != nil {
return err
}
if cmd.ServiceName == "" {
if !cmd.SharedActor.IsOrgTargeted() || !cmd.SharedActor.IsSpaceTargeted() {
return errors.New("Cannot list marketplace services without a targeted space")
}
cmd.UI.DisplayTextWithFlavor("Getting services from marketplace in org {{.OrgName}} / space {{.SpaceName}} as {{.Username}}...", map[string]interface{}{
"OrgName": cmd.Config.TargetedOrganization().Name,
"SpaceName": cmd.Config.TargetedSpace().Name,
"Username": user.Name,
})
serviceSummaries, warnings, err := cmd.Actor.GetServicesSummariesForSpace(cmd.Config.TargetedSpace().GUID)
cmd.UI.DisplayWarnings(warnings)
if err != nil {
return err
}
cmd.UI.DisplayOK()
cmd.displayServiceSummaries(serviceSummaries)
} else {
if !cmd.SharedActor.IsOrgTargeted() || !cmd.SharedActor.IsSpaceTargeted() {
return fmt.Errorf("Cannot list plan information for %s without a targeted space", cmd.ServiceName)
}
cmd.UI.DisplayTextWithFlavor("Getting service plan information for service {{.ServiceName}} as {{.Username}}...",
map[string]interface{}{
"ServiceName": cmd.ServiceName,
"Username": user.Name,
})
serviceSummary, warnings, err := cmd.Actor.GetServiceSummaryForSpaceByName(cmd.Config.TargetedSpace().GUID, cmd.ServiceName)
cmd.UI.DisplayWarnings(warnings)
if err != nil {
return err
}
cmd.UI.DisplayOK()
cmd.displayServiceSummary(serviceSummary)
}
return nil
}
func (cmd *MarketplaceCommand) publicMarketplace() error {
if cmd.ServiceName == "" {
cmd.UI.DisplayText("Getting all services from marketplace...")
serviceSummaries, warnings, err := cmd.Actor.GetServicesSummaries()
cmd.UI.DisplayWarnings(warnings)
if err != nil {
return err
}
cmd.UI.DisplayOK()
cmd.displayServiceSummaries(serviceSummaries)
} else {
cmd.UI.DisplayTextWithFlavor("Getting service plan information for service {{.ServiceName}}...",
map[string]interface{}{
"ServiceName": cmd.ServiceName,
})
serviceSummary, warnings, err := cmd.Actor.GetServiceSummaryByName(cmd.ServiceName)
cmd.UI.DisplayWarnings(warnings)
if err != nil {
return err
}
cmd.UI.DisplayOK()
cmd.displayServiceSummary(serviceSummary)
}
return nil
}
func (cmd *MarketplaceCommand) displayServiceSummaries(serviceSummaries []v2action.ServiceSummary) {
if len(serviceSummaries) == 0 {
cmd.UI.DisplayText("No service offerings found")
} else {
var table = [][]string{}
if cmd.NoPlans {
tableHeaders := []string{"service", "description", "broker"}
table = [][]string{tableHeaders}
for _, serviceSummary := range serviceSummaries {
table = append(table, []string{
serviceSummary.Label,
serviceSummary.Description,
serviceSummary.ServiceBrokerName,
})
}
} else {
tableHeaders := []string{"service", "plans", "description", "broker"}
table = [][]string{tableHeaders}
for _, serviceSummary := range serviceSummaries {
table = append(table, []string{
serviceSummary.Label,
planNames(serviceSummary),
serviceSummary.Description,
serviceSummary.ServiceBrokerName,
})
}
}
cmd.UI.DisplayTableWithHeader("", table, ui.DefaultTableSpacePadding)
cmd.UI.DisplayNewline()
cmd.UI.DisplayText("TIP: Use 'cf marketplace -s SERVICE' to view descriptions of individual plans of a given service.")
}
}
func (cmd *MarketplaceCommand) displayServiceSummary(serviceSummary v2action.ServiceSummary) {
tableHeaders := []string{"service plan", "description", "free or paid"}
table := [][]string{tableHeaders}
for _, plan := range serviceSummary.Plans {
table = append(table, []string{
plan.Name,
plan.Description,
formatFreeOrPaid(plan.Free),
})
}
cmd.UI.DisplayTableWithHeader("", table, ui.DefaultTableSpacePadding)
}
func planNames(serviceSummary v2action.ServiceSummary) string {
names := []string{}
for _, planSummary := range serviceSummary.Plans {
names = append(names, planSummary.Name)
}
return strings.Join(names, ", ")
}
func formatFreeOrPaid(free bool) string {
if free {
return "free"
}
return "paid"
}