github.com/arduino/arduino-cloud-cli@v0.0.0-20240517070944-e7a449561083/cli/dashboard/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 dashboard 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/dashboard" 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 override map[string]string 38 } 39 40 func initCreateCommand() *cobra.Command { 41 flags := &createFlags{} 42 createCommand := &cobra.Command{ 43 Use: "create", 44 Short: "Create a dashboard from a template", 45 Long: "Create a dashboard from a template for Arduino IoT Cloud", 46 Run: func(cmd *cobra.Command, args []string) { 47 if err := runCreateCommand(flags); err != nil { 48 feedback.Errorf("Error during dashboard create: %v", err) 49 os.Exit(errorcodes.ErrGeneric) 50 } 51 }, 52 } 53 createCommand.Flags().StringVarP(&flags.name, "name", "n", "", "Dashboard name") 54 createCommand.Flags().StringVarP(&flags.template, "template", "t", "", 55 "File containing a dashboard template, JSON and YAML format are supported", 56 ) 57 createCommand.Flags().StringToStringVarP(&flags.override, "override", "o", nil, 58 "Map stating the items to be overridden. Ex: 'thing-0=xxxxxxxx,thing-1=yyyyyyyy'") 59 60 createCommand.MarkFlagRequired("template") 61 return createCommand 62 } 63 64 func runCreateCommand(flags *createFlags) error { 65 logrus.Infof("Creating dashboard 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 := &dashboard.CreateParams{ 73 Template: flags.template, 74 Override: flags.override, 75 } 76 if flags.name != "" { 77 params.Name = &flags.name 78 } 79 80 dashboard, err := dashboard.Create(context.TODO(), params, cred) 81 if err != nil { 82 return err 83 } 84 85 feedback.PrintResult(createResult{dashboard}) 86 return nil 87 } 88 89 type createResult struct { 90 dashboard *dashboard.DashboardInfo 91 } 92 93 func (r createResult) Data() interface{} { 94 return r.dashboard 95 } 96 97 func (r createResult) String() string { 98 return fmt.Sprintf( 99 "name: %s\nid: %s\nupdated_at: %s\nwidgets: %s", 100 r.dashboard.Name, 101 r.dashboard.ID, 102 r.dashboard.UpdatedAt, 103 strings.Join(r.dashboard.Widgets, ", "), 104 ) 105 }