Skip to content

Commit

Permalink
incus/cluster/group/list: Add support for column selection
Browse files Browse the repository at this point in the history
Part of lxc#1027

Signed-off-by: HassanAlsamahi <hassanalsamahi12@gmail.com>
  • Loading branch information
HassanAlsamahi committed Aug 9, 2024
1 parent 051eefb commit 10a1eca
Showing 1 changed file with 60 additions and 5 deletions.
65 changes: 60 additions & 5 deletions cmd/incus/cluster_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ type cmdClusterGroup struct {
cluster *cmdCluster
}

type clusterGroupColumn struct {
Name string
Data func(api.ClusterGroup) string
}

// Cluster management including assignment, creation, deletion, editing, listing, removal, renaming, and showing details.
func (c *cmdClusterGroup) Command() *cobra.Command {
cmd := &cobra.Command{}
Expand Down Expand Up @@ -487,6 +492,47 @@ Pre-defined column shorthand chars:

const defaultClusterGroupColumns = "ndm"

func (c *cmdClusterGroupList) parseColumns() ([]clusterGroupColumn, error) {
columnsShorthandMap := map[rune]clusterGroupColumn{
'n': {i18n.G("NAME"), c.clusterGroupNameColumnData},
'm': {i18n.G("MEMBERS"), c.membersColumnData},
'd': {i18n.G("DESCRIPTION"), c.descriptionColumnData},
}

columnList := strings.Split(c.flagColumns, ",")

columns := []clusterGroupColumn{}

for _, columnEntry := range columnList {
if columnEntry == "" {
return nil, fmt.Errorf(i18n.G("Empty column entry (redundant, leading or trailing command) in '%s'"), c.flagColumns)
}

for _, columnRune := range columnEntry {
column, ok := columnsShorthandMap[columnRune]
if !ok {
return nil, fmt.Errorf(i18n.G("Unknown column shorthand char '%c' in '%s'"), columnRune, columnEntry)
}

columns = append(columns, column)
}
}

return columns, nil
}

func (c *cmdClusterGroupList) clusterGroupNameColumnData(group api.ClusterGroup) string {
return group.Name
}

func (c *cmdClusterGroupList) descriptionColumnData(group api.ClusterGroup) string {
return group.Description
}

func (c *cmdClusterGroupList) membersColumnData(group api.ClusterGroup) string {
return fmt.Sprintf("%d", len(group.Members))
}

// Run executes the command to list all the cluster groups, their descriptions, and number of members.
func (c *cmdClusterGroupList) Run(cmd *cobra.Command, args []string) error {
// Quick checks.
Expand Down Expand Up @@ -523,19 +569,28 @@ func (c *cmdClusterGroupList) Run(cmd *cobra.Command, args []string) error {
return err
}

// Parse column flags.
columns, err := c.parseColumns()
if err != nil {
return err
}

// Render the table
data := [][]string{}
for _, group := range groups {
line := []string{group.Name, group.Description, fmt.Sprintf("%d", len(group.Members))}
line := []string{}
for _, column := range columns {
line = append(line, column.Data(group))
}

data = append(data, line)
}

sort.Sort(cli.SortColumnsNaturally(data))

header := []string{
i18n.G("NAME"),
i18n.G("DESCRIPTION"),
i18n.G("MEMBERS"),
header := []string{}
for _, column := range columns {
header = append(header, column.Name)
}

return cli.RenderTable(c.flagFormat, header, data, groups)
Expand Down

0 comments on commit 10a1eca

Please sign in to comment.