github.com/arduino/arduino-cloud-cli@v0.0.0-20240517070944-e7a449561083/cli/device/listfrequency.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  
    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/device"
    29  	"github.com/arduino/arduino-cloud-cli/config"
    30  	"github.com/sirupsen/logrus"
    31  	"github.com/spf13/cobra"
    32  )
    33  
    34  func initListFrequencyPlansCommand() *cobra.Command {
    35  	listCommand := &cobra.Command{
    36  		Use:   "list-frequency-plans",
    37  		Short: "List LoRa frequency plans",
    38  		Long:  "List all supported LoRa frequency plans",
    39  		Run: func(cmd *cobra.Command, args []string) {
    40  			if err := runListFrequencyPlansCommand(); err != nil {
    41  				feedback.Errorf("Error during device list-frequency-plans: %v", err)
    42  				os.Exit(errorcodes.ErrGeneric)
    43  			}
    44  		},
    45  	}
    46  	return listCommand
    47  }
    48  
    49  func runListFrequencyPlansCommand() error {
    50  	logrus.Info("Listing supported frequency plans")
    51  
    52  	cred, err := config.RetrieveCredentials()
    53  	if err != nil {
    54  		return fmt.Errorf("retrieving credentials: %w", err)
    55  	}
    56  
    57  	freqs, err := device.ListFrequencyPlans(context.TODO(), cred)
    58  	if err != nil {
    59  		return err
    60  	}
    61  
    62  	feedback.PrintResult(listFrequencyPlansResult{freqs})
    63  	return nil
    64  }
    65  
    66  type listFrequencyPlansResult struct {
    67  	freqs []device.FrequencyPlanInfo
    68  }
    69  
    70  func (r listFrequencyPlansResult) Data() interface{} {
    71  	return r.freqs
    72  }
    73  
    74  func (r listFrequencyPlansResult) String() string {
    75  	if len(r.freqs) == 0 {
    76  		return "No frequency plan found."
    77  	}
    78  	t := table.New()
    79  	t.SetHeader("ID", "Name", "Advanced")
    80  	for _, freq := range r.freqs {
    81  		t.AddRow(
    82  			freq.ID,
    83  			freq.Name,
    84  			freq.Advanced,
    85  		)
    86  	}
    87  	return t.Render()
    88  }