|
| 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 template |
| 19 | + |
| 20 | +import ( |
| 21 | + "fmt" |
| 22 | + "os" |
| 23 | + |
| 24 | + "github.com/arduino/arduino-cli/cli/errorcodes" |
| 25 | + "github.com/arduino/arduino-cli/cli/feedback" |
| 26 | + "github.com/arduino/arduino-cloud-cli/command/template" |
| 27 | + "github.com/arduino/arduino-cloud-cli/config" |
| 28 | + "github.com/spf13/cobra" |
| 29 | +) |
| 30 | + |
| 31 | +type applyFlags struct { |
| 32 | + templateId string |
| 33 | + templateName string |
| 34 | +} |
| 35 | + |
| 36 | +func initTemplateApplyCommand() *cobra.Command { |
| 37 | + flags := &applyFlags{} |
| 38 | + applyCommand := &cobra.Command{ |
| 39 | + Use: "apply", |
| 40 | + Short: "Apply custom template", |
| 41 | + Long: "Given a template, apply it and create all the resources defined in it", |
| 42 | + Run: func(cmd *cobra.Command, args []string) { |
| 43 | + if err := runTemplateApplyCommand(flags); err != nil { |
| 44 | + feedback.Errorf("Error during template apply: %v", err) |
| 45 | + os.Exit(errorcodes.ErrGeneric) |
| 46 | + } |
| 47 | + }, |
| 48 | + } |
| 49 | + |
| 50 | + applyCommand.Flags().StringVarP(&flags.templateId, "template-id", "t", "", "Template id") |
| 51 | + applyCommand.Flags().StringVarP(&flags.templateName, "name", "n", "", "Name") |
| 52 | + |
| 53 | + applyCommand.MarkFlagRequired("template-id") |
| 54 | + applyCommand.MarkFlagRequired("name") |
| 55 | + |
| 56 | + return applyCommand |
| 57 | +} |
| 58 | + |
| 59 | +func runTemplateApplyCommand(flags *applyFlags) error { |
| 60 | + cred, err := config.RetrieveCredentials() |
| 61 | + if err != nil { |
| 62 | + return fmt.Errorf("retrieving credentials: %w", err) |
| 63 | + } |
| 64 | + return template.ApplyCustomTemplates(cred, flags.templateId) |
| 65 | +} |
| 66 | + |
| 67 | +/* |
| 68 | +
|
| 69 | +curl --location --request PUT 'http://localhost:9000/iot/v1/templates' \ |
| 70 | +--header 'Accept: application/yaml' \ |
| 71 | +--header 'Content-Type: application/json' \ |
| 72 | +--header 'Authorization: ' \ |
| 73 | +--data '{ |
| 74 | + "template_name": "home", |
| 75 | + "custom_template_id": "d864f20e-dcf4-4c8a-b3e7-f1bfffe86f60", |
| 76 | + "things_options": { |
| 77 | + "home-3a06e": { |
| 78 | + "device_id": "08d75172-335e-4cb9-b401-83eb4db213fb", |
| 79 | + "secrets": { |
| 80 | + "SECRET_SSID": "asdas", |
| 81 | + "SECRET_OPTIONAL_PASS": "asdsad" |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | +}' |
| 86 | +
|
| 87 | +*/ |
0 commit comments