-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WIRE-422] Custom templates support (#157)
- Loading branch information
1 parent
a17c934
commit 5ca90df
Showing
26 changed files
with
1,769 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
// This file is part of arduino-cloud-cli. | ||
// | ||
// Copyright (C) 2024 ARDUINO SA (http://www.arduino.cc/) | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as published | ||
// by the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
package device | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"github.com/arduino/arduino-cli/cli/errorcodes" | ||
"github.com/arduino/arduino-cli/cli/feedback" | ||
"github.com/arduino/arduino-cli/table" | ||
"github.com/arduino/arduino-cloud-cli/command/device" | ||
"github.com/arduino/arduino-cloud-cli/config" | ||
"github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
type showFlags struct { | ||
deviceId string | ||
} | ||
|
||
func initShowCommand() *cobra.Command { | ||
flags := &showFlags{} | ||
showCommand := &cobra.Command{ | ||
Use: "show", | ||
Short: "Show device properties", | ||
Long: "Show device properties on Arduino IoT Cloud", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
if err := runShowCommand(flags); err != nil { | ||
feedback.Errorf("Error during device show: %v", err) | ||
os.Exit(errorcodes.ErrGeneric) | ||
} | ||
}, | ||
} | ||
showCommand.Flags().StringVarP(&flags.deviceId, "device-id", "d", "", "device ID") | ||
|
||
showCommand.MarkFlagRequired("device-id") | ||
|
||
return showCommand | ||
} | ||
|
||
func runShowCommand(flags *showFlags) error { | ||
logrus.Info("Show device") | ||
|
||
cred, err := config.RetrieveCredentials() | ||
if err != nil { | ||
return fmt.Errorf("retrieving credentials: %w", err) | ||
} | ||
|
||
dev, _, err := device.Show(context.TODO(), flags.deviceId, cred) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
feedback.PrintResult(showResult{dev}) | ||
return nil | ||
} | ||
|
||
type showResult struct { | ||
device *device.DeviceInfo | ||
} | ||
|
||
func (r showResult) Data() interface{} { | ||
return r.device | ||
} | ||
|
||
func (r showResult) String() string { | ||
if r.device == nil { | ||
return "No device found." | ||
} | ||
t := table.New() | ||
t.SetHeader("Name", "ID", "Board", "FQBN", "SerialNumber", "Status", "Connection type", "Thing", "Tags") | ||
t.AddRow( | ||
r.device.Name, | ||
r.device.ID, | ||
r.device.Board, | ||
r.device.FQBN, | ||
r.device.Serial, | ||
dereferenceString(r.device.Status), | ||
dereferenceString(r.device.ConnectionType), | ||
dereferenceString(r.device.ThingID), | ||
strings.Join(r.device.Tags, ","), | ||
) | ||
return t.Render() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// This file is part of arduino-cloud-cli. | ||
// | ||
// Copyright (C) 2024 ARDUINO SA (http://www.arduino.cc/) | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as published | ||
// by the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
package template | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"github.com/arduino/arduino-cli/cli/errorcodes" | ||
"github.com/arduino/arduino-cli/cli/feedback" | ||
"github.com/arduino/arduino-cloud-cli/command/template" | ||
"github.com/arduino/arduino-cloud-cli/config" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
type applyFlags struct { | ||
templateId string | ||
templatePrefix string | ||
deviceId string | ||
netCredentials string | ||
applyOta bool | ||
} | ||
|
||
func initTemplateApplyCommand() *cobra.Command { | ||
flags := &applyFlags{} | ||
applyCommand := &cobra.Command{ | ||
Use: "apply", | ||
Short: "Apply custom template", | ||
Long: "Given a template, apply it and create all the resources defined in it", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
if err := runTemplateApplyCommand(flags); err != nil { | ||
feedback.Errorf("Error during template apply: %v", err) | ||
os.Exit(errorcodes.ErrGeneric) | ||
} | ||
}, | ||
} | ||
|
||
applyCommand.Flags().StringVarP(&flags.templateId, "template-id", "t", "", "Template ID") | ||
applyCommand.Flags().StringVarP(&flags.templatePrefix, "prefix", "p", "", "Prefix to apply to the name of created resources") | ||
applyCommand.Flags().StringVarP(&flags.deviceId, "device-id", "d", "", "Device ID") | ||
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") | ||
|
||
applyCommand.MarkFlagRequired("template-id") | ||
applyCommand.MarkFlagRequired("prefix") | ||
applyCommand.MarkFlagRequired("device-id") | ||
|
||
flags.applyOta = false | ||
|
||
return applyCommand | ||
} | ||
|
||
func runTemplateApplyCommand(flags *applyFlags) error { | ||
cred, err := config.RetrieveCredentials() | ||
if err != nil { | ||
return fmt.Errorf("retrieving credentials: %w", err) | ||
} | ||
|
||
deviceNetCredentials, err := parseCredentials(flags.netCredentials) | ||
if err != nil { | ||
return fmt.Errorf("parsing network credentials: %w", err) | ||
} | ||
|
||
return template.ApplyCustomTemplates(cred, flags.templateId, flags.deviceId, flags.templatePrefix, deviceNetCredentials, flags.applyOta) | ||
} | ||
|
||
func parseCredentials(credentials string) (map[string]string, error) { | ||
credentialsMap := make(map[string]string) | ||
if credentials == "" { | ||
return credentialsMap, nil | ||
} | ||
credentialsArray := strings.Split(credentials, ",") | ||
for _, credential := range credentialsArray { | ||
credentialArray := strings.Split(credential, "=") | ||
if len(credentialArray) != 2 { | ||
return nil, fmt.Errorf("invalid network credential: %s", credential) | ||
} | ||
credentialsMap[credentialArray[0]] = credentialArray[1] | ||
} | ||
return credentialsMap, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// This file is part of arduino-cloud-cli. | ||
// | ||
// Copyright (C) 2021 ARDUINO SA (http://www.arduino.cc/) | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as published | ||
// by the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
package template | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/arduino/arduino-cli/cli/errorcodes" | ||
"github.com/arduino/arduino-cli/cli/feedback" | ||
"github.com/arduino/arduino-cloud-cli/command/template" | ||
"github.com/arduino/arduino-cloud-cli/config" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
type exportFlags struct { | ||
templateId string | ||
path string | ||
} | ||
|
||
func initTemplateExportCommand() *cobra.Command { | ||
flags := &exportFlags{} | ||
uploadCommand := &cobra.Command{ | ||
Use: "export", | ||
Short: "Export template", | ||
Long: "Export template to a file", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
if err := runTemplateExportCommand(flags); err != nil { | ||
feedback.Errorf("Error during template export: %v", err) | ||
os.Exit(errorcodes.ErrGeneric) | ||
} | ||
}, | ||
} | ||
|
||
uploadCommand.Flags().StringVarP(&flags.templateId, "template-id", "t", "", "Template id") | ||
uploadCommand.Flags().StringVarP(&flags.path, "directory", "d", "", "Output directory") | ||
|
||
uploadCommand.MarkFlagRequired("template-id") | ||
|
||
return uploadCommand | ||
} | ||
|
||
func runTemplateExportCommand(flags *exportFlags) error { | ||
cred, err := config.RetrieveCredentials() | ||
if err != nil { | ||
return fmt.Errorf("retrieving credentials: %w", err) | ||
} | ||
return template.ExportCustomTemplate(cred, flags.templateId, flags.path) | ||
} |
Oops, something went wrong.