github.com/arduino/arduino-cloud-cli@v0.0.0-20240517070944-e7a449561083/cli/device/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 device
    19  
    20  import (
    21  	"context"
    22  	"fmt"
    23  	"os"
    24  	"strings"
    25  
    26  	"github.com/arduino/arduino-cli/cli/errorcodes"
    27  	"github.com/arduino/arduino-cli/cli/feedback"
    28  	"github.com/arduino/arduino-cli/table"
    29  	"github.com/arduino/arduino-cloud-cli/command/device"
    30  	"github.com/arduino/arduino-cloud-cli/config"
    31  	"github.com/sirupsen/logrus"
    32  	"github.com/spf13/cobra"
    33  )
    34  
    35  type listFlags struct {
    36  	tags      map[string]string
    37  	deviceIds string
    38  }
    39  
    40  func initListCommand() *cobra.Command {
    41  	flags := &listFlags{}
    42  	listCommand := &cobra.Command{
    43  		Use:   "list",
    44  		Short: "List devices",
    45  		Long:  "List devices on Arduino IoT Cloud",
    46  		Run: func(cmd *cobra.Command, args []string) {
    47  			if err := runListCommand(flags); err != nil {
    48  				feedback.Errorf("Error during device list: %v", err)
    49  				os.Exit(errorcodes.ErrGeneric)
    50  			}
    51  		},
    52  	}
    53  	listCommand.Flags().StringToStringVar(
    54  		&flags.tags,
    55  		"tags",
    56  		nil,
    57  		"Comma-separated list of tags with format <key>=<value>.\n"+
    58  			"List only devices that match the provided tags.",
    59  	)
    60  	listCommand.Flags().StringVarP(&flags.deviceIds, "device-ids", "d", "", "Comma separated list of Device IDs")
    61  	return listCommand
    62  }
    63  
    64  func runListCommand(flags *listFlags) error {
    65  	logrus.Info("Listing devices")
    66  
    67  	cred, err := config.RetrieveCredentials()
    68  	if err != nil {
    69  		return fmt.Errorf("retrieving credentials: %w", err)
    70  	}
    71  
    72  	params := &device.ListParams{Tags: flags.tags, DeviceIds: flags.deviceIds}
    73  	devs, err := device.List(context.TODO(), params, cred)
    74  	if err != nil {
    75  		return err
    76  	}
    77  
    78  	feedback.PrintResult(listResult{devs})
    79  	return nil
    80  }
    81  
    82  type listResult struct {
    83  	devices []device.DeviceInfo
    84  }
    85  
    86  func (r listResult) Data() interface{} {
    87  	return r.devices
    88  }
    89  
    90  func (r listResult) String() string {
    91  	if len(r.devices) == 0 {
    92  		return "No devices found."
    93  	}
    94  	t := table.New()
    95  	t.SetHeader("Name", "ID", "Board", "FQBN", "SerialNumber", "Tags")
    96  	for _, device := range r.devices {
    97  		t.AddRow(
    98  			device.Name,
    99  			device.ID,
   100  			device.Board,
   101  			device.FQBN,
   102  			device.Serial,
   103  			strings.Join(device.Tags, ","),
   104  		)
   105  	}
   106  	return t.Render()
   107  }