Skip to content

Add dashboard list command #45

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Oct 12, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add flag to show widgets
  • Loading branch information
polldo committed Oct 11, 2021
commit db1eebb0a5aecd187e158c0decc190be9c462ff8
45 changes: 25 additions & 20 deletions cli/dashboard/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const (
)

var listFlags struct {
showSharing bool
showWidgets bool
}

func initListCommand() *cobra.Command {
Expand All @@ -46,7 +46,7 @@ func initListCommand() *cobra.Command {
Run: runListCommand,
}

listCommand.Flags().BoolVarP(&listFlags.showSharing, "show-sharing", "s", false, "Show dashboard sharing information")
listCommand.Flags().BoolVarP(&listFlags.showWidgets, "show-widgets", "s", false, "Show names of dashboard widgets")
return listCommand
}

Expand Down Expand Up @@ -76,31 +76,36 @@ func (r listResult) String() string {
}
t := table.New()

head := []interface{}{"Name", "ID", "Widgets", "UpdatedAt"}
head := []interface{}{"Name", "ID", "UpdatedAt"}
if listFlags.showWidgets {
head = append(head, "Widgets")
}
t.SetHeader(head...)

for _, dash := range r.dashboards {
row := []interface{}{dash.Name, dash.ID}

// Limit number of widgets per row.
if len(dash.Widgets) > widgetsPerRow {
row = append(row, strings.Join(dash.Widgets[:widgetsPerRow], ", "))
dash.Widgets = dash.Widgets[widgetsPerRow:]
} else {
row = append(row, strings.Join(dash.Widgets, ", "))
dash.Widgets = nil
row := []interface{}{dash.Name, dash.ID, dash.UpdatedAt}

if listFlags.showWidgets {
// Limit number of widgets per row.
if len(dash.Widgets) > widgetsPerRow {
row = append(row, strings.Join(dash.Widgets[:widgetsPerRow], ", "))
dash.Widgets = dash.Widgets[widgetsPerRow:]
} else {
row = append(row, strings.Join(dash.Widgets, ", "))
dash.Widgets = nil
}
}
row = append(row, dash.UpdatedAt)
t.AddRow(row...)

// Print remaining widgets in new rows
for len(dash.Widgets) > 0 {
row := []interface{}{"", ""}
l := int(math.Min(float64(len(dash.Widgets)), widgetsPerRow))
row = append(row, strings.Join(dash.Widgets[:l], ", "))
dash.Widgets = dash.Widgets[l:]
row = append(row, "")
t.AddRow(row...)
if listFlags.showWidgets {
for len(dash.Widgets) > 0 {
row := []interface{}{"", "", ""}
l := int(math.Min(float64(len(dash.Widgets)), widgetsPerRow))
row = append(row, strings.Join(dash.Widgets[:l], ", "))
dash.Widgets = dash.Widgets[l:]
t.AddRow(row...)
}
}
}
return t.Render()
Expand Down