Skip to content

Refactor template format #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 3, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Thing template - Support both yaml and json formats
  • Loading branch information
polldo committed Sep 2, 2021
commit dfbe62ab4c9b8e3fdc5f549f7471256546f6b067
10 changes: 7 additions & 3 deletions command/thing/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
iotclient "github.com/arduino/iot-client-go"
"github.com/arduino/iot-cloud-cli/internal/config"
"github.com/arduino/iot-cloud-cli/internal/iot"
"gopkg.in/yaml.v3"
)

// CreateParams contains the parameters needed to create a new thing.
Expand Down Expand Up @@ -67,9 +68,12 @@ func loadTemplate(file string) (*iotclient.Thing, error) {
}

template := make(map[string]interface{})
err = json.Unmarshal([]byte(templateBytes), &template)
if err != nil {
return nil, fmt.Errorf("%s: %w", "reading template file: template not valid: ", err)

// Extract template trying all the supported formats: json and yaml
if err = json.Unmarshal([]byte(templateBytes), &template); err != nil {
if err = yaml.Unmarshal([]byte(templateBytes), &template); err != nil {
return nil, errors.New("reading template file: template format is not valid")
}
}

// Adapt thing template to thing structure
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ require (
google.golang.org/genproto v0.0.0-20210504143626-3b2ad6ccc450 // indirect
google.golang.org/grpc v1.39.0
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c
)