diff --git a/cmd/incusd/main_cluster.go b/cmd/incusd/main_cluster.go index 91e7dd9beb6..7fa10a43802 100644 --- a/cmd/incusd/main_cluster.go +++ b/cmd/incusd/main_cluster.go @@ -334,6 +334,8 @@ func (c *cmdClusterShow) Run(cmd *cobra.Command, args []string) error { type cmdClusterListDatabase struct { global *cmdGlobal + + flagFormat string } func (c *cmdClusterListDatabase) Command() *cobra.Command { @@ -341,6 +343,7 @@ func (c *cmdClusterListDatabase) Command() *cobra.Command { cmd.Use = "list-database" cmd.Aliases = []string{"ls"} cmd.Short = "Print the addresses of the cluster members serving the database" + cmd.Flags().StringVarP(&c.flagFormat, "format", "f", "table", "Format (csv|json|table|yaml|compact)") cmd.RunE = c.Run @@ -366,7 +369,7 @@ func (c *cmdClusterListDatabase) Run(cmd *cobra.Command, args []string) error { data[i] = []string{address} } - _ = cli.RenderTable(cli.TableFormatTable, columns, data, nil) + _ = cli.RenderTable(c.flagFormat, columns, data, nil) return nil } diff --git a/internal/cmd/table.go b/internal/cmd/table.go index 411b73a065b..9f6b1343ee8 100644 --- a/internal/cmd/table.go +++ b/internal/cmd/table.go @@ -5,6 +5,8 @@ import ( "encoding/json" "fmt" "os" + "slices" + "strings" "github.com/olekukonko/tablewriter" "gopkg.in/yaml.v2" @@ -21,8 +23,25 @@ const ( TableFormatCompact = "compact" ) +const ( + // TableOptionNoHeader hides the table header when possible. + TableOptionNoHeader = "noheader" +) + // RenderTable renders tabular data in various formats. func RenderTable(format string, header []string, data [][]string, raw any) error { + fields := strings.SplitN(format, ",", 2) + format = fields[0] + + var options []string + if len(fields) == 2 { + options = strings.Split(fields[1], ",") + + if slices.Contains(options, TableOptionNoHeader) { + header = nil + } + } + switch format { case TableFormatTable: table := getBaseTable(header, data)