github.com/arduino/arduino-cloud-cli@v0.0.0-20240517070944-e7a449561083/cli/device/listfqbn.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  	"os"
    23  
    24  	"github.com/arduino/arduino-cli/cli/errorcodes"
    25  	"github.com/arduino/arduino-cli/cli/feedback"
    26  	"github.com/arduino/arduino-cli/table"
    27  	"github.com/arduino/arduino-cloud-cli/command/device"
    28  	"github.com/sirupsen/logrus"
    29  	"github.com/spf13/cobra"
    30  )
    31  
    32  func initListFQBNCommand() *cobra.Command {
    33  	listFQBNCommand := &cobra.Command{
    34  		Use:   "list-fqbn",
    35  		Short: "List supported FQBN",
    36  		Long:  "List all the FQBN supported by Arduino IoT Cloud",
    37  		Run: func(cmd *cobra.Command, args []string) {
    38  			if err := runListFQBNCommand(); err != nil {
    39  				feedback.Errorf("Error during device list-fqbn: %v", err)
    40  				os.Exit(errorcodes.ErrGeneric)
    41  			}
    42  		},
    43  	}
    44  	return listFQBNCommand
    45  }
    46  
    47  func runListFQBNCommand() error {
    48  	logrus.Info("Listing supported FQBN")
    49  
    50  	fqbn, err := device.ListFQBN(context.TODO())
    51  	if err != nil {
    52  		return err
    53  	}
    54  
    55  	feedback.PrintResult(listFQBNResult{fqbn})
    56  	return nil
    57  }
    58  
    59  type listFQBNResult struct {
    60  	fqbn []device.FQBNInfo
    61  }
    62  
    63  func (r listFQBNResult) Data() interface{} {
    64  	return r.fqbn
    65  }
    66  
    67  func (r listFQBNResult) String() string {
    68  	if len(r.fqbn) == 0 {
    69  		return "No FQBN."
    70  	}
    71  	t := table.New()
    72  	t.SetHeader("Name", "FQBN")
    73  	for _, f := range r.fqbn {
    74  		t.AddRow(
    75  			f.Name,
    76  			f.Value,
    77  		)
    78  	}
    79  	return t.Render()
    80  }