Skip to content

Commit 0efcb1e

Browse files
committed
Support for multiple ota-ids retreival
1 parent c6ad6c9 commit 0efcb1e

File tree

6 files changed

+48
-17
lines changed

6 files changed

+48
-17
lines changed

cli/ota/status.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030

3131
type statusFlags struct {
3232
otaID string
33+
otaIDs string
3334
deviceId string
3435
limit int16
3536
sort string
@@ -49,6 +50,7 @@ func initOtaStatusCommand() *cobra.Command {
4950
},
5051
}
5152
uploadCommand.Flags().StringVarP(&flags.otaID, "ota-id", "o", "", "OTA ID")
53+
uploadCommand.Flags().StringVarP(&flags.otaIDs, "ota-ids", "", "", "OTA IDs (comma separated)")
5254
uploadCommand.Flags().StringVarP(&flags.deviceId, "device-id", "d", "", "Device ID")
5355
uploadCommand.Flags().Int16VarP(&flags.limit, "limit", "l", 10, "Output limit (default: 10)")
5456
uploadCommand.Flags().StringVarP(&flags.sort, "sort", "s", "desc", "Sorting (default: desc)")
@@ -57,7 +59,7 @@ func initOtaStatusCommand() *cobra.Command {
5759
}
5860

5961
func runOtaStatusCommand(flags *statusFlags) error {
60-
if flags.otaID == "" && flags.deviceId == "" {
62+
if flags.otaID == "" && flags.deviceId == "" && flags.otaIDs == "" {
6163
return fmt.Errorf("required flag(s) \"ota-id\" or \"device-id\" or \"ota-ids\" not set")
6264
}
6365

@@ -66,5 +68,5 @@ func runOtaStatusCommand(flags *statusFlags) error {
6668
return fmt.Errorf("retrieving credentials: %w", err)
6769
}
6870

69-
return ota.PrintOtaStatus(flags.otaID, flags.deviceId, cred, int(flags.limit), flags.sort)
71+
return ota.PrintOtaStatus(flags.otaID, flags.otaIDs, flags.deviceId, cred, int(flags.limit), flags.sort)
7072
}

command/ota/massupload.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ type otaUploader interface {
158158
}
159159

160160
type otaStatusGetter interface {
161-
GetOtaLastStatusByDeviceID(deviceID string) (*otaapi.OtaStatusByDeviceResponse, error)
161+
GetOtaLastStatusByDeviceID(deviceID string) (*otaapi.OtaStatusList, error)
162162
}
163163

164164
func run(ctx context.Context, uploader otaUploader, otapi otaStatusGetter, ids []string, otaFile string, expiration int) []Result {

command/ota/massupload_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ func (d *deviceUploaderTest) DeviceOTA(ctx context.Context, id string, file *os.
2424

2525
type otaStatusGetterTest struct{}
2626

27-
func (s *otaStatusGetterTest) GetOtaLastStatusByDeviceID(deviceID string) (*otaapi.OtaStatusByDeviceResponse, error) {
27+
func (s *otaStatusGetterTest) GetOtaLastStatusByDeviceID(deviceID string) (*otaapi.OtaStatusList, error) {
2828
ota := otaapi.Ota{
2929
ID: uuid.Must(uuid.NewV4()).String(),
3030
Status: "in_progress",
3131
StartedAt: "2021-09-01T12:00:00Z",
3232
}
33-
response := &otaapi.OtaStatusByDeviceResponse{
33+
response := &otaapi.OtaStatusList{
3434
Ota: []otaapi.Ota{ota},
3535
}
3636
return response, nil

command/ota/status.go

+10-3
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,25 @@ import (
88
otaapi "github.com/arduino/arduino-cloud-cli/internal/ota-api"
99
)
1010

11-
func PrintOtaStatus(otaid, device string, cred *config.Credentials, limit int, order string) error {
11+
func PrintOtaStatus(otaid, otaids, device string, cred *config.Credentials, limit int, order string) error {
1212

1313
if feedback.GetFormat() == feedback.JSONMini {
1414
return fmt.Errorf("jsonmini format is not supported for this command")
1515
}
1616

1717
otapi := otaapi.NewClient(cred)
1818

19-
if otaid != "" {
19+
if otaids != "" {
20+
res, err := otapi.GetOtaStatusByOtaIDs(otaids)
21+
if err == nil && res != nil {
22+
feedback.PrintResult(res)
23+
} else if err != nil {
24+
return err
25+
}
26+
} else if otaid != "" {
2027
res, err := otapi.GetOtaStatusByOtaID(otaid, limit, order)
2128
if err == nil && res != nil {
22-
feedback.PrintResult(otaapi.OtaStatusPrint{
29+
feedback.PrintResult(otaapi.OtaStatusDetail{
2330
Ota: res.Ota,
2431
Details: res.States,
2532
})

internal/ota-api/client.go

+25-3
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,33 @@ func (c *OtaApiClient) GetOtaStatusByOtaID(otaid string, limit int, order string
134134
return nil, err
135135
}
136136

137-
func (c *OtaApiClient) GetOtaLastStatusByDeviceID(deviceID string) (*OtaStatusByDeviceResponse, error) {
137+
func (c *OtaApiClient) GetOtaStatusByOtaIDs(otaids string) (*OtaStatusList, error) {
138+
139+
ids := strings.Split(otaids, ",")
140+
if len(ids) == 0 {
141+
return nil, fmt.Errorf("invalid ota-ids: empty")
142+
}
143+
144+
returnStatus := OtaStatusList{}
145+
for _, id := range ids {
146+
if id != "" {
147+
resp, err := c.GetOtaStatusByOtaID(id, 1, OrderDesc)
148+
if err != nil {
149+
return nil, err
150+
}
151+
returnStatus.Ota = append(returnStatus.Ota, resp.Ota)
152+
}
153+
154+
}
155+
156+
return &returnStatus, nil
157+
}
158+
159+
func (c *OtaApiClient) GetOtaLastStatusByDeviceID(deviceID string) (*OtaStatusList, error) {
138160
return c.GetOtaStatusByDeviceID(deviceID, 1, OrderDesc)
139161
}
140162

141-
func (c *OtaApiClient) GetOtaStatusByDeviceID(deviceID string, limit int, order string) (*OtaStatusByDeviceResponse, error) {
163+
func (c *OtaApiClient) GetOtaStatusByDeviceID(deviceID string, limit int, order string) (*OtaStatusList, error) {
142164

143165
if deviceID == "" {
144166
return nil, fmt.Errorf("invalid device-id: empty")
@@ -167,7 +189,7 @@ func (c *OtaApiClient) GetOtaStatusByDeviceID(deviceID string, limit int, order
167189
bodyb, err := io.ReadAll(res.Body)
168190

169191
if res.StatusCode == 200 {
170-
var otaResponse OtaStatusByDeviceResponse
192+
var otaResponse OtaStatusList
171193
if err == nil && bodyb != nil {
172194
err = json.Unmarshal(bodyb, &otaResponse)
173195
if err != nil {

internal/ota-api/dto.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ type (
3232
States []State `json:"states,omitempty"`
3333
}
3434

35-
OtaStatusByDeviceResponse struct {
35+
OtaStatusList struct {
3636
Ota []Ota `json:"ota"`
3737
}
3838

@@ -53,17 +53,17 @@ type (
5353
Timestamp string `json:"timestamp,omitempty"`
5454
}
5555

56-
OtaStatusPrint struct {
56+
OtaStatusDetail struct {
5757
Ota Ota `json:"ota"`
5858
Details []State `json:"details,omitempty"`
5959
}
6060
)
6161

62-
func (r OtaStatusByDeviceResponse) Data() interface{} {
62+
func (r OtaStatusList) Data() interface{} {
6363
return r.Ota
6464
}
6565

66-
func (r OtaStatusByDeviceResponse) String() string {
66+
func (r OtaStatusList) String() string {
6767
if len(r.Ota) == 0 {
6868
return ""
6969
}
@@ -125,11 +125,11 @@ func (r Ota) String() string {
125125
return t.Render()
126126
}
127127

128-
func (r OtaStatusPrint) Data() interface{} {
128+
func (r OtaStatusDetail) Data() interface{} {
129129
return r.Ota
130130
}
131131

132-
func (r OtaStatusPrint) String() string {
132+
func (r OtaStatusDetail) String() string {
133133
if r.Ota.ID == "" {
134134
return "No OTA found"
135135
}

0 commit comments

Comments
 (0)