github.com/arduino/arduino-cloud-cli@v0.0.0-20240517070944-e7a449561083/cli/dashboard/list.go (about)

     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  	"context"
    22  	"fmt"
    23  	"math"
    24  	"os"
    25  	"strings"
    26  
    27  	"github.com/arduino/arduino-cli/cli/errorcodes"
    28  	"github.com/arduino/arduino-cli/cli/feedback"
    29  	"github.com/arduino/arduino-cli/table"
    30  	"github.com/arduino/arduino-cloud-cli/command/dashboard"
    31  	"github.com/arduino/arduino-cloud-cli/config"
    32  	"github.com/sirupsen/logrus"
    33  	"github.com/spf13/cobra"
    34  )
    35  
    36  const (
    37  	widgetsPerRow = 3
    38  )
    39  
    40  type listFlags struct {
    41  	showWidgets bool
    42  }
    43  
    44  func initListCommand() *cobra.Command {
    45  	flags := &listFlags{}
    46  	listCommand := &cobra.Command{
    47  		Use:   "list",
    48  		Short: "List dashboards",
    49  		Long:  "List dashboards on Arduino IoT Cloud",
    50  		Run: func(cmd *cobra.Command, args []string) {
    51  			if err := runListCommand(flags); err != nil {
    52  				feedback.Errorf("Error during dashboard list: %v", err)
    53  				os.Exit(errorcodes.ErrGeneric)
    54  			}
    55  		},
    56  	}
    57  
    58  	listCommand.Flags().BoolVarP(&flags.showWidgets, "show-widgets", "s", false, "Show names of dashboard widgets")
    59  	return listCommand
    60  }
    61  
    62  func runListCommand(flags *listFlags) error {
    63  	logrus.Info("Listing dashboards")
    64  
    65  	cred, err := config.RetrieveCredentials()
    66  	if err != nil {
    67  		return fmt.Errorf("retrieving credentials: %w", err)
    68  	}
    69  
    70  	dash, err := dashboard.List(context.TODO(), cred)
    71  	if err != nil {
    72  		return err
    73  	}
    74  
    75  	feedback.PrintResult(listResult{dashboards: dash, showWidgets: flags.showWidgets})
    76  	return nil
    77  }
    78  
    79  type listResult struct {
    80  	dashboards  []dashboard.DashboardInfo
    81  	showWidgets bool
    82  }
    83  
    84  func (r listResult) Data() interface{} {
    85  	return r.dashboards
    86  }
    87  
    88  func (r listResult) String() string {
    89  	if len(r.dashboards) == 0 {
    90  		return "No dashboard found."
    91  	}
    92  	t := table.New()
    93  
    94  	head := []interface{}{"Name", "ID", "UpdatedAt"}
    95  	if r.showWidgets {
    96  		head = append(head, "Widgets")
    97  	}
    98  	t.SetHeader(head...)
    99  
   100  	for _, dash := range r.dashboards {
   101  		row := []interface{}{dash.Name, dash.ID, dash.UpdatedAt}
   102  
   103  		if r.showWidgets {
   104  			// Limit number of widgets per row.
   105  			if len(dash.Widgets) > widgetsPerRow {
   106  				row = append(row, strings.Join(dash.Widgets[:widgetsPerRow], ", "))
   107  				dash.Widgets = dash.Widgets[widgetsPerRow:]
   108  			} else {
   109  				row = append(row, strings.Join(dash.Widgets, ", "))
   110  				dash.Widgets = nil
   111  			}
   112  		}
   113  		t.AddRow(row...)
   114  
   115  		// Print remaining widgets in new rows
   116  		if r.showWidgets {
   117  			for len(dash.Widgets) > 0 {
   118  				row := []interface{}{"", "", ""}
   119  				l := int(math.Min(float64(len(dash.Widgets)), widgetsPerRow))
   120  				row = append(row, strings.Join(dash.Widgets[:l], ", "))
   121  				dash.Widgets = dash.Widgets[l:]
   122  				t.AddRow(row...)
   123  			}
   124  		}
   125  	}
   126  	return t.Render()
   127  }