-
Notifications
You must be signed in to change notification settings - Fork 929
/
marketplace_services.go
91 lines (76 loc) · 2.13 KB
/
marketplace_services.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
package service
import (
"cf/api"
"cf/configuration"
"cf/models"
"cf/requirements"
"cf/terminal"
"github.com/codegangsta/cli"
"sort"
"strings"
)
type MarketplaceServices struct {
ui terminal.UI
config configuration.Reader
serviceRepo api.ServiceRepository
}
func NewMarketplaceServices(ui terminal.UI, config configuration.Reader, serviceRepo api.ServiceRepository) (cmd MarketplaceServices) {
cmd.ui = ui
cmd.config = config
cmd.serviceRepo = serviceRepo
return
}
func (cmd MarketplaceServices) GetRequirements(requirementsFactory requirements.Factory, c *cli.Context) (reqs []requirements.Requirement, err error) {
reqs = append(reqs, requirementsFactory.NewApiEndpointRequirement())
return
}
func (cmd MarketplaceServices) Run(c *cli.Context) {
var (
serviceOfferings models.ServiceOfferings
apiErr error
)
if cmd.config.HasSpace() {
cmd.ui.Say("Getting services from marketplace in org %s / space %s as %s...",
terminal.EntityNameColor(cmd.config.OrganizationFields().Name),
terminal.EntityNameColor(cmd.config.SpaceFields().Name),
terminal.EntityNameColor(cmd.config.Username()),
)
serviceOfferings, apiErr = cmd.serviceRepo.GetServiceOfferingsForSpace(cmd.config.SpaceFields().Guid)
} else if !cmd.config.IsLoggedIn() {
cmd.ui.Say("Getting all services from marketplace...")
serviceOfferings, apiErr = cmd.serviceRepo.GetAllServiceOfferings()
} else {
cmd.ui.Failed("Cannot list marketplace services without a targeted space")
}
if apiErr != nil {
cmd.ui.Failed(apiErr.Error())
return
}
cmd.ui.Ok()
cmd.ui.Say("")
if len(serviceOfferings) == 0 {
cmd.ui.Say("No service offerings found")
return
}
table := [][]string{
[]string{"service", "plans", "description"},
}
sort.Sort(serviceOfferings)
for _, offering := range serviceOfferings {
planNames := ""
for _, plan := range offering.Plans {
if plan.Name == "" {
continue
}
planNames = planNames + ", " + plan.Name
}
planNames = strings.TrimPrefix(planNames, ", ")
table = append(table, []string{
offering.Label,
planNames,
offering.Description,
})
}
cmd.ui.DisplayTable(table)
return
}