github.com/arduino/arduino-cloud-cli@v0.0.0-20240517070944-e7a449561083/cli/device/create.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-cloud-cli/command/device"
    28  	"github.com/arduino/arduino-cloud-cli/config"
    29  	"github.com/sirupsen/logrus"
    30  	"github.com/spf13/cobra"
    31  	"go.bug.st/cleanup"
    32  )
    33  
    34  type createFlags struct {
    35  	port  string
    36  	name  string
    37  	fqbn  string
    38  	ctype string
    39  }
    40  
    41  func initCreateCommand() *cobra.Command {
    42  	flags := &createFlags{}
    43  	createCommand := &cobra.Command{
    44  		Use:   "create",
    45  		Short: "Create a device provisioning the onboard secure element with a valid certificate",
    46  		Long:  "Create a device for Arduino IoT Cloud provisioning the onboard secure element with a valid certificate",
    47  		Run: func(cmd *cobra.Command, args []string) {
    48  			if err := runCreateCommand(flags); err != nil {
    49  				feedback.Errorf("Error during device create: %v", err)
    50  				os.Exit(errorcodes.ErrGeneric)
    51  			}
    52  		},
    53  	}
    54  	createCommand.Flags().StringVarP(&flags.port, "port", "p", "", "Device port")
    55  	createCommand.Flags().StringVarP(&flags.name, "name", "n", "", "Device name")
    56  	createCommand.Flags().StringVarP(&flags.fqbn, "fqbn", "b", "", "Device fqbn")
    57  	createCommand.Flags().StringVarP(&flags.ctype, "connection", "c", "", "Device connection type")
    58  	createCommand.MarkFlagRequired("name")
    59  	return createCommand
    60  }
    61  
    62  func runCreateCommand(flags *createFlags) error {
    63  	logrus.Infof("Creating device with name %s", flags.name)
    64  
    65  	cred, err := config.RetrieveCredentials()
    66  	if err != nil {
    67  		return fmt.Errorf("retrieving credentials: %w", err)
    68  	}
    69  
    70  	params := &device.CreateParams{
    71  		Name: flags.name,
    72  	}
    73  	if flags.ctype != "" {
    74  		params.ConnectionType = &flags.ctype
    75  	}
    76  	if flags.port != "" {
    77  		params.Port = &flags.port
    78  	}
    79  	if flags.fqbn != "" {
    80  		params.FQBN = &flags.fqbn
    81  	}
    82  
    83  	ctx, cancel := cleanup.InterruptableContext(context.Background())
    84  	defer cancel()
    85  
    86  	dev, err := device.Create(ctx, params, cred)
    87  	if err != nil {
    88  		return err
    89  	}
    90  
    91  	feedback.PrintResult(createResult{dev})
    92  	return nil
    93  }
    94  
    95  type createResult struct {
    96  	device *device.DeviceInfo
    97  }
    98  
    99  func (r createResult) Data() interface{} {
   100  	return r.device
   101  }
   102  
   103  func (r createResult) String() string {
   104  	return fmt.Sprintf(
   105  		"name: %s\nid: %s\nboard: %s\nserial_number: %s\nfqbn: %s",
   106  		r.device.Name,
   107  		r.device.ID,
   108  		r.device.Board,
   109  		r.device.Serial,
   110  		r.device.FQBN,
   111  	)
   112  }