github.com/arduino/arduino-cloud-cli@v0.0.0-20240517070944-e7a449561083/cli/thing/clone.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 cloneFlags struct {
    35  	name    string
    36  	cloneID string
    37  }
    38  
    39  func initCloneCommand() *cobra.Command {
    40  	flags := &cloneFlags{}
    41  	cloneCommand := &cobra.Command{
    42  		Use:   "clone",
    43  		Short: "Clone a thing",
    44  		Long:  "Clone a thing for Arduino IoT Cloud",
    45  		Run: func(cmd *cobra.Command, args []string) {
    46  			if err := runCloneCommand(flags); err != nil {
    47  				feedback.Errorf("Error during thing clone: %v", err)
    48  				os.Exit(errorcodes.ErrGeneric)
    49  			}
    50  		},
    51  	}
    52  	cloneCommand.Flags().StringVarP(&flags.name, "name", "n", "", "Thing name")
    53  	cloneCommand.Flags().StringVarP(&flags.cloneID, "clone-id", "c", "", "ID of Thing to be cloned")
    54  	cloneCommand.MarkFlagRequired("name")
    55  	cloneCommand.MarkFlagRequired("clone-id")
    56  	return cloneCommand
    57  }
    58  
    59  func runCloneCommand(flags *cloneFlags) error {
    60  	logrus.Infof("Cloning thing %s into a new thing called %s", flags.cloneID, flags.name)
    61  
    62  	cred, err := config.RetrieveCredentials()
    63  	if err != nil {
    64  		return fmt.Errorf("retrieving credentials: %w", err)
    65  	}
    66  
    67  	params := &thing.CloneParams{
    68  		Name:    flags.name,
    69  		CloneID: flags.cloneID,
    70  	}
    71  
    72  	thing, err := thing.Clone(context.TODO(), params, cred)
    73  	if err != nil {
    74  		return err
    75  	}
    76  
    77  	feedback.PrintResult(cloneResult{thing})
    78  	return nil
    79  }
    80  
    81  type cloneResult struct {
    82  	thing *thing.ThingInfo
    83  }
    84  
    85  func (r cloneResult) Data() interface{} {
    86  	return r.thing
    87  }
    88  
    89  func (r cloneResult) String() string {
    90  	return fmt.Sprintf(
    91  		"name: %s\nid: %s\ndevice_id: %s\nvariables: %s",
    92  		r.thing.Name,
    93  		r.thing.ID,
    94  		r.thing.DeviceID,
    95  		strings.Join(r.thing.Variables, ", "),
    96  	)
    97  }