|
| 1 | +// This file is part of arduino-cloud-cli. |
| 2 | +// |
| 3 | +// Copyright (C) 2021 ARDUINO SA (http://www.arduino.cc/) |
| 4 | +// |
| 5 | +// This program is free software: you can redistribute it and/or modify |
| 6 | +// it under the terms of the GNU Affero General Public License as published |
| 7 | +// by the Free Software Foundation, either version 3 of the License, or |
| 8 | +// (at your option) any later version. |
| 9 | +// |
| 10 | +// This program is distributed in the hope that it will be useful, |
| 11 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +// GNU Affero General Public License for more details. |
| 14 | +// |
| 15 | +// You should have received a copy of the GNU Affero General Public License |
| 16 | +// along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 17 | + |
| 18 | +package dashboard |
| 19 | + |
| 20 | +import ( |
| 21 | + "math" |
| 22 | + "os" |
| 23 | + "strings" |
| 24 | + |
| 25 | + "github.com/arduino/arduino-cli/cli/errorcodes" |
| 26 | + "github.com/arduino/arduino-cli/cli/feedback" |
| 27 | + "github.com/arduino/arduino-cli/table" |
| 28 | + "github.com/arduino/arduino-cloud-cli/command/dashboard" |
| 29 | + "github.com/sirupsen/logrus" |
| 30 | + "github.com/spf13/cobra" |
| 31 | +) |
| 32 | + |
| 33 | +const ( |
| 34 | + widgetsPerRow = 3 |
| 35 | +) |
| 36 | + |
| 37 | +var listFlags struct { |
| 38 | + showWidgets bool |
| 39 | +} |
| 40 | + |
| 41 | +func initListCommand() *cobra.Command { |
| 42 | + listCommand := &cobra.Command{ |
| 43 | + Use: "list", |
| 44 | + Short: "List dashboards", |
| 45 | + Long: "List dashboards on Arduino IoT Cloud", |
| 46 | + Run: runListCommand, |
| 47 | + } |
| 48 | + |
| 49 | + listCommand.Flags().BoolVarP(&listFlags.showWidgets, "show-widgets", "s", false, "Show names of dashboard widgets") |
| 50 | + return listCommand |
| 51 | +} |
| 52 | + |
| 53 | +func runListCommand(cmd *cobra.Command, args []string) { |
| 54 | + logrus.Info("Listing dashboards") |
| 55 | + |
| 56 | + dash, err := dashboard.List() |
| 57 | + if err != nil { |
| 58 | + feedback.Errorf("Error during dashboard list: %v", err) |
| 59 | + os.Exit(errorcodes.ErrGeneric) |
| 60 | + } |
| 61 | + |
| 62 | + feedback.PrintResult(listResult{dash}) |
| 63 | +} |
| 64 | + |
| 65 | +type listResult struct { |
| 66 | + dashboards []dashboard.DashboardInfo |
| 67 | +} |
| 68 | + |
| 69 | +func (r listResult) Data() interface{} { |
| 70 | + return r.dashboards |
| 71 | +} |
| 72 | + |
| 73 | +func (r listResult) String() string { |
| 74 | + if len(r.dashboards) == 0 { |
| 75 | + return "No dashboard found." |
| 76 | + } |
| 77 | + t := table.New() |
| 78 | + |
| 79 | + head := []interface{}{"Name", "ID", "UpdatedAt"} |
| 80 | + if listFlags.showWidgets { |
| 81 | + head = append(head, "Widgets") |
| 82 | + } |
| 83 | + t.SetHeader(head...) |
| 84 | + |
| 85 | + for _, dash := range r.dashboards { |
| 86 | + row := []interface{}{dash.Name, dash.ID, dash.UpdatedAt} |
| 87 | + |
| 88 | + if listFlags.showWidgets { |
| 89 | + // Limit number of widgets per row. |
| 90 | + if len(dash.Widgets) > widgetsPerRow { |
| 91 | + row = append(row, strings.Join(dash.Widgets[:widgetsPerRow], ", ")) |
| 92 | + dash.Widgets = dash.Widgets[widgetsPerRow:] |
| 93 | + } else { |
| 94 | + row = append(row, strings.Join(dash.Widgets, ", ")) |
| 95 | + dash.Widgets = nil |
| 96 | + } |
| 97 | + } |
| 98 | + t.AddRow(row...) |
| 99 | + |
| 100 | + // Print remaining widgets in new rows |
| 101 | + if listFlags.showWidgets { |
| 102 | + for len(dash.Widgets) > 0 { |
| 103 | + row := []interface{}{"", "", ""} |
| 104 | + l := int(math.Min(float64(len(dash.Widgets)), widgetsPerRow)) |
| 105 | + row = append(row, strings.Join(dash.Widgets[:l], ", ")) |
| 106 | + dash.Widgets = dash.Widgets[l:] |
| 107 | + t.AddRow(row...) |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + return t.Render() |
| 112 | +} |
0 commit comments