Skip to content

Commit 12186eb

Browse files
committed
Set cmd optional params as pointers (#29)
All implemented commands now accept optional parameters as pointers. This change makes the command package more clear and easier to use: if the type of a parameter is a pointer, then it becomes clear that such parameter is optional. So, for example, instead of an empty string now it's possible to pass nil for optional parameters.
1 parent 2cc92ee commit 12186eb

File tree

6 files changed

+38
-23
lines changed

6 files changed

+38
-23
lines changed

cli/device/create.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,13 @@ func runCreateCommand(cmd *cobra.Command, args []string) error {
3131
fmt.Printf("Creating device with name %s\n", createFlags.name)
3232

3333
params := &device.CreateParams{
34-
Port: createFlags.port,
3534
Name: createFlags.name,
36-
Fqbn: createFlags.fqbn,
35+
}
36+
if createFlags.port != "" {
37+
params.Port = &createFlags.port
38+
}
39+
if createFlags.fqbn != "" {
40+
params.Fqbn = &createFlags.fqbn
3741
}
3842

3943
devID, err := device.Create(params)

cli/thing/create.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@ func runCreateCommand(cmd *cobra.Command, args []string) error {
2929
fmt.Printf("Creating thing from template %s\n", createFlags.template)
3030

3131
params := &thing.CreateParams{
32-
Name: createFlags.name,
3332
Template: createFlags.template,
3433
}
34+
if createFlags.name != "" {
35+
params.Name = &createFlags.name
36+
}
3537

3638
thingID, err := thing.Create(params)
3739
if err != nil {

cli/thing/extract.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ func initExtractCommand() *cobra.Command {
2828
func runExtractCommand(cmd *cobra.Command, args []string) error {
2929
fmt.Printf("Extracting template from thing %s\n", extractFlags.id)
3030

31-
params := &thing.ExtractParams{ID: extractFlags.id}
31+
params := &thing.ExtractParams{
32+
ID: extractFlags.id,
33+
}
3234
if extractFlags.outfile != "" {
3335
params.Outfile = &extractFlags.outfile
3436
}

command/device/create.go

+12-9
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ import (
1313

1414
// CreateParams contains the parameters needed
1515
// to find the device to be provisioned.
16-
// If Port is an empty string, then each serial port is analyzed.
17-
// If Fqbn is an empty string, then the first device found gets selected.
16+
// Name - mandatory parameter.
17+
// Port - optional parameter. If omitted then each serial port is analyzed.
18+
// Fqbn - optional parameter. If omitted then the first device found gets selected.
1819
type CreateParams struct {
19-
Port string
2020
Name string
21-
Fqbn string
21+
Port *string
22+
Fqbn *string
2223
}
2324

2425
type device struct {
@@ -96,31 +97,33 @@ func deviceFromPorts(ports []*rpc.DetectedPort, params *CreateParams) *device {
9697
return nil
9798
}
9899

99-
// portFilter filters out the given port if the port parameter is not an empty string
100-
// and if they do not match.
100+
// portFilter filters out the given port in the following cases:
101+
// - if the port parameter does not match the actual port address.
102+
// - if the the detected port does not contain any board.
101103
// It returns:
102104
// true -> to skip the port
103105
// false -> to keep the port
104106
func portFilter(port *rpc.DetectedPort, params *CreateParams) bool {
105107
if len(port.Boards) == 0 {
106108
return true
107109
}
108-
if params.Port != "" && params.Port != port.Address {
110+
if params.Port != nil && *params.Port != port.Address {
109111
return true
110112
}
111113
return false
112114
}
113115

114116
// boardFilter looks for a board which has the same fqbn passed as parameter.
117+
// If fqbn parameter is nil, then the first board found is returned.
115118
// It returns:
116119
// - a board if it is found.
117120
// - nil if no board matching the fqbn parameter is found.
118121
func boardFilter(boards []*rpc.BoardListItem, params *CreateParams) (board *rpc.BoardListItem) {
119-
if params.Fqbn == "" {
122+
if params.Fqbn == nil {
120123
return boards[0]
121124
}
122125
for _, b := range boards {
123-
if b.Fqbn == params.Fqbn {
126+
if b.Fqbn == *params.Fqbn {
124127
return b
125128
}
126129
}

command/device/create_test.go

+11-7
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ var (
3535
}
3636
)
3737

38+
func stringPointer(s string) *string {
39+
return &s
40+
}
41+
3842
func TestDeviceFromPorts(t *testing.T) {
3943
tests := []struct {
4044
name string
@@ -45,50 +49,50 @@ func TestDeviceFromPorts(t *testing.T) {
4549

4650
{
4751
name: "port-filter",
48-
filter: &CreateParams{Fqbn: "", Port: "ACM1"},
52+
filter: &CreateParams{Fqbn: nil, Port: stringPointer("ACM1")},
4953
ports: portsTwoBoards,
5054
want: &device{fqbn: "arduino:avr:uno", port: "ACM1"},
5155
},
5256

5357
{
5458
name: "fqbn-filter",
55-
filter: &CreateParams{Fqbn: "arduino:avr:uno", Port: ""},
59+
filter: &CreateParams{Fqbn: stringPointer("arduino:avr:uno"), Port: nil},
5660
ports: portsTwoBoards,
5761
want: &device{fqbn: "arduino:avr:uno", port: "ACM1"},
5862
},
5963

6064
{
6165
name: "no-filter-noboards",
62-
filter: &CreateParams{Fqbn: "", Port: ""},
66+
filter: &CreateParams{Fqbn: nil, Port: nil},
6367
ports: portsNoBoards,
6468
want: nil,
6569
},
6670

6771
{
6872
name: "no-filter",
69-
filter: &CreateParams{Fqbn: "", Port: ""},
73+
filter: &CreateParams{Fqbn: nil, Port: nil},
7074
ports: portsTwoBoards,
7175
// first device found is selected
7276
want: &device{fqbn: "arduino:samd:nano_33_iot", port: "ACM0"},
7377
},
7478

7579
{
7680
name: "both-filter-noboards",
77-
filter: &CreateParams{Fqbn: "arduino:avr:uno", Port: "ACM1"},
81+
filter: &CreateParams{Fqbn: stringPointer("arduino:avr:uno"), Port: stringPointer("ACM1")},
7882
ports: portsNoBoards,
7983
want: nil,
8084
},
8185

8286
{
8387
name: "both-filter-found",
84-
filter: &CreateParams{Fqbn: "arduino:avr:uno", Port: "ACM1"},
88+
filter: &CreateParams{Fqbn: stringPointer("arduino:avr:uno"), Port: stringPointer("ACM1")},
8589
ports: portsTwoBoards,
8690
want: &device{fqbn: "arduino:avr:uno", port: "ACM1"},
8791
},
8892

8993
{
9094
name: "both-filter-notfound",
91-
filter: &CreateParams{Fqbn: "arduino:avr:uno", Port: "ACM0"},
95+
filter: &CreateParams{Fqbn: stringPointer("arduino:avr:uno"), Port: stringPointer("ACM0")},
9296
ports: portsTwoBoards,
9397
want: nil,
9498
},

command/thing/create.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
// CreateParams contains the parameters needed to create a new thing.
1616
type CreateParams struct {
1717
// Optional - contains the name of the thing
18-
Name string
18+
Name *string
1919
// Mandatory - contains the path of the template file
2020
Template string
2121
}
@@ -37,8 +37,8 @@ func Create(params *CreateParams) (string, error) {
3737
}
3838

3939
// Name passed as parameter has priority over name from template
40-
if params.Name != "" {
41-
thing.Name = params.Name
40+
if params.Name != nil {
41+
thing.Name = *params.Name
4242
}
4343
// If name is not specified in the template, it should be passed as parameter
4444
if thing.Name == "" {

0 commit comments

Comments
 (0)