Skip to content

Commit bb9cd22

Browse files
Paolo Calaopolldo
Paolo Calao
authored andcommitted
Improve template extraction (#49)
Functions to extract templates from a thing and a dashboard does not return errors anymore (they're not needed). Default template filenames have been changed: whitespaces are stripped and a suffix '-thing' or '-dashboard' has been added. Destination files are now logged. * Remove useless error parameter * Strip spaces from extracted template names * Extraction: Log destination file
1 parent 67c6495 commit bb9cd22

File tree

3 files changed

+14
-14
lines changed

3 files changed

+14
-14
lines changed

Diff for: command/dashboard/extract.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/arduino/arduino-cloud-cli/internal/config"
2626
"github.com/arduino/arduino-cloud-cli/internal/iot"
2727
"github.com/arduino/arduino-cloud-cli/internal/template"
28+
"github.com/sirupsen/logrus"
2829
)
2930

3031
// ExtractParams contains the parameters needed to
@@ -58,20 +59,19 @@ func Extract(params *ExtractParams) error {
5859
return err
5960
}
6061

61-
templ, err := template.FromDashboard(dashboard)
62-
if err != nil {
63-
return err
64-
}
62+
templ := template.FromDashboard(dashboard)
6563

6664
if params.Outfile == nil {
6765
name, ok := templ["name"].(string)
6866
if name == "" || !ok {
6967
return errors.New("dashboard template does not have a valid name")
7068
}
71-
outfile := name + "-dashboard" + "." + params.Format
69+
name = strings.Join(strings.Fields(name), "")
70+
outfile := name + "-dashboard." + params.Format
7271
params.Outfile = &outfile
7372
}
7473

74+
logrus.Infof("Extracting template in file: %s", *params.Outfile)
7575
err = template.ToFile(templ, *params.Outfile, params.Format)
7676
if err != nil {
7777
return fmt.Errorf("saving template: %w", err)

Diff for: command/thing/extract.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/arduino/arduino-cloud-cli/internal/config"
2626
"github.com/arduino/arduino-cloud-cli/internal/iot"
2727
"github.com/arduino/arduino-cloud-cli/internal/template"
28+
"github.com/sirupsen/logrus"
2829
)
2930

3031
// ExtractParams contains the parameters needed to
@@ -58,20 +59,19 @@ func Extract(params *ExtractParams) error {
5859
return err
5960
}
6061

61-
templ, err := template.FromThing(thing)
62-
if err != nil {
63-
return err
64-
}
62+
templ := template.FromThing(thing)
6563

6664
if params.Outfile == nil {
6765
name, ok := templ["name"].(string)
6866
if name == "" || !ok {
6967
return errors.New("thing template does not have a valid name")
7068
}
71-
outfile := name + "." + params.Format
69+
name = strings.Join(strings.Fields(name), "")
70+
outfile := name + "-thing." + params.Format
7271
params.Outfile = &outfile
7372
}
7473

74+
logrus.Infof("Extracting template in file: %s", *params.Outfile)
7575
err = template.ToFile(templ, *params.Outfile, params.Format)
7676
if err != nil {
7777
return fmt.Errorf("saving template: %w", err)

Diff for: internal/template/extract.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929
)
3030

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

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

50-
return template, nil
50+
return template
5151
}
5252

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

@@ -94,7 +94,7 @@ func FromDashboard(dashboard *iotclient.ArduinoDashboardv2) (map[string]interfac
9494
if len(widgets) > 0 {
9595
template["widgets"] = widgets
9696
}
97-
return template, nil
97+
return template
9898
}
9999

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

0 commit comments

Comments
 (0)