github.com/arduino/arduino-cloud-cli@v0.0.0-20240517070944-e7a449561083/cli/device/creategeneric.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 createGenericFlags struct { 35 name string 36 fqbn string 37 } 38 39 func initCreateGenericCommand() *cobra.Command { 40 flags := &createGenericFlags{} 41 createGenericCommand := &cobra.Command{ 42 Use: "create-generic", 43 Short: "Create a generic device with password authentication - without secure element - WARNING: less secure", 44 Long: "Create a generic device with password authentication for Arduino IoT Cloud - without secure element - WARNING: less secure", 45 Run: func(cmd *cobra.Command, args []string) { 46 if err := runCreateGenericCommand(flags); err != nil { 47 feedback.Errorf("Error during device create-generic: %v", err) 48 os.Exit(errorcodes.ErrGeneric) 49 } 50 }, 51 } 52 createGenericCommand.Flags().StringVarP(&flags.name, "name", "n", "", "Device name") 53 createGenericCommand.Flags().StringVarP(&flags.fqbn, "fqbn", "b", "generic:generic:generic", "Device fqbn") 54 createGenericCommand.MarkFlagRequired("name") 55 return createGenericCommand 56 } 57 58 func runCreateGenericCommand(flags *createGenericFlags) error { 59 logrus.Infof("Creating generic device with name %s", flags.name) 60 61 cred, err := config.RetrieveCredentials() 62 if err != nil { 63 return fmt.Errorf("retrieving credentials: %w", err) 64 } 65 66 params := &device.CreateGenericParams{ 67 Name: flags.name, 68 FQBN: flags.fqbn, 69 } 70 71 ctx, cancel := cleanup.InterruptableContext(context.Background()) 72 defer cancel() 73 74 dev, err := device.CreateGeneric(ctx, params, cred) 75 if err != nil { 76 return err 77 } 78 79 feedback.PrintResult(createGenericResult{dev}) 80 return nil 81 } 82 83 type createGenericResult struct { 84 device *device.DeviceGenericInfo 85 } 86 87 func (r createGenericResult) Data() interface{} { 88 return r.device 89 } 90 91 func (r createGenericResult) String() string { 92 return fmt.Sprintf( 93 "id: %s\nsecret_key: %s\nname: %s\nboard: %s\nserial_number: %s\nfqbn: %s", 94 r.device.ID, 95 r.device.Password, 96 r.device.Name, 97 r.device.Board, 98 r.device.Serial, 99 r.device.FQBN, 100 ) 101 }