|
| 1 | +// This file is part of arduino-cloud-cli. |
| 2 | +// |
| 3 | +// Copyright (C) 2024 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 | + "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/template" |
| 28 | + "github.com/arduino/arduino-cloud-cli/config" |
| 29 | + "github.com/spf13/cobra" |
| 30 | +) |
| 31 | + |
| 32 | +type applyFlags struct { |
| 33 | + templateId string |
| 34 | + templatePrefix string |
| 35 | + deviceId string |
| 36 | + netCredentials string |
| 37 | + applyOta bool |
| 38 | +} |
| 39 | + |
| 40 | +func initTemplateApplyCommand() *cobra.Command { |
| 41 | + flags := &applyFlags{} |
| 42 | + applyCommand := &cobra.Command{ |
| 43 | + Use: "apply", |
| 44 | + Short: "Apply custom template", |
| 45 | + Long: "Given a template, apply it and create all the resources defined in it", |
| 46 | + Run: func(cmd *cobra.Command, args []string) { |
| 47 | + if err := runTemplateApplyCommand(flags); err != nil { |
| 48 | + feedback.Errorf("Error during template apply: %v", err) |
| 49 | + os.Exit(errorcodes.ErrGeneric) |
| 50 | + } |
| 51 | + }, |
| 52 | + } |
| 53 | + |
| 54 | + applyCommand.Flags().StringVarP(&flags.templateId, "template-id", "t", "", "Template ID") |
| 55 | + applyCommand.Flags().StringVarP(&flags.templatePrefix, "prefix", "p", "", "Prefix to apply to the name of created resources") |
| 56 | + applyCommand.Flags().StringVarP(&flags.deviceId, "device-id", "d", "", "Device ID") |
| 57 | + applyCommand.Flags().StringVarP(&flags.netCredentials, "network-credentials", "n", "", "Comma separated network credentials used to configure device with format <key>=<value>. Supported values: SECRET_SSID | SECRET_OPTIONAL_PASS | SECRET_DEVICE_KEY") |
| 58 | + |
| 59 | + applyCommand.MarkFlagRequired("template-id") |
| 60 | + applyCommand.MarkFlagRequired("prefix") |
| 61 | + applyCommand.MarkFlagRequired("device-id") |
| 62 | + |
| 63 | + flags.applyOta = false |
| 64 | + |
| 65 | + return applyCommand |
| 66 | +} |
| 67 | + |
| 68 | +func runTemplateApplyCommand(flags *applyFlags) error { |
| 69 | + cred, err := config.RetrieveCredentials() |
| 70 | + if err != nil { |
| 71 | + return fmt.Errorf("retrieving credentials: %w", err) |
| 72 | + } |
| 73 | + |
| 74 | + deviceNetCredentials, err := parseCredentials(flags.netCredentials) |
| 75 | + if err != nil { |
| 76 | + return fmt.Errorf("parsing network credentials: %w", err) |
| 77 | + } |
| 78 | + |
| 79 | + return template.ApplyCustomTemplates(cred, flags.templateId, flags.deviceId, flags.templatePrefix, deviceNetCredentials, flags.applyOta) |
| 80 | +} |
| 81 | + |
| 82 | +func parseCredentials(credentials string) (map[string]string, error) { |
| 83 | + credentialsMap := make(map[string]string) |
| 84 | + if credentials == "" { |
| 85 | + return credentialsMap, nil |
| 86 | + } |
| 87 | + credentialsArray := strings.Split(credentials, ",") |
| 88 | + for _, credential := range credentialsArray { |
| 89 | + credentialArray := strings.Split(credential, "=") |
| 90 | + if len(credentialArray) != 2 { |
| 91 | + return nil, fmt.Errorf("invalid network credential: %s", credential) |
| 92 | + } |
| 93 | + credentialsMap[credentialArray[0]] = credentialArray[1] |
| 94 | + } |
| 95 | + return credentialsMap, nil |
| 96 | +} |
0 commit comments