Skip to content

Commit a78ee26

Browse files
committed
Started refactoring to move to client v2
1 parent e7a4495 commit a78ee26

21 files changed

+105
-56
lines changed

command/dashboard/create.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ func Create(ctx context.Context, params *CreateParams, cred *config.Credentials)
4747

4848
// Name passed as parameter has priority over name from template
4949
if params.Name != nil {
50-
dashboard.Name = *params.Name
50+
dashboard.Name = params.Name
5151
}
5252
// If name is not specified in the template, it should be passed as parameter
53-
if dashboard.Name == "" {
53+
if dashboard.Name == nil || *dashboard.Name == "" {
5454
return nil, errors.New("dashboard name not specified")
5555
}
5656

command/dashboard/dashboard.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ type DashboardInfo struct {
3333
func getDashboardInfo(dashboard *iotclient.ArduinoDashboardv2) *DashboardInfo {
3434
var widgets []string
3535
for _, w := range dashboard.Widgets {
36-
widgets = append(widgets, w.Name)
36+
if w.Name != nil {
37+
widgets = append(widgets, *w.Name)
38+
}
3739
}
3840
info := &DashboardInfo{
3941
Name: dashboard.Name,

command/device/create.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,14 @@ func Create(ctx context.Context, params *CreateParams, cred *config.Credentials)
100100
ID: dev.Id,
101101
Board: dev.Type,
102102
Serial: dev.Serial,
103-
FQBN: dev.Fqbn,
103+
FQBN: dereferenceString(dev.Fqbn),
104104
}
105105
return devInfo, nil
106106
}
107+
108+
func dereferenceString(s *string) string {
109+
if s == nil {
110+
return ""
111+
}
112+
return *s
113+
}

command/device/creategeneric.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ func CreateGeneric(ctx context.Context, params *CreateGenericParams, cred *confi
7575
ID: dev.Id,
7676
Board: dev.Type,
7777
Serial: dev.Serial,
78-
FQBN: dev.Fqbn,
78+
FQBN: dereferenceString(dev.Fqbn),
7979
},
80-
Password: pass.SuggestedPassword,
80+
Password: dereferenceString(pass.SuggestedPassword),
8181
}
8282
return devInfo, nil
8383
}

command/device/createlora.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ func getDeviceLoraInfo(ctx context.Context, iotClient *iot.Client, loraDev *iotc
181181
ID: dev.Id,
182182
Board: dev.Type,
183183
Serial: dev.Serial,
184-
FQBN: dev.Fqbn,
184+
FQBN: dereferenceString(dev.Fqbn),
185185
},
186186
AppEUI: loraDev.AppEui,
187187
AppKey: loraDev.AppKey,

command/device/device.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func getDeviceInfo(device *iotclient.ArduinoDevicev2) (*DeviceInfo, error) {
4545
ID: device.Id,
4646
Board: device.Type,
4747
Serial: device.Serial,
48-
FQBN: device.Fqbn,
48+
FQBN: dereferenceString(device.Fqbn),
4949
Tags: tags,
5050
}
5151
return dev, nil

command/device/provision.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ func (p provision) configBoard(ctx context.Context) error {
185185
}
186186

187187
logrus.Info("Sending certificate authority key")
188-
b, err = hex.DecodeString(cert.AuthorityKeyIdentifier)
188+
b, err = hex.DecodeString(dereferenceString(cert.AuthorityKeyIdentifier))
189189
if err != nil {
190190
return fmt.Errorf("decoding certificate authority key id: %w", err)
191191
}

command/ota/generate.go

+7
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,10 @@ func Generate(binFile string, outFile string, fqbn string) error {
6868

6969
return nil
7070
}
71+
72+
func dereferenceString(s *string) string {
73+
if s == nil {
74+
return ""
75+
}
76+
return *s
77+
}

command/ota/massupload.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ import (
2121
"context"
2222
"errors"
2323
"fmt"
24-
"github.com/sirupsen/logrus"
2524
"os"
2625
"path/filepath"
2726

27+
"github.com/sirupsen/logrus"
28+
2829
"github.com/arduino/arduino-cloud-cli/config"
2930
"github.com/arduino/arduino-cloud-cli/internal/iot"
3031
"github.com/arduino/arduino-cloud-cli/internal/ota"
@@ -179,8 +180,8 @@ func validateDevices(ctx context.Context, lister deviceLister, ids []string, fqb
179180
continue
180181
}
181182
// Device FQBN doesn't match the passed one
182-
if found.Fqbn != fqbn {
183-
inv := Result{ID: id, Err: fmt.Errorf("has FQBN '%s' instead of '%s'", found.Fqbn, fqbn)}
183+
if dereferenceString(found.Fqbn) != fqbn {
184+
inv := Result{ID: id, Err: fmt.Errorf("has FQBN '%s' instead of '%s'", dereferenceString(found.Fqbn), fqbn)}
184185
invalid = append(invalid, inv)
185186
continue
186187
}

command/ota/massupload_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,9 @@ func TestValidateDevices(t *testing.T) {
9696

9797
mockDeviceList := deviceListerTest{
9898
list: []iotclient.ArduinoDevicev2{
99-
{Id: idCorrect1, Fqbn: correctFQBN},
100-
{Id: idCorrect2, Fqbn: correctFQBN},
101-
{Id: idNotValid, Fqbn: wrongFQBN},
99+
{Id: idCorrect1, Fqbn: &correctFQBN},
100+
{Id: idCorrect2, Fqbn: &correctFQBN},
101+
{Id: idNotValid, Fqbn: &wrongFQBN},
102102
},
103103
}
104104

command/ota/upload.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func Upload(ctx context.Context, params *UploadParams, cred *config.Credentials)
8585
otaFile = filepath.Join(otaDir, "temp.ota")
8686
defer os.RemoveAll(otaDir)
8787

88-
err = Generate(params.File, otaFile, dev.Fqbn)
88+
err = Generate(params.File, otaFile, dereferenceString(dev.Fqbn))
8989
if err != nil {
9090
return fmt.Errorf("%s: %w", "cannot generate .ota file", err)
9191
}

command/thing/bind.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func Bind(ctx context.Context, params *BindParams, cred *config.Credentials) err
4242
}
4343

4444
thing := &iotclient.ThingUpdate{
45-
DeviceId: params.DeviceID,
45+
DeviceId: &params.DeviceID,
4646
}
4747

4848
err = iotClient.ThingUpdate(ctx, params.ID, thing, true)
@@ -52,3 +52,10 @@ func Bind(ctx context.Context, params *BindParams, cred *config.Credentials) err
5252

5353
return nil
5454
}
55+
56+
func dereferenceString(s *string) string {
57+
if s == nil {
58+
return ""
59+
}
60+
return *s
61+
}

command/thing/clone.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func Clone(ctx context.Context, params *CloneParams, cred *config.Credentials) (
4444
return nil, err
4545
}
4646

47-
thing.Name = params.Name
47+
thing.Name = &params.Name
4848
force := true
4949
newThing, err := iotClient.ThingCreate(ctx, thing, force)
5050
if err != nil {

command/thing/create.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ func Create(ctx context.Context, params *CreateParams, cred *config.Credentials)
4747

4848
// Name passed as parameter has priority over name from template
4949
if params.Name != nil {
50-
thing.Name = *params.Name
50+
thing.Name = params.Name
5151
}
5252
// If name is not specified in the template, it should be passed as parameter
53-
if thing.Name == "" {
53+
if dereferenceString(thing.Name) == "" {
5454
return nil, errors.New("thing name not specified")
5555
}
5656

command/thing/thing.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func getThingInfo(thing *iotclient.ArduinoThing) (*ThingInfo, error) {
4747
info := &ThingInfo{
4848
Name: thing.Name,
4949
ID: thing.Id,
50-
DeviceID: thing.DeviceId,
50+
DeviceID: dereferenceString(thing.DeviceId),
5151
Variables: vars,
5252
Tags: tags,
5353
}

go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ require (
77
github.com/arduino/arduino-cli v0.0.0-20221116144942-76251df9241a
88
github.com/arduino/go-paths-helper v1.7.0
99
github.com/arduino/go-win32-utils v1.0.0
10-
github.com/arduino/iot-client-go v1.4.4
10+
github.com/arduino/iot-client-go v1.4.5-0.20240416140423-fb8fda8a47af
1111
github.com/gofrs/uuid v4.2.0+incompatible
1212
github.com/google/go-cmp v0.6.0
1313
github.com/howeyc/crc16 v0.0.0-20171223171357-2b2a61e366a6
14+
github.com/icza/bitio v1.1.0
1415
github.com/manifoldco/promptui v0.9.0
1516
github.com/sirupsen/logrus v1.9.0
1617
github.com/spf13/cobra v1.3.0
@@ -43,7 +44,6 @@ require (
4344
github.com/golang/protobuf v1.5.3 // indirect
4445
github.com/h2non/filetype v1.1.3 // indirect
4546
github.com/hashicorp/hcl v1.0.0 // indirect
46-
github.com/icza/bitio v1.1.0 // indirect
4747
github.com/inconshreveable/mousetrap v1.0.0 // indirect
4848
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
4949
github.com/josharian/intern v1.0.0 // indirect

go.sum

+3-2
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ github.com/arduino/go-properties-orderedmap v1.7.1 h1:HQ9Pn/mk3+XyfrE39EEvaZwJkr
7575
github.com/arduino/go-properties-orderedmap v1.7.1/go.mod h1:DKjD2VXY/NZmlingh4lSFMEYCVubfeArCsGPGDwb2yk=
7676
github.com/arduino/go-win32-utils v1.0.0 h1:/cXB86sOJxOsCHP7sQmXGLkdValwJt56mIwOHYxgQjQ=
7777
github.com/arduino/go-win32-utils v1.0.0/go.mod h1:0jqM7doGEAs6DaJCxxhLBUDS5OawrqF48HqXkcEie/Q=
78-
github.com/arduino/iot-client-go v1.4.4 h1:FICCXD5uCZ0scGG6RioOlpZamDqgSD1l/fQlFwKR284=
79-
github.com/arduino/iot-client-go v1.4.4/go.mod h1:gYvpMt7Qw+OSScTLyIlCnpbvy9y96ey/2zhB4w6FoK0=
78+
github.com/arduino/iot-client-go v1.4.5-0.20240416140423-fb8fda8a47af h1:/dbu5I3q09kOePrWbBVMrKEPDM8KKUIGATQpwWc/wWo=
79+
github.com/arduino/iot-client-go v1.4.5-0.20240416140423-fb8fda8a47af/go.mod h1:aKaQRceeYIeMpyFyEUGJj4/5WFETZD1CrarYGn4v5Nc=
8080
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
8181
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY=
8282
github.com/armon/go-metrics v0.3.10/go.mod h1:4O98XIr/9W0sxpJ8UaYkvjk10Iff7SnFrb4QAOwNTFc=
@@ -287,6 +287,7 @@ github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:
287287
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
288288
github.com/icza/bitio v1.1.0 h1:ysX4vtldjdi3Ygai5m1cWy4oLkhWTAi+SyO6HC8L9T0=
289289
github.com/icza/bitio v1.1.0/go.mod h1:0jGnlLAx8MKMr9VGnn/4YrvZiprkvBelsVIbA9Jjr9A=
290+
github.com/icza/mighty v0.0.0-20180919140131-cfd07d671de6 h1:8UsGZ2rr2ksmEru6lToqnXgA8Mz1DP11X4zSJ159C3k=
290291
github.com/icza/mighty v0.0.0-20180919140131-cfd07d671de6/go.mod h1:xQig96I1VNBDIWGCdTt54nHt6EeI639SmHycLYL7FkA=
291292
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
292293
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=

internal/iot/client.go

+19-10
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ func NewClient(cred *config.Credentials) (*Client, error) {
4848
return cl, nil
4949
}
5050

51+
func toStringPointer(s string) *string {
52+
return &s
53+
}
54+
5155
// DeviceCreate allows to create a new device on Arduino IoT Cloud.
5256
// It returns the newly created device, and an error.
5357
func (cl *Client) DeviceCreate(ctx context.Context, fqbn, name, serial, dType string, cType *string) (*iotclient.ArduinoDevicev2, error) {
@@ -57,22 +61,24 @@ func (cl *Client) DeviceCreate(ctx context.Context, fqbn, name, serial, dType st
5761
}
5862

5963
payload := iotclient.CreateDevicesV2Payload{
60-
Fqbn: fqbn,
61-
Name: name,
62-
Serial: serial,
64+
Fqbn: toStringPointer(fqbn),
65+
Name: toStringPointer(name),
66+
Serial: toStringPointer(serial),
6367
Type: dType,
6468
}
6569

6670
if cType != nil {
67-
payload.ConnectionType = *cType
71+
payload.ConnectionType = cType
6872
}
6973

70-
dev, _, err := cl.api.DevicesV2Api.DevicesV2Create(ctx, payload, nil)
74+
req := cl.api.DevicesV2Api.DevicesV2Create(ctx)
75+
req = req.CreateDevicesV2Payload(payload)
76+
dev, _, err := cl.api.DevicesV2Api.DevicesV2CreateExecute(req)
7177
if err != nil {
7278
err = fmt.Errorf("creating device, %w", errorDetail(err))
7379
return nil, err
7480
}
75-
return &dev, nil
81+
return dev, nil
7682
}
7783

7884
// DeviceLoraCreate allows to create a new LoRa device on Arduino IoT Cloud.
@@ -88,17 +94,19 @@ func (cl *Client) DeviceLoraCreate(ctx context.Context, name, serial, devType, e
8894
Eui: eui,
8995
FrequencyPlan: freq,
9096
Name: name,
91-
Serial: serial,
97+
Serial: toStringPointer(serial),
9298
Type: devType,
9399
UserId: "me",
94100
}
95101

96-
dev, _, err := cl.api.LoraDevicesV1Api.LoraDevicesV1Create(ctx, payload, nil)
102+
req := cl.api.LoraDevicesV1Api.LoraDevicesV1Create(ctx)
103+
req = req.CreateLoraDevicesV1Payload(payload)
104+
dev, _, err := cl.api.LoraDevicesV1Api.LoraDevicesV1CreateExecute(req)
97105
if err != nil {
98106
err = fmt.Errorf("creating lora device: %w", errorDetail(err))
99107
return nil, err
100108
}
101-
return &dev, nil
109+
return dev, nil
102110
}
103111

104112
// DevicePassSet sets the device password to the one suggested by Arduino IoT Cloud.
@@ -135,7 +143,8 @@ func (cl *Client) DeviceDelete(ctx context.Context, id string) error {
135143
return err
136144
}
137145

138-
_, err = cl.api.DevicesV2Api.DevicesV2Delete(ctx, id, nil)
146+
req := cl.api.DevicesV2Api.DevicesV2Delete(ctx, id)
147+
_, err = cl.api.DevicesV2Api.DevicesV2DeleteExecute(req)
139148
if err != nil {
140149
err = fmt.Errorf("deleting device: %w", errorDetail(err))
141150
return err

internal/template/dashboard.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,17 @@ func getVariableID(ctx context.Context, thingID string, variableName string, fet
7676
}
7777

7878
for _, v := range thing.Properties {
79-
if v.VariableName == variableName {
79+
if v.VariableName == &variableName {
8080
return v.Id, nil
8181
}
8282
}
8383

8484
return "", fmt.Errorf("thing with id %s doesn't have variable with name %s : %w", thingID, variableName, err)
8585
}
86+
87+
func dereferenceString(s *string) string {
88+
if s == nil {
89+
return ""
90+
}
91+
return *s
92+
}

internal/template/extract.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func FromDashboard(dashboard *iotclient.ArduinoDashboardv2) map[string]interface
6767
widget["x"] = w.X
6868
widget["y"] = w.Y
6969

70-
if w.WidthMobile != 0 && w.HeightMobile != 0 {
70+
if w.WidthMobile != nil && w.HeightMobile != nil && *w.WidthMobile != 0 && *w.HeightMobile != 0 {
7171
widget["width_mobile"] = w.WidthMobile
7272
widget["height_mobile"] = w.HeightMobile
7373
widget["x_mobile"] = w.XMobile

0 commit comments

Comments
 (0)