Skip to content

Improve template extraction #49

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 3 commits into from
Oct 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 5 additions & 5 deletions command/dashboard/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/arduino/arduino-cloud-cli/internal/config"
"github.com/arduino/arduino-cloud-cli/internal/iot"
"github.com/arduino/arduino-cloud-cli/internal/template"
"github.com/sirupsen/logrus"
)

// ExtractParams contains the parameters needed to
Expand Down Expand Up @@ -58,20 +59,19 @@ func Extract(params *ExtractParams) error {
return err
}

templ, err := template.FromDashboard(dashboard)
if err != nil {
return err
}
templ := template.FromDashboard(dashboard)

if params.Outfile == nil {
name, ok := templ["name"].(string)
if name == "" || !ok {
return errors.New("dashboard template does not have a valid name")
}
outfile := name + "-dashboard" + "." + params.Format
name = strings.Join(strings.Fields(name), "")
outfile := name + "-dashboard." + params.Format
params.Outfile = &outfile
}

logrus.Infof("Extracting template in file: %s", *params.Outfile)
err = template.ToFile(templ, *params.Outfile, params.Format)
if err != nil {
return fmt.Errorf("saving template: %w", err)
Expand Down
10 changes: 5 additions & 5 deletions command/thing/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/arduino/arduino-cloud-cli/internal/config"
"github.com/arduino/arduino-cloud-cli/internal/iot"
"github.com/arduino/arduino-cloud-cli/internal/template"
"github.com/sirupsen/logrus"
)

// ExtractParams contains the parameters needed to
Expand Down Expand Up @@ -58,20 +59,19 @@ func Extract(params *ExtractParams) error {
return err
}

templ, err := template.FromThing(thing)
if err != nil {
return err
}
templ := template.FromThing(thing)

if params.Outfile == nil {
name, ok := templ["name"].(string)
if name == "" || !ok {
return errors.New("thing template does not have a valid name")
}
outfile := name + "." + params.Format
name = strings.Join(strings.Fields(name), "")
outfile := name + "-thing." + params.Format
params.Outfile = &outfile
}

logrus.Infof("Extracting template in file: %s", *params.Outfile)
err = template.ToFile(templ, *params.Outfile, params.Format)
if err != nil {
return fmt.Errorf("saving template: %w", err)
Expand Down
8 changes: 4 additions & 4 deletions internal/template/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
)

// FromThing extracts a template of type map[string]interface{} from a thing.
func FromThing(thing *iotclient.ArduinoThing) (map[string]interface{}, error) {
func FromThing(thing *iotclient.ArduinoThing) map[string]interface{} {
template := make(map[string]interface{})
template["name"] = thing.Name

Expand All @@ -47,11 +47,11 @@ func FromThing(thing *iotclient.ArduinoThing) (map[string]interface{}, error) {
}
template["variables"] = props

return template, nil
return template
}

// FromDashboard extracts a template of type map[string]interface{} from a dashboard.
func FromDashboard(dashboard *iotclient.ArduinoDashboardv2) (map[string]interface{}, error) {
func FromDashboard(dashboard *iotclient.ArduinoDashboardv2) map[string]interface{} {
template := make(map[string]interface{})
template["name"] = dashboard.Name

Expand Down Expand Up @@ -94,7 +94,7 @@ func FromDashboard(dashboard *iotclient.ArduinoDashboardv2) (map[string]interfac
if len(widgets) > 0 {
template["widgets"] = widgets
}
return template, nil
return template
}

// ToFile takes a generic template and saves it into a file,
Expand Down