Skip to content

Commit 1770dfb

Browse files
committed
Refactored strcuture
1 parent 61a4b67 commit 1770dfb

File tree

7 files changed

+51
-51
lines changed

7 files changed

+51
-51
lines changed

cli/device/masscreate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func runMassCreateCommand(flags *massCreateFlags) error {
7777
for _, board := range boards {
7878
params := &device.CreateParams{
7979
Name: flags.name,
80-
Port: &board.address,
80+
Port: &board.Address,
8181
}
8282
if flags.ctype != "" {
8383
params.ConnectionType = &flags.ctype

command/device/board.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,29 +43,29 @@ var (
4343
)
4444

4545
// board contains details of a physical arduino board.
46-
type board struct {
47-
fqbn string
48-
serial string
49-
dType string
50-
address string
51-
protocol string
46+
type Board struct {
47+
Fqbn string
48+
Serial string
49+
DType string
50+
Address string
51+
Protocol string
5252
}
5353

5454
// isCrypto checks if the board is a valid arduino board with a
5555
// supported crypto-chip.
56-
func (b *board) isCrypto() bool {
56+
func (b *Board) isCrypto() bool {
5757
for _, f := range cryptoFQBN {
58-
if b.fqbn == f {
58+
if b.Fqbn == f {
5959
return true
6060
}
6161
}
6262
return false
6363
}
6464

6565
// isLora checks if the board is a valid LoRa arduino board.
66-
func (b *board) isLora() bool {
66+
func (b *Board) isLora() bool {
6767
for _, f := range loraFQBN {
68-
if b.fqbn == f {
68+
if b.Fqbn == f {
6969
return true
7070
}
7171
}
@@ -74,19 +74,19 @@ func (b *board) isLora() bool {
7474

7575
// boardFromPorts returns a board that matches all the criteria
7676
// passed in. If no criteria are passed, it returns the first board found.
77-
func boardFromPorts(ports []*rpc.DetectedPort, params *CreateParams) *board {
77+
func boardFromPorts(ports []*rpc.DetectedPort, params *CreateParams) *Board {
7878
for _, port := range ports {
7979
if portFilter(port, params) {
8080
continue
8181
}
8282
boardFound := boardFilter(port.MatchingBoards, params)
8383
if boardFound != nil {
84-
b := &board{
85-
fqbn: boardFound.Fqbn,
86-
serial: port.Port.Properties["serialNumber"],
87-
dType: strings.Split(boardFound.Fqbn, ":")[2],
88-
address: port.Port.Address,
89-
protocol: port.Port.Protocol,
84+
b := &Board{
85+
Fqbn: boardFound.Fqbn,
86+
Serial: port.Port.Properties["serialNumber"],
87+
DType: strings.Split(boardFound.Fqbn, ":")[2],
88+
Address: port.Port.Address,
89+
Protocol: port.Port.Protocol,
9090
}
9191
return b
9292
}

command/device/board_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,21 +61,21 @@ func TestBoardFromPorts(t *testing.T) {
6161
name string
6262
filter *CreateParams
6363
ports []*rpc.DetectedPort
64-
want *board
64+
want *Board
6565
}{
6666

6767
{
6868
name: "port-filter",
6969
filter: &CreateParams{FQBN: nil, Port: stringPointer("ACM1")},
7070
ports: portsTwoBoards,
71-
want: &board{fqbn: "arduino:avr:uno", address: "ACM1"},
71+
want: &Board{Fqbn: "arduino:avr:uno", Address: "ACM1"},
7272
},
7373

7474
{
7575
name: "fqbn-filter",
7676
filter: &CreateParams{FQBN: stringPointer("arduino:avr:uno"), Port: nil},
7777
ports: portsTwoBoards,
78-
want: &board{fqbn: "arduino:avr:uno", address: "ACM1"},
78+
want: &Board{Fqbn: "arduino:avr:uno", Address: "ACM1"},
7979
},
8080

8181
{
@@ -90,7 +90,7 @@ func TestBoardFromPorts(t *testing.T) {
9090
filter: &CreateParams{FQBN: nil, Port: nil},
9191
ports: portsTwoBoards,
9292
// first board found is selected
93-
want: &board{fqbn: "arduino:samd:nano_33_iot", address: "ACM0"},
93+
want: &Board{Fqbn: "arduino:samd:nano_33_iot", Address: "ACM0"},
9494
},
9595

9696
{
@@ -104,7 +104,7 @@ func TestBoardFromPorts(t *testing.T) {
104104
name: "both-filter-found",
105105
filter: &CreateParams{FQBN: stringPointer("arduino:avr:uno"), Port: stringPointer("ACM1")},
106106
ports: portsTwoBoards,
107-
want: &board{fqbn: "arduino:avr:uno", address: "ACM1"},
107+
want: &Board{Fqbn: "arduino:avr:uno", Address: "ACM1"},
108108
},
109109

110110
{
@@ -123,14 +123,14 @@ func TestBoardFromPorts(t *testing.T) {
123123
return
124124

125125
} else if got != nil && tt.want == nil {
126-
t.Errorf("Expected nil board, received not nil board with port %s and fqbn %s", got.address, got.fqbn)
126+
t.Errorf("Expected nil board, received not nil board with port %s and fqbn %s", got.Address, got.Fqbn)
127127

128128
} else if got == nil && tt.want != nil {
129-
t.Errorf("Expected not nil board with port %s and fqbn %s, received a nil board", tt.want.address, tt.want.fqbn)
129+
t.Errorf("Expected not nil board with port %s and fqbn %s, received a nil board", tt.want.Address, tt.want.Fqbn)
130130

131-
} else if got.address != tt.want.address || got.fqbn != tt.want.fqbn {
131+
} else if got.Address != tt.want.Address || got.Fqbn != tt.want.Fqbn {
132132
t.Errorf("Expected board with port %s and fqbn %s, received board with port %s and fqbn %s",
133-
tt.want.address, tt.want.fqbn, got.address, got.fqbn)
133+
tt.want.Address, tt.want.Fqbn, got.Address, got.Fqbn)
134134
}
135135
})
136136
}

command/device/create.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ func Create(ctx context.Context, params *CreateParams, cred *config.Credentials)
6060
"board with fqbn %s found at port %s is not a device with a supported crypto-chip.\n"+
6161
"Try the 'create-lora' command instead if it's a LoRa device"+
6262
" or 'create-generic' otherwise",
63-
board.fqbn,
64-
board.address,
63+
board.Fqbn,
64+
board.Address,
6565
)
6666
}
6767

@@ -71,7 +71,7 @@ func Create(ctx context.Context, params *CreateParams, cred *config.Credentials)
7171
}
7272

7373
logrus.Info("Creating a new device on the cloud")
74-
dev, err := iotClient.DeviceCreate(ctx, board.fqbn, params.Name, board.serial, board.dType, params.ConnectionType)
74+
dev, err := iotClient.DeviceCreate(ctx, board.Fqbn, params.Name, board.Serial, board.DType, params.ConnectionType)
7575
if err != nil {
7676
return nil, err
7777
}

command/device/createlora.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,26 +84,26 @@ func CreateLora(ctx context.Context, params *CreateLoraParams, cred *config.Cred
8484
"board with fqbn %s found at port %s is not a LoRa device."+
8585
" Try the 'create' command instead if it's a device with a supported crypto-chip"+
8686
" or 'create-generic' otherwise",
87-
board.fqbn,
88-
board.address,
87+
board.Fqbn,
88+
board.Address,
8989
)
9090
}
9191

92-
bin, err := downloadProvisioningFile(ctx, board.fqbn)
92+
bin, err := downloadProvisioningFile(ctx, board.Fqbn)
9393
if err != nil {
9494
return nil, err
9595
}
9696

9797
logrus.Infof("%s", "Uploading deveui sketch on the LoRa board")
9898
errMsg := "Error while uploading the LoRa provisioning binary"
9999
err = retry(ctx, deveuiUploadAttempts, deveuiUploadWait*time.Millisecond, errMsg, func() error {
100-
return comm.UploadBin(ctx, board.fqbn, bin, board.address, board.protocol)
100+
return comm.UploadBin(ctx, board.Fqbn, bin, board.Address, board.Protocol)
101101
})
102102
if err != nil {
103103
return nil, fmt.Errorf("failed to upload LoRa provisioning binary: %w", err)
104104
}
105105

106-
eui, err := extractEUI(ctx, board.address)
106+
eui, err := extractEUI(ctx, board.Address)
107107
if err != nil {
108108
return nil, err
109109
}
@@ -114,7 +114,7 @@ func CreateLora(ctx context.Context, params *CreateLoraParams, cred *config.Cred
114114
}
115115

116116
logrus.Info("Creating a new device on the cloud")
117-
dev, err := iotClient.DeviceLoraCreate(ctx, params.Name, board.serial, board.dType, eui, params.FrequencyPlan)
117+
dev, err := iotClient.DeviceLoraCreate(ctx, params.Name, board.Serial, board.DType, eui, params.FrequencyPlan)
118118
if err != nil {
119119
return nil, err
120120
}

command/device/masscreate.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ import (
1010
"go.bug.st/cleanup"
1111
)
1212

13-
func ListAllConnectedBoardsWithCrypto() ([]*board, error) {
13+
func ListAllConnectedBoardsWithCrypto() ([]*Board, error) {
1414
comm, err := ListAllConnectedBoards()
1515
if err != nil {
1616
return nil, err
1717
}
18-
var withcrypto []*board
18+
var withcrypto []*Board
1919
for _, b := range comm {
2020
if b.isCrypto() {
2121
withcrypto = append(withcrypto, b)
@@ -24,7 +24,7 @@ func ListAllConnectedBoardsWithCrypto() ([]*board, error) {
2424
return withcrypto, nil
2525
}
2626

27-
func ListAllConnectedBoards() ([]*board, error) {
27+
func ListAllConnectedBoards() ([]*Board, error) {
2828
comm, err := cli.NewCommander()
2929
if err != nil {
3030
return nil, err
@@ -48,18 +48,18 @@ func ListAllConnectedBoards() ([]*board, error) {
4848
}
4949

5050
// boardsFromPorts returns boards that matches all the criteria
51-
func boardsFromPorts(ports []*rpc.DetectedPort, fqbn *string) []*board {
52-
var boards []*board
51+
func boardsFromPorts(ports []*rpc.DetectedPort, fqbn *string) []*Board {
52+
var boards []*Board
5353
for _, port := range ports {
5454
boardsFound := boardsFilter(port.MatchingBoards, fqbn)
5555
if len(boardsFound) > 0 {
5656
for _, boardFound := range boardsFound {
57-
b := &board{
58-
fqbn: boardFound.Fqbn,
59-
serial: port.Port.Properties["serialNumber"],
60-
dType: strings.Split(boardFound.Fqbn, ":")[2],
61-
address: port.Port.Address,
62-
protocol: port.Port.Protocol,
57+
b := &Board{
58+
Fqbn: boardFound.Fqbn,
59+
Serial: port.Port.Properties["serialNumber"],
60+
DType: strings.Split(boardFound.Fqbn, ":")[2],
61+
Address: port.Port.Address,
62+
Protocol: port.Port.Protocol,
6363
}
6464
b.isCrypto()
6565
boards = append(boards, b)

command/device/provision.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,13 @@ type provision struct {
7474
arduino.Commander
7575
cert certificateCreator
7676
ser *serial.Serial
77-
board *board
77+
board *Board
7878
id string
7979
}
8080

8181
// run provisioning procedure for boards with crypto-chip.
8282
func (p provision) run(ctx context.Context) error {
83-
bin, err := downloadProvisioningFile(ctx, p.board.fqbn)
83+
bin, err := downloadProvisioningFile(ctx, p.board.Fqbn)
8484
if err != nil {
8585
return err
8686
}
@@ -93,7 +93,7 @@ func (p provision) run(ctx context.Context) error {
9393
errMsg := "Error while uploading the provisioning sketch"
9494
err = retry(ctx, 5, time.Millisecond*1000, errMsg, func() error {
9595
//serialutils.Reset(dev.port, true, nil)
96-
return p.UploadBin(ctx, p.board.fqbn, bin, p.board.address, p.board.protocol)
96+
return p.UploadBin(ctx, p.board.Fqbn, bin, p.board.Address, p.board.Protocol)
9797
})
9898
if err != nil {
9999
return err
@@ -107,7 +107,7 @@ func (p provision) run(ctx context.Context) error {
107107
p.ser = serial.NewSerial()
108108
errMsg = "Error while connecting to the board"
109109
err = retry(ctx, 5, time.Millisecond*1000, errMsg, func() error {
110-
return p.ser.Connect(p.board.address)
110+
return p.ser.Connect(p.board.Address)
111111
})
112112
if err != nil {
113113
return err

0 commit comments

Comments
 (0)