github.com/arduino/arduino-cloud-cli@v0.0.0-20240517070944-e7a449561083/cli/thing/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 thing
    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-cloud-cli/command/thing"
    29  	"github.com/arduino/arduino-cloud-cli/config"
    30  	"github.com/sirupsen/logrus"
    31  	"github.com/spf13/cobra"
    32  )
    33  
    34  type createFlags struct {
    35  	name     string
    36  	template string
    37  }
    38  
    39  func initCreateCommand() *cobra.Command {
    40  	flags := &createFlags{}
    41  	createCommand := &cobra.Command{
    42  		Use:   "create",
    43  		Short: "Create a thing from a template",
    44  		Long:  "Create a thing from a template for Arduino IoT Cloud",
    45  		Run: func(cmd *cobra.Command, args []string) {
    46  			if err := runCreateCommand(flags); err != nil {
    47  				feedback.Errorf("Error during thing create: %v", err)
    48  				os.Exit(errorcodes.ErrGeneric)
    49  			}
    50  		},
    51  	}
    52  	createCommand.Flags().StringVarP(&flags.name, "name", "n", "", "Thing name")
    53  	createCommand.Flags().StringVarP(
    54  		&flags.template,
    55  		"template",
    56  		"t",
    57  		"",
    58  		"File containing a thing template, JSON and YAML format are supported",
    59  	)
    60  	createCommand.MarkFlagRequired("template")
    61  	return createCommand
    62  }
    63  
    64  func runCreateCommand(flags *createFlags) error {
    65  	logrus.Infof("Creating thing from template %s", flags.template)
    66  
    67  	cred, err := config.RetrieveCredentials()
    68  	if err != nil {
    69  		return fmt.Errorf("retrieving credentials: %w", err)
    70  	}
    71  
    72  	params := &thing.CreateParams{
    73  		Template: flags.template,
    74  	}
    75  	if flags.name != "" {
    76  		params.Name = &flags.name
    77  	}
    78  
    79  	thing, err := thing.Create(context.TODO(), params, cred)
    80  	if err != nil {
    81  		return err
    82  	}
    83  
    84  	feedback.PrintResult(createResult{thing})
    85  	return nil
    86  }
    87  
    88  type createResult struct {
    89  	thing *thing.ThingInfo
    90  }
    91  
    92  func (r createResult) Data() interface{} {
    93  	return r.thing
    94  }
    95  
    96  func (r createResult) String() string {
    97  	return fmt.Sprintf(
    98  		"name: %s\nid: %s\ndevice_id: %s\nvariables: %s",
    99  		r.thing.Name,
   100  		r.thing.ID,
   101  		r.thing.DeviceID,
   102  		strings.Join(r.thing.Variables, ", "),
   103  	)
   104  }