Skip to content

Commit

Permalink
Added customizable columns to incus profile list
Browse files Browse the repository at this point in the history
  • Loading branch information
amro kerkiz committed Apr 28, 2024
1 parent e2842dd commit c8d4476
Showing 1 changed file with 74 additions and 8 deletions.
82 changes: 74 additions & 8 deletions cmd/incus/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ import (
"github.com/lxc/incus/v6/shared/termios"
)

type profileColumn struct {
Name string
Data func(api.Profile) string
}

type cmdProfile struct {
global *cmdGlobal
}
Expand Down Expand Up @@ -692,6 +697,7 @@ type cmdProfileList struct {
global *cmdGlobal
profile *cmdProfile
flagFormat string
flagColumns string
}

func (c *cmdProfileList) Command() *cobra.Command {
Expand All @@ -700,7 +706,17 @@ func (c *cmdProfileList) Command() *cobra.Command {
cmd.Aliases = []string{"ls"}
cmd.Short = i18n.G("List profiles")
cmd.Long = cli.FormatSection(i18n.G("Description"), i18n.G(
`List profiles`))
`List profiles
The -c option takes a (optionally comma-separated) list of arguments
that control which image attributes to output when displaying in table
or csv format.
Default column layout is: nipvbwzdu
Column shorthand chars:
n - Profile Name
d - Description
u - Used By`))

cmd.Flags().StringVarP(&c.flagColumns, "columns", "c", defaultProfileColumns, i18n.G("Columns")+"``")

cmd.RunE = c.Run
cmd.Flags().StringVarP(&c.flagFormat, "format", "f", "table", i18n.G("Format (csv|json|table|yaml|compact)")+"``")
Expand All @@ -716,6 +732,49 @@ func (c *cmdProfileList) Command() *cobra.Command {
return cmd
}

const defaultProfileColumns = "ndu"
func (c *cmdProfileList) parseColumns() ([]profileColumn, error) {
columnsShorthandMap := map[rune]profileColumn{
'n': {i18n.G("NAME"), c.profileNameColumnData},
'd': {i18n.G("DESCRIPTION"), c.descriptionColumnData},
'u': {i18n.G("USED BY"), c.usedByColumnData},
}


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

columns := []profileColumn{}

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 *cmdProfileList) profileNameColumnData(profile api.Profile) string {
return profile.Name
}

func (c *cmdProfileList) descriptionColumnData(profile api.Profile) string {
return profile.Description
}

func (c *cmdProfileList) usedByColumnData(profile api.Profile) string {
return fmt.Sprintf("%d", len(profile.UsedBy))
}

func (c *cmdProfileList) Run(cmd *cobra.Command, args []string) error {
// Quick checks.
exit, err := c.global.CheckArgs(cmd, args, 0, 1)
Expand All @@ -742,18 +801,25 @@ func (c *cmdProfileList) Run(cmd *cobra.Command, args []string) error {
return err
}

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

data := [][]string{}
for _, profile := range profiles {
strUsedBy := fmt.Sprintf("%d", len(profile.UsedBy))
data = append(data, []string{profile.Name, profile.Description, strUsedBy})
line := []string{}
for _, column := range columns {
line = append(line, column.Data(profile))
}
data = append(data, line)
}

sort.Sort(cli.SortColumnsNaturally(data))

header := []string{
i18n.G("NAME"),
i18n.G("DESCRIPTION"),
i18n.G("USED BY")}

header := []string{}
for _, column := range columns {
header = append(header, column.Name)

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

0 comments on commit c8d4476

Please sign in to comment.