|
| 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 | + "fmt" |
| 22 | + "os" |
| 23 | + "strings" |
| 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/dashboard" |
| 28 | + "github.com/sirupsen/logrus" |
| 29 | + "github.com/spf13/cobra" |
| 30 | +) |
| 31 | + |
| 32 | +var createFlags struct { |
| 33 | + name string |
| 34 | + template string |
| 35 | + override map[string]string |
| 36 | +} |
| 37 | + |
| 38 | +func initCreateCommand() *cobra.Command { |
| 39 | + createCommand := &cobra.Command{ |
| 40 | + Use: "create", |
| 41 | + Short: "Create a dashboard from a template", |
| 42 | + Long: "Create a dashboard from a template for Arduino IoT Cloud", |
| 43 | + Run: runCreateCommand, |
| 44 | + } |
| 45 | + createCommand.Flags().StringVarP(&createFlags.name, "name", "n", "", "Dashboard name") |
| 46 | + createCommand.Flags().StringVarP(&createFlags.template, "template", "t", "", |
| 47 | + "File containing a dashboard template, JSON and YAML format are supported", |
| 48 | + ) |
| 49 | + createCommand.Flags().StringToStringVarP(&createFlags.override, "override", "o", nil, |
| 50 | + "Map stating the items to be overridden. Ex: 'thing-0=xxxxxxxx,thing-1=yyyyyyyy'") |
| 51 | + |
| 52 | + createCommand.MarkFlagRequired("template") |
| 53 | + return createCommand |
| 54 | +} |
| 55 | + |
| 56 | +func runCreateCommand(cmd *cobra.Command, args []string) { |
| 57 | + logrus.Infof("Creating dashboard from template %s\n", createFlags.template) |
| 58 | + |
| 59 | + params := &dashboard.CreateParams{ |
| 60 | + Template: createFlags.template, |
| 61 | + Override: createFlags.override, |
| 62 | + } |
| 63 | + if createFlags.name != "" { |
| 64 | + params.Name = &createFlags.name |
| 65 | + } |
| 66 | + |
| 67 | + dashboard, err := dashboard.Create(params) |
| 68 | + if err != nil { |
| 69 | + feedback.Errorf("Error during dashboard create: %v", err) |
| 70 | + os.Exit(errorcodes.ErrGeneric) |
| 71 | + } |
| 72 | + |
| 73 | + feedback.PrintResult(createResult{dashboard}) |
| 74 | +} |
| 75 | + |
| 76 | +type createResult struct { |
| 77 | + dashboard *dashboard.DashboardInfo |
| 78 | +} |
| 79 | + |
| 80 | +func (r createResult) Data() interface{} { |
| 81 | + return r.dashboard |
| 82 | +} |
| 83 | + |
| 84 | +func (r createResult) String() string { |
| 85 | + return fmt.Sprintf( |
| 86 | + "name: %s\nid: %s\nupdated_at: %s\nwidgets: %s", |
| 87 | + r.dashboard.Name, |
| 88 | + r.dashboard.ID, |
| 89 | + r.dashboard.UpdatedAt, |
| 90 | + strings.Join(r.dashboard.Widgets, ", "), |
| 91 | + ) |
| 92 | +} |
0 commit comments