diff --git a/internal/cmd/extensions/client.go b/internal/cmd/extensions/client.go index 629c55d..4237aa6 100644 --- a/internal/cmd/extensions/client.go +++ b/internal/cmd/extensions/client.go @@ -36,7 +36,7 @@ const ( const IFrameExtensionType = "iframe" type IE11yClient interface { - List(ctx context.Context, companyID string) ([]*extensibility.Extension, error) + List(ctx context.Context, companyID string) ([]*extensibility.ExtensionInfo, error) GetOne(ctx context.Context, companyID string, extensionID string) (*extensibility.ExtensionInfo, error) Apply(ctx context.Context, companyID string, extensionData *extensibility.Extension) (string, error) Delete(ctx context.Context, companyID string, extensionID string) error @@ -52,7 +52,7 @@ func New(c *client.APIClient) IE11yClient { return &E11yClient{c: c} } -func (e *E11yClient) List(ctx context.Context, companyID string) ([]*extensibility.Extension, error) { +func (e *E11yClient) List(ctx context.Context, companyID string) ([]*extensibility.ExtensionInfo, error) { apiPath := fmt.Sprintf(tenantsExtensionsAPIFmt, companyID) resp, err := e.c.Get().APIPath(apiPath).Do(ctx) if err != nil { @@ -63,7 +63,7 @@ func (e *E11yClient) List(ctx context.Context, companyID string) ([]*extensibili return nil, err } - extensions := make([]*extensibility.Extension, 0) + extensions := make([]*extensibility.ExtensionInfo, 0) if err := resp.ParseResponse(&extensions); err != nil { return nil, fmt.Errorf("error parsing response body: %w", err) } diff --git a/internal/cmd/extensions/client_test.go b/internal/cmd/extensions/client_test.go index aaccb95..2285fd5 100644 --- a/internal/cmd/extensions/client_test.go +++ b/internal/cmd/extensions/client_test.go @@ -89,7 +89,7 @@ func TestE11yClientList(t *testing.T) { require.Nil(t, data) } else { require.NoError(t, err) - require.Equal(t, []*extensibility.Extension{ + require.Equal(t, []*extensibility.ExtensionInfo{ { ExtensionID: "ext-1", Name: "Extension 1", @@ -109,7 +109,7 @@ func TestE11yClientList(t *testing.T) { func TestE11yClientGetOne(t *testing.T) { validBodyString := `{ "extensionId": "mocked-id", - "extensionName": "mocked-name", + "name": "mocked-name", "entry": "http://example.com/", "type": "iframe", "destination": {"id": "project"}, @@ -168,7 +168,7 @@ func TestE11yClientGetOne(t *testing.T) { require.NoError(t, err) require.Equal(t, &extensibility.ExtensionInfo{ ExtensionID: "mocked-id", - ExtensionName: "mocked-name", + Name: "mocked-name", Entry: "http://example.com/", Type: "iframe", Destination: extensibility.DestinationArea{ID: "project"}, diff --git a/internal/cmd/extensions/list.go b/internal/cmd/extensions/list.go index f31ee95..bb7c656 100644 --- a/internal/cmd/extensions/list.go +++ b/internal/cmd/extensions/list.go @@ -16,6 +16,8 @@ package extensions import ( + "fmt" + "github.com/mia-platform/miactl/internal/client" "github.com/mia-platform/miactl/internal/clioptions" "github.com/mia-platform/miactl/internal/printer" @@ -44,20 +46,59 @@ func ListCmd(options *clioptions.CLIOptions) *cobra.Command { extensions, err := extensibilityClient.List(cmd.Context(), restConfig.CompanyID) cobra.CheckErr(err) - printExtensionsList(extensions, options.Printer()) + printExtensionsList(extensions, options.Printer(clioptions.DisableWrapLines(true))) return nil }, } } -func printExtensionsList(extensions []*extensibility.Extension, p printer.IPrinter) { - p.Keys("ID", "Name", "Description") +func printExtensionsList(extensions []*extensibility.ExtensionInfo, p printer.IPrinter) { + p.Keys("ID", "Name", "Entry", "Destination", "Menu (id) / Category (id)", "Description") for _, extension := range extensions { p.Record( extension.ExtensionID, extension.Name, + extension.Entry, + extension.Destination.ID, + menucolumn(extension), extension.Description, ) } p.Print() } + +func menucolumn(extension *extensibility.ExtensionInfo) string { + if extension.Menu.ID == "" { + return "" + } + + menuLabel := getTranslation(extension.Menu.LabelIntl, extensibility.En) + if menuLabel == "" { + return "" + } + + menu := fmt.Sprintf("%s (%s)", menuLabel, extension.Menu.ID) + + categoryLabel := getTranslation(extension.Category.LabelIntl, extensibility.En) + if categoryLabel != "" { + menu += fmt.Sprintf(" / %s (%s)", categoryLabel, extension.Category.ID) + } + + return menu +} + +func getTranslation(messages extensibility.IntlMessages, defaultLang extensibility.Languages) string { + if len(messages) == 0 { + return "" + } + + defaultMessage, ok := messages[defaultLang] + if !ok { + for _, msg := range messages { + if msg != "" { + return msg + } + } + } + return defaultMessage +} diff --git a/internal/cmd/extensions/list_test.go b/internal/cmd/extensions/list_test.go index 02c2311..3572e92 100644 --- a/internal/cmd/extensions/list_test.go +++ b/internal/cmd/extensions/list_test.go @@ -33,15 +33,22 @@ func TestListCommandBuilder(t *testing.T) { } func TestPrintExtensionsList(t *testing.T) { - data := []*extensibility.Extension{ + data := []*extensibility.ExtensionInfo{ { ExtensionID: "ext-1", Name: "Extension 1", + Entry: "https://docs.mia-platform.eu", + Destination: extensibility.DestinationArea{ID: "d1"}, + Menu: extensibility.Menu{ID: "menu-id", LabelIntl: extensibility.IntlMessages{"en": "Ex 1"}}, + Category: extensibility.Category{ID: "cat-1", LabelIntl: extensibility.IntlMessages{"en": "Cat 1"}}, Description: "Description 1", }, { ExtensionID: "ext-2", Name: "Extension 2", + Entry: "https://mia-platform.eu", + Destination: extensibility.DestinationArea{ID: "d2"}, + Menu: extensibility.Menu{ID: "menu-id-2", LabelIntl: extensibility.IntlMessages{"en": "Ex 2"}}, Description: "Description 2", }, } @@ -53,9 +60,9 @@ func TestPrintExtensionsList(t *testing.T) { ) expectedTokens := []string{ - "ID", "NAME", "DESCRIPTION", - "ext-1", "Extension 1", "Description 1", - "ext-2", "Extension 2", "Description 2", + "ID", "NAME", "ENTRY", "DESTINATION", "MENU (ID) / CATEGORY (ID)", "DESCRIPTION", + "ext-1", "Extension 1", "https://docs.mia-platform.eu", "d1", "Ex 1 (menu-id) / Cat 1 (cat-1)", "Description 1", + "ext-2", "Extension 2", "https://mia-platform.eu", "d2", "Ex 2 (menu-id-2)", "Description 2", } for _, expected := range expectedTokens { diff --git a/internal/resources/extensibility/extensionInfo.go b/internal/resources/extensibility/extensionInfo.go index c685b33..33d5f0a 100644 --- a/internal/resources/extensibility/extensionInfo.go +++ b/internal/resources/extensibility/extensionInfo.go @@ -50,7 +50,7 @@ type Menu struct { type ExtensionInfo struct { ExtensionID string `json:"extensionId" yaml:"extensionId"` - ExtensionName string `json:"extensionName" yaml:"extensionName"` + Name string `json:"name" yaml:"name"` Entry string `json:"entry" yaml:"entry"` Type string `json:"type" yaml:"type"` Destination DestinationArea `json:"destination" yaml:"destination"`