-
Notifications
You must be signed in to change notification settings - Fork 69
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Connector type list #1641
Connector type list #1641
Changes from all commits
746f628
a4cc7aa
abf0ab9
baf97d7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
package list | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"strconv" | ||
|
||
"github.com/redhat-developer/app-services-cli/pkg/cmd/connector/connectorcmdutil" | ||
"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" | ||
"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.IntVar(&opts.limit, "limit", 150, f.Localizer.MustLocalize("connector.type.list.flag.page.description")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should use build package limits here as in other implementations |
||
flags.StringVar(&opts.search, "search", DefaultSearch, f.Localizer.MustLocalize("connector.type.list.flag.search.description")) | ||
flags.IntVar(&opts.page, "page", 1, f.Localizer.MustLocalize("connector.type.list.flag.page.description")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is pagination starting from 0? :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. afaik 1 is the start and when passing 0 it just gives the same output as 1 anyway There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. That is common pattern |
||
return cmd | ||
|
||
} | ||
|
||
func runUpdateCommand(opts *options) error { | ||
f := opts.f | ||
|
||
var conn connection.Connection | ||
conn, err := f.Connection(connection.DefaultConfigSkipMasAuth) | ||
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 := fmt.Sprintf("name like %s", opts.search) | ||
jackdelahunt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
request = request.Search(query) | ||
} | ||
|
||
types, httpRes, err := request.Execute() | ||
|
||
if httpRes != nil { | ||
defer httpRes.Body.Close() | ||
} | ||
|
||
if err != nil { | ||
switch httpRes.StatusCode { | ||
case http.StatusUnauthorized: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is antipattern. We should not use status codes for validation. We should use |
||
return opts.f.Localizer.MustLocalizeError("connector.common.error.unauthorized") | ||
case http.StatusInternalServerError: | ||
return opts.f.Localizer.MustLocalizeError("connector.common.error.internalServerError") | ||
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 | ||
} | ||
} | ||
|
||
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 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package connector_type | ||
|
||
import ( | ||
"github.com/redhat-developer/app-services-cli/internal/doc" | ||
|
||
"github.com/redhat-developer/app-services-cli/pkg/cmd/connector/connector_type/list" | ||
"github.com/redhat-developer/app-services-cli/pkg/shared/factory" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// NewTypeCommand creates a new command to use different connector types | ||
func NewTypeCommand(f *factory.Factory) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "type", | ||
Annotations: map[string]string{doc.AnnotationName: "Connectors commands"}, | ||
Short: f.Localizer.MustLocalize("connector.type.cmd.shortDescription"), | ||
Long: f.Localizer.MustLocalize("connector.type.cmd.longDescription"), | ||
Example: f.Localizer.MustLocalize("connector.type.cmd.example"), | ||
Args: cobra.MinimumNArgs(1), | ||
} | ||
|
||
// add sub-commands | ||
cmd.AddCommand( | ||
list.NewListCommand(f), | ||
) | ||
|
||
return cmd | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wrong naming format :D
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is due to
type
being a reserved word in go. I just went with connector_type instead.