-
Notifications
You must be signed in to change notification settings - Fork 16
/
catalog.go
95 lines (84 loc) · 2.18 KB
/
catalog.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
package cmd
import (
"encoding/json"
"fmt"
"os"
"github.com/jhunt/go-table"
"github.com/starkandwayne/eden/apiclient"
)
// CatalogOpts represents the 'catalog' command
type CatalogOpts struct {
Strict bool `long:"strict" description:"Validate the catalog using the same heuristics as Cloud Foundry" env:"EDEN_STRICT"`
}
// Execute is callback from go-flags.Commander interface
func (c CatalogOpts) Execute(_ []string) (err error) {
broker := apiclient.NewOpenServiceBroker(
Opts.Broker.URLOpt,
Opts.Broker.ClientOpt,
Opts.Broker.ClientSecretOpt,
Opts.Broker.APIVersion,
)
catalogResp, err := broker.Catalog()
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
if Opts.Catalog.Strict {
errors := make([]error, 0)
seen := make(map[string]bool)
for _, service := range catalogResp.Services {
for _, plan := range service.Plans {
if _, exists := seen[plan.ID]; exists {
errors = append(errors, fmt.Errorf("Service '%s' Plan '%s' ID '%s' is not unique", service.Name, plan.Name, plan.ID))
}
seen[plan.ID] = true
}
}
if len(errors) != 0 {
fmt.Fprintf(os.Stderr, "Catalog validation failed:\n")
for _, err := range errors {
fmt.Fprintf(os.Stderr, " - %s\n", err)
}
os.Exit(1)
}
}
if Opts.JSON {
b, err := json.Marshal(catalogResp)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
fmt.Printf("%s\n", string(b))
os.Exit(0)
}
table := table.NewTable("Service", "Plan", "Free", "Description")
var serviceID string
var planID string
previousService := ""
for _, service := range catalogResp.Services {
if serviceID == "" {
serviceID = service.ID
}
for _, plan := range service.Plans {
if planID == "" {
planID = plan.ID
}
/* FIXME service descriptions are ignored */
freeOrPaid := "unspecified"
if plan.Free != nil {
freeOrPaid = "paid"
if *plan.Free {
freeOrPaid = "free"
}
}
if previousService == service.Name {
table.Row(nil, "~", plan.Name, freeOrPaid, plan.Description)
} else {
table.Row(nil, service.Name, plan.Name, freeOrPaid, plan.Description)
}
previousService = service.Name
}
}
table.Output(os.Stdout)
return
}