Skip to content

Commit

Permalink
modules render refactor (#2173)
Browse files Browse the repository at this point in the history
  • Loading branch information
anoipm authored Jul 8, 2024
1 parent c4496e7 commit c33dd0d
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 76 deletions.
8 changes: 4 additions & 4 deletions internal/cmd/alpha/modules/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func collectiveView(cfg *modulesConfig) clierror.Error {

combinedData := communitymodules.MergeRowMaps(catalog, installedWith, managedWith)

communitymodules.RenderTableForCollective(cfg.raw, combinedData)
communitymodules.RenderModules(cfg.raw, combinedData, communitymodules.CollectiveTableInfo)
return nil
}

Expand All @@ -110,7 +110,7 @@ func listInstalledModules(cfg *modulesConfig) clierror.Error {
return clierror.WrapE(err, clierror.New("failed to get installed Kyma modules"))
}

communitymodules.RenderTableForInstalled(cfg.raw, installed)
communitymodules.RenderModules(cfg.raw, installed, communitymodules.InstalledTableInfo)
return nil
}

Expand All @@ -121,7 +121,7 @@ func listManagedModules(cfg *modulesConfig) clierror.Error {
return clierror.WrapE(err, clierror.New("failed to get managed Kyma modules"))
}

communitymodules.RenderTableForManaged(cfg.raw, managed)
communitymodules.RenderModules(cfg.raw, managed, communitymodules.ManagedTableInfo)
return nil
}

Expand All @@ -132,6 +132,6 @@ func listModulesCatalog(cfg *modulesConfig) clierror.Error {
return clierror.WrapE(err, clierror.New("failed to get all Kyma catalog"))
}

communitymodules.RenderTableForCatalog(cfg.raw, catalog)
communitymodules.RenderModules(cfg.raw, catalog, communitymodules.CatalogTableInfo)
return nil
}
76 changes: 33 additions & 43 deletions internal/communitymodules/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,62 +2,53 @@ package communitymodules

import (
"os"
"sort"
"strings"

"github.com/olekukonko/tablewriter"
)

func RenderTableForCollective(raw bool, moduleMap moduleMap) {
renderTable(raw,
convertRowToCollective(moduleMap),
[]string{"NAME", "REPOSITORY", "VERSION INSTALLED", "MANAGED"})
type RowConverter func(row) []string
type TableInfo struct {
Header []string
RowConverter RowConverter
}

func convertRowToCollective(moduleMap moduleMap) [][]string {
var result [][]string
for _, row := range moduleMap {
result = append(result, []string{row.Name, row.Repository, row.Version, row.Managed})
var (
CollectiveTableInfo = TableInfo{
Header: []string{"NAME", "REPOSITORY", "VERSION INSTALLED", "MANAGED"},
RowConverter: func(r row) []string { return []string{r.Name, r.Repository, r.Version, r.Managed} },
}
return result
}

func RenderTableForInstalled(raw bool, moduleMap moduleMap) {
renderTable(raw,
convertRowToInstalled(moduleMap),
[]string{"NAME", "VERSION"})
}

func convertRowToInstalled(moduleMap moduleMap) [][]string {
var result [][]string
for _, row := range moduleMap {
result = append(result, []string{row.Name, row.Version})
InstalledTableInfo = TableInfo{
Header: []string{"NAME", "VERSION"},
RowConverter: func(r row) []string { return []string{r.Name, r.Version} },
}
return result
}
ManagedTableInfo = TableInfo{
Header: []string{"NAME"},
RowConverter: func(r row) []string { return []string{r.Name} },
}
CatalogTableInfo = TableInfo{
Header: []string{"NAME", "REPOSITORY", "LATEST VERSION"},
RowConverter: func(r row) []string { return []string{r.Name, r.Repository, r.LatestVersion} },
}
)

func RenderTableForManaged(raw bool, moduleMap moduleMap) {
renderTable(raw,
convertRowToManaged(moduleMap),
[]string{"NAME"})
func RenderModules(raw bool, moduleMap moduleMap, tableInfo TableInfo) {
renderTable(
raw,
convertModuleMapToTable(moduleMap, tableInfo.RowConverter),
tableInfo.Header)
}

func convertRowToManaged(moduleMap moduleMap) [][]string {
var result [][]string
for _, row := range moduleMap {
result = append(result, []string{row.Name})
func convertModuleMapToTable(moduleMap moduleMap, rowConverter RowConverter) [][]string {
var moduleNames []string
for key := range moduleMap {
moduleNames = append(moduleNames, key)
}
return result
}
func RenderTableForCatalog(raw bool, moduleMap moduleMap) {
renderTable(raw,
convertRowToCatalog(moduleMap),
[]string{"NAME", "REPOSITORY", "LATEST VERSION"})
}

func convertRowToCatalog(moduleMap moduleMap) [][]string {
sort.Strings(moduleNames)
var result [][]string
for _, row := range moduleMap {
result = append(result, []string{row.Name, row.Repository, row.LatestVersion})
for _, key := range moduleNames {
result = append(result, rowConverter(moduleMap[key]))
}
return result
}
Expand All @@ -69,7 +60,6 @@ func renderTable(raw bool, modulesData [][]string, headers []string) {
println(strings.Join(row, "\t"))
}
} else {

var table [][]string
table = append(table, modulesData...)

Expand Down
57 changes: 28 additions & 29 deletions internal/communitymodules/render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,41 +33,40 @@ func TestRender(t *testing.T) {
Managed: "testMan2",
},
}
var testMapSort = moduleMap{
"ghi": {
Name: "ghi",
Version: "3",
},
"def": {
Name: "def",
Version: "2",
},
"abc": {
Name: "abc",
Version: "1",
},
"jkl": {
Name: "jkl",
Version: "4",
},
}

t.Run("RenderTable... doesn't panic for empty Map", func(t *testing.T) {
t.Run("RenderModules doesn't panic for empty Map", func(t *testing.T) {
require.NotPanics(t, func() {
RenderTableForCatalog(false, moduleMapEmpty)
RenderTableForManaged(false, moduleMapEmpty)
RenderTableForInstalled(false, moduleMapEmpty)
RenderTableForCollective(false, moduleMapEmpty)
})
})
t.Run("convertRow... doesn't panic for empty Map", func(t *testing.T) {
require.NotPanics(t, func() {
convertRowToCollective(moduleMapEmpty)
convertRowToCatalog(moduleMapEmpty)
convertRowToManaged(moduleMapEmpty)
convertRowToInstalled(moduleMapEmpty)
RenderModules(false, moduleMapEmpty, CatalogTableInfo)
})
})
t.Run("convertRowToCatalog", func(t *testing.T) {
result := convertRowToCatalog(testMap)
require.Equal(t, [][]string{{"testName", "testRepo", "testLatest"}}, result)
})
t.Run("convertRowToManaged", func(t *testing.T) {
result := convertRowToManaged(testMap)
require.Equal(t, [][]string{{"testName"}}, result)
})
t.Run("convertRowToInstalled", func(t *testing.T) {
result := convertRowToInstalled(testMap)
require.Equal(t, [][]string{{"testName", "testVer"}}, result)
result := convertModuleMapToTable(testMap, func(r row) []string { return []string{r.Name, r.LatestVersion, r.Version} })
require.Equal(t, [][]string{{"testName", "testLatest", "testVer"}}, result)
})
t.Run("convertRowToCollective", func(t *testing.T) {
result := convertRowToCollective(testMap)
require.Equal(t, [][]string{{"testName", "testRepo", "testVer", "testMan"}}, result)
t.Run("convertRowToCatalog for map with mutliple entries", func(t *testing.T) {
result := convertModuleMapToTable(testMapLong, func(r row) []string { return []string{r.Repository, r.LatestVersion, r.Managed} })
require.ElementsMatch(t, [][]string{{"testRepo1", "testLatest1", "testMan1"}, {"testRepo2", "testLatest2", "testMan2"}}, result)
})
t.Run("for map with mutliple entries", func(t *testing.T) {
result := convertRowToCatalog(testMapLong)
require.ElementsMatch(t, [][]string{{"testName1", "testRepo1", "testLatest1"}, {"testName2", "testRepo2", "testLatest2"}}, result)
t.Run("sort names", func(t *testing.T) {
result := convertModuleMapToTable(testMapSort, func(r row) []string { return []string{r.Name, r.Version} })
require.Equal(t, [][]string{{"abc", "1"}, {"def", "2"}, {"ghi", "3"}, {"jkl", "4"}}, result)
})
}

0 comments on commit c33dd0d

Please sign in to comment.