-
Notifications
You must be signed in to change notification settings - Fork 72
/
list.go
136 lines (107 loc) · 4.08 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package list
import (
"fmt"
"strconv"
"github.com/redhat-developer/app-services-cli/internal/build"
"github.com/redhat-developer/app-services-cli/pkg/cmd/connector/connectorcmdutil"
"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/connection"
"github.com/redhat-developer/app-services-cli/pkg/shared/factory"
connectormgmtclient "github.com/redhat-developer/app-services-sdk-go/connectormgmt/apiv1/client"
connectorerror "github.com/redhat-developer/app-services-sdk-go/connectormgmt/apiv1/error"
"github.com/spf13/cobra"
)
const (
DefaultSearch = ""
)
type options struct {
search string
limit int
page int
outputFormat string
f *factory.Factory
}
type connectorOutput struct {
Name string `json:"name,omitempty"`
Id string `json:"id,omitempty"`
Description string `json:"description,omitempty"`
}
// NewListCommand creates a new command to list connector types
func NewListCommand(f *factory.Factory) *cobra.Command {
opts := &options{
f: f,
}
cmd := &cobra.Command{
Use: "list",
Short: f.Localizer.MustLocalize("connector.type.list.cmd.shortDescription"),
Long: f.Localizer.MustLocalize("connector.type.list.cmd.longDescription"),
Example: f.Localizer.MustLocalize("connector.type.list.cmd.example"),
RunE: func(cmd *cobra.Command, args []string) error {
validOutputFormats := flagutil.ValidOutputFormats
if opts.outputFormat != "" && !flagutil.IsValidInput(opts.outputFormat, validOutputFormats...) {
return flagutil.InvalidValueError("output", opts.outputFormat, validOutputFormats...)
}
return runUpdateCommand(opts)
},
}
flags := connectorcmdutil.NewFlagSet(cmd, f)
flags.AddOutput(&opts.outputFormat)
flags.StringVar(&opts.search, "search", DefaultSearch, f.Localizer.MustLocalize("connector.type.list.flag.search.description"))
flags.IntVar(&opts.limit, "limit", int(cmdutil.ConvertPageValueToInt32(build.DefaultPageSize)), f.Localizer.MustLocalize("connector.type.list.flag.page.description"))
flags.IntVar(&opts.page, "page", int(cmdutil.ConvertPageValueToInt32(build.DefaultPageNumber)), f.Localizer.MustLocalize("connector.type.list.flag.page.description"))
return cmd
}
func runUpdateCommand(opts *options) error {
f := opts.f
var conn connection.Connection
conn, err := f.Connection()
if err != nil {
return err
}
api := conn.API()
request := api.ConnectorsMgmt().ConnectorTypesApi.GetConnectorTypes(f.Context)
request = request.Page(strconv.Itoa(opts.page))
request = request.Size(strconv.Itoa(opts.limit))
if opts.search != DefaultSearch {
query := connectorcmdutil.NewSearchQuery(opts.search).Filter("name").Filter("description").Build()
request = request.Search(query)
}
types, httpRes, err := request.Execute()
if httpRes != nil {
defer httpRes.Body.Close()
}
if apiErr := connectorerror.GetAPIError(err); apiErr != nil {
switch apiErr.GetCode() {
case connectorerror.ERROR_11:
return opts.f.Localizer.MustLocalizeError("connector.common.error.unauthorized")
case connectorerror.ERROR_23:
return opts.f.Localizer.MustLocalizeError("connector.common.error.parse.search")
default:
return err
}
}
rows := mapResponseToConnectorTypes(&types)
for i := 0; i < len(rows); i++ {
if err = dump.Formatted(f.IOStreams.Out, opts.outputFormat, rows[i]); err != nil {
return err
}
}
start := (opts.page - 1) * opts.limit
end := start + len(rows)
opts.f.Logger.Info(fmt.Sprintf("[%v %v : %v - %v]", opts.f.Localizer.MustLocalize("connector.common.page.prompt"), opts.page, start, end))
return nil
}
func mapResponseToConnectorTypes(list *connectormgmtclient.ConnectorTypeList) []connectorOutput {
types := make([]connectorOutput, len(list.Items))
for i := 0; i < len(list.Items); i++ {
item := &list.Items[i]
types[i] = connectorOutput{
Name: item.GetName(),
Id: item.GetId(),
Description: item.GetDescription(),
}
}
return types
}