feat: add generic table formatter (#3415)

This commit is contained in:
Dean Sheather
2022-08-18 02:28:22 +10:00
committed by GitHub
parent b1b2d1b2b2
commit a872330a8d
9 changed files with 619 additions and 36 deletions

View File

@ -1,12 +1,8 @@
package cli
import (
"time"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/spf13/cobra"
"github.com/coder/coder/cli/cliui"
"github.com/coder/coder/codersdk"
)
@ -25,26 +21,3 @@ func users() *cobra.Command {
)
return cmd
}
// displayUsers will return a table displaying all users passed in.
// filterColumns must be a subset of the user fields and will determine which
// columns to display
func displayUsers(filterColumns []string, users ...codersdk.User) string {
tableWriter := cliui.Table()
header := table.Row{"id", "username", "email", "created at", "status"}
tableWriter.AppendHeader(header)
tableWriter.SetColumnConfigs(cliui.FilterTableColumns(header, filterColumns))
tableWriter.SortBy([]table.SortBy{{
Name: "username",
}})
for _, user := range users {
tableWriter.AppendRow(table.Row{
user.ID.String(),
user.Username,
user.Email,
user.CreatedAt.Format(time.Stamp),
user.Status,
})
}
return tableWriter.Render()
}