|
| 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 | + "os" |
| 22 | + |
| 23 | + "github.com/arduino/arduino-cli/cli/errorcodes" |
| 24 | + "github.com/arduino/arduino-cli/cli/feedback" |
| 25 | + "github.com/arduino/arduino-cloud-cli/command/dashboard" |
| 26 | + "github.com/sirupsen/logrus" |
| 27 | + "github.com/spf13/cobra" |
| 28 | +) |
| 29 | + |
| 30 | +var extractFlags struct { |
| 31 | + id string |
| 32 | + outfile string |
| 33 | + format string |
| 34 | +} |
| 35 | + |
| 36 | +func initExtractCommand() *cobra.Command { |
| 37 | + extractCommand := &cobra.Command{ |
| 38 | + Use: "extract", |
| 39 | + Short: "Extract a template from a dashboard", |
| 40 | + Long: "Extract a template from a Arduino IoT Cloud dashboard and save it in a file", |
| 41 | + Run: runExtractCommand, |
| 42 | + } |
| 43 | + extractCommand.Flags().StringVarP(&extractFlags.id, "id", "i", "", "Dashboard ID") |
| 44 | + extractCommand.Flags().StringVarP(&extractFlags.outfile, "outfile", "o", "", "Template file destination path") |
| 45 | + extractCommand.Flags().StringVar( |
| 46 | + &extractFlags.format, |
| 47 | + "format", |
| 48 | + "yaml", |
| 49 | + "Format of template file, can be {json|yaml}. Default is 'yaml'", |
| 50 | + ) |
| 51 | + |
| 52 | + extractCommand.MarkFlagRequired("id") |
| 53 | + return extractCommand |
| 54 | +} |
| 55 | + |
| 56 | +func runExtractCommand(cmd *cobra.Command, args []string) { |
| 57 | + logrus.Infof("Extracting template from dashboard %s\n", extractFlags.id) |
| 58 | + |
| 59 | + params := &dashboard.ExtractParams{ |
| 60 | + ID: extractFlags.id, |
| 61 | + Format: extractFlags.format, |
| 62 | + } |
| 63 | + if extractFlags.outfile != "" { |
| 64 | + params.Outfile = &extractFlags.outfile |
| 65 | + } |
| 66 | + |
| 67 | + err := dashboard.Extract(params) |
| 68 | + if err != nil { |
| 69 | + feedback.Errorf("Error during template extraction: %v", err) |
| 70 | + os.Exit(errorcodes.ErrGeneric) |
| 71 | + } |
| 72 | + |
| 73 | + logrus.Info("Template successfully extracted") |
| 74 | +} |
0 commit comments