-
Notifications
You must be signed in to change notification settings - Fork 72
/
list.go
119 lines (93 loc) · 3.04 KB
/
list.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
package list
import (
"strconv"
connectormgmtclient "github.com/redhat-developer/app-services-sdk-go/connectormgmt/apiv1/client"
"github.com/redhat-developer/app-services-cli/pkg/core/cmdutil"
"github.com/redhat-developer/app-services-cli/pkg/core/cmdutil/flagutil"
"github.com/redhat-developer/app-services-cli/pkg/core/ioutil/dump"
"github.com/redhat-developer/app-services-cli/pkg/shared/factory"
"github.com/spf13/cobra"
"github.com/redhat-developer/app-services-cli/internal/build"
)
// row is the details of a Kafka instance needed to print to a table
type itemRow struct {
ID string `json:"id" header:"ID"`
Name string `json:"name" header:"Name"`
Owner string `json:"owner" header:"Owner"`
Cluster string `json:"clusterId" header:"Cluster ID"`
}
type options struct {
outputFormat string
page int
limit int
f *factory.Factory
}
func NewListCommand(f *factory.Factory) *cobra.Command {
opts := &options{
page: 0,
limit: 100,
f: f,
}
cmd := &cobra.Command{
Use: "list",
Short: f.Localizer.MustLocalize("connector.namespace.list.cmd.shortDescription"),
Long: f.Localizer.MustLocalize("connector.namespace.list.cmd.longDescription"),
Example: f.Localizer.MustLocalize("connector.namespace.list.cmd.example"),
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
if opts.outputFormat != "" && !flagutil.IsValidInput(opts.outputFormat, flagutil.ValidOutputFormats...) {
return flagutil.InvalidValueError("output", opts.outputFormat, flagutil.ValidOutputFormats...)
}
return runList(opts)
},
}
flags := flagutil.NewFlagSet(cmd, f.Localizer)
flags.AddOutput(&opts.outputFormat)
flags.IntVar(&opts.page, "page", int(cmdutil.ConvertPageValueToInt32(build.DefaultPageNumber)), f.Localizer.MustLocalize("connector.common.list.flag.page"))
flags.IntVar(&opts.limit, "limit", 100, f.Localizer.MustLocalize("connector.common.list.flag.limit"))
return cmd
}
func runList(opts *options) error {
f := opts.f
conn, err := f.Connection()
if err != nil {
return err
}
api := conn.API()
a := api.ConnectorsMgmt().ConnectorNamespacesApi.ListConnectorNamespaces(f.Context)
a = a.Page(strconv.Itoa(opts.page))
a = a.Size(strconv.Itoa(opts.limit))
response, _, err := a.Execute()
if err != nil {
return err
}
if response.Size == 0 && opts.outputFormat == "" {
f.Logger.Info(f.Localizer.MustLocalize("connector.common.log.info.noResults"))
return nil
}
switch opts.outputFormat {
case dump.EmptyFormat:
var rows []itemRow
rows = mapResponseItemsToRows(response.Items)
dump.Table(f.IOStreams.Out, rows)
f.Logger.Info("")
default:
return dump.Formatted(f.IOStreams.Out, opts.outputFormat, response)
}
return nil
}
func mapResponseItemsToRows(items []connectormgmtclient.ConnectorNamespace) []itemRow {
rows := make([]itemRow, len(items))
for i := range items {
k := items[i]
name := k.GetName()
row := itemRow{
ID: k.GetId(),
Name: name,
Owner: k.GetOwner(),
Cluster: k.GetClusterId(),
}
rows[i] = row
}
return rows
}