Skip to content

Commit c33c7aa

Browse files
authored
Merge pull request #1102 from HassanAlsamahi/cluster-group-list-add-column-flag
Cluster group list add column flag
2 parents ab81c29 + 8d3526a commit c33c7aa

File tree

12 files changed

+1388
-1026
lines changed

12 files changed

+1388
-1026
lines changed

cmd/incus/cluster_group.go

+83-7
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ type cmdClusterGroup struct {
2222
cluster *cmdCluster
2323
}
2424

25+
type clusterGroupColumn struct {
26+
Name string
27+
Data func(api.ClusterGroup) string
28+
}
29+
2530
// Cluster management including assignment, creation, deletion, editing, listing, removal, renaming, and showing details.
2631
func (c *cmdClusterGroup) Command() *cobra.Command {
2732
cmd := &cobra.Command{}
@@ -439,7 +444,8 @@ type cmdClusterGroupList struct {
439444
global *cmdGlobal
440445
cluster *cmdCluster
441446

442-
flagFormat string
447+
flagFormat string
448+
flagColumns string
443449
}
444450

445451
// Command returns a cobra command to list all the cluster groups in a specified format.
@@ -449,7 +455,26 @@ func (c *cmdClusterGroupList) Command() *cobra.Command {
449455
cmd.Aliases = []string{"ls"}
450456
cmd.Short = i18n.G("List all the cluster groups")
451457
cmd.Long = cli.FormatSection(i18n.G("Description"), i18n.G(
452-
`List all the cluster groups`))
458+
`List all the cluster groups
459+
460+
Default column layout: ndm
461+
462+
== Columns ==
463+
The -c option takes a comma separated list of arguments that control
464+
which instance attributes to output when displaying in table or csv
465+
format.
466+
467+
Column arguments are either pre-defined shorthand chars (see below),
468+
or (extended) config keys.
469+
470+
Commas between consecutive shorthand chars are optional.
471+
472+
Pre-defined column shorthand chars:
473+
n - Name
474+
d - Description
475+
m - Member`))
476+
477+
cmd.Flags().StringVarP(&c.flagColumns, "columns", "c", defaultClusterGroupColumns, i18n.G("Columns")+"``")
453478
cmd.Flags().StringVarP(&c.flagFormat, "format", "f", "table", i18n.G("Format (csv|json|table|yaml|compact)")+"``")
454479

455480
cmd.RunE = c.Run
@@ -465,6 +490,48 @@ func (c *cmdClusterGroupList) Command() *cobra.Command {
465490
return cmd
466491
}
467492

493+
const defaultClusterGroupColumns = "ndm"
494+
495+
func (c *cmdClusterGroupList) parseColumns() ([]clusterGroupColumn, error) {
496+
columnsShorthandMap := map[rune]clusterGroupColumn{
497+
'n': {i18n.G("NAME"), c.clusterGroupNameColumnData},
498+
'm': {i18n.G("MEMBERS"), c.membersColumnData},
499+
'd': {i18n.G("DESCRIPTION"), c.descriptionColumnData},
500+
}
501+
502+
columnList := strings.Split(c.flagColumns, ",")
503+
columns := []clusterGroupColumn{}
504+
505+
for _, columnEntry := range columnList {
506+
if columnEntry == "" {
507+
return nil, fmt.Errorf(i18n.G("Empty column entry (redundant, leading or trailing command) in '%s'"), c.flagColumns)
508+
}
509+
510+
for _, columnRune := range columnEntry {
511+
column, ok := columnsShorthandMap[columnRune]
512+
if !ok {
513+
return nil, fmt.Errorf(i18n.G("Unknown column shorthand char '%c' in '%s'"), columnRune, columnEntry)
514+
}
515+
516+
columns = append(columns, column)
517+
}
518+
}
519+
520+
return columns, nil
521+
}
522+
523+
func (c *cmdClusterGroupList) clusterGroupNameColumnData(group api.ClusterGroup) string {
524+
return group.Name
525+
}
526+
527+
func (c *cmdClusterGroupList) descriptionColumnData(group api.ClusterGroup) string {
528+
return group.Description
529+
}
530+
531+
func (c *cmdClusterGroupList) membersColumnData(group api.ClusterGroup) string {
532+
return fmt.Sprintf("%d", len(group.Members))
533+
}
534+
468535
// Run executes the command to list all the cluster groups, their descriptions, and number of members.
469536
func (c *cmdClusterGroupList) Run(cmd *cobra.Command, args []string) error {
470537
// Quick checks.
@@ -501,19 +568,28 @@ func (c *cmdClusterGroupList) Run(cmd *cobra.Command, args []string) error {
501568
return err
502569
}
503570

571+
// Parse column flags.
572+
columns, err := c.parseColumns()
573+
if err != nil {
574+
return err
575+
}
576+
504577
// Render the table
505578
data := [][]string{}
506579
for _, group := range groups {
507-
line := []string{group.Name, group.Description, fmt.Sprintf("%d", len(group.Members))}
580+
line := []string{}
581+
for _, column := range columns {
582+
line = append(line, column.Data(group))
583+
}
584+
508585
data = append(data, line)
509586
}
510587

511588
sort.Sort(cli.SortColumnsNaturally(data))
512589

513-
header := []string{
514-
i18n.G("NAME"),
515-
i18n.G("DESCRIPTION"),
516-
i18n.G("MEMBERS"),
590+
header := []string{}
591+
for _, column := range columns {
592+
header = append(header, column.Name)
517593
}
518594

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

0 commit comments

Comments
 (0)