Skip to content

Commit

Permalink
feat: extend extension list result and fixed new model name field (#211)
Browse files Browse the repository at this point in the history
  • Loading branch information
fredmaggiowski authored Jul 22, 2024
1 parent dd3bb37 commit 4e39a14
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 14 deletions.
6 changes: 3 additions & 3 deletions internal/cmd/extensions/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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)
}
Expand Down
6 changes: 3 additions & 3 deletions internal/cmd/extensions/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"},
Expand Down Expand Up @@ -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"},
Expand Down
47 changes: 44 additions & 3 deletions internal/cmd/extensions/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
15 changes: 11 additions & 4 deletions internal/cmd/extensions/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
}
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion internal/resources/extensibility/extensionInfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down

0 comments on commit 4e39a14

Please sign in to comment.