Skip to content

Commit 3eb0590

Browse files
committed
Added reordering
1 parent 9c1ed87 commit 3eb0590

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

command/ota/status.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,15 @@ func PrintOtaStatus(otaid, device string, cred *config.Credentials) error {
1515
var err error
1616
if otaid != "" {
1717
res, err = otapi.GetOtaStatusByOtaID(otaid)
18-
18+
if err == nil {
19+
res = res.(*otaapi.OtaStatusResponse).Ota
20+
}
1921
}
2022
if device != "" {
2123
res, err = otapi.GetOtaStatusByDeviceID(device)
24+
if err == nil {
25+
res = res.(*otaapi.OtaStatusByDeviceResponse).Ota
26+
}
2227
}
2328
if err != nil {
2429
return err

internal/ota-api/client.go

+18
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ import (
2323
"fmt"
2424
"io"
2525
"net/http"
26+
"sort"
2627
"strings"
28+
"time"
2729

2830
"github.com/arduino/arduino-cloud-cli/config"
2931
"github.com/arduino/arduino-cloud-cli/internal/iot"
@@ -131,6 +133,22 @@ func (c *OtaApiClient) GetOtaStatusByDeviceID(deviceID string) (*OtaStatusByDevi
131133
return nil, err
132134
}
133135
}
136+
137+
if len(otaResponse.Ota) > 0 {
138+
// Sort output by StartedAt
139+
sort.Slice(otaResponse.Ota, func(i, j int) bool {
140+
t1, err := time.Parse(time.RFC3339, otaResponse.Ota[i].StartedAt)
141+
if err != nil {
142+
return false
143+
}
144+
t2, err := time.Parse(time.RFC3339, otaResponse.Ota[j].StartedAt)
145+
if err != nil {
146+
return false
147+
}
148+
return t1.After(t2)
149+
})
150+
}
151+
134152
return &otaResponse, nil
135153
}
136154

internal/ota-api/dto.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,21 @@ package otaapi
2020
type (
2121
OtaStatusResponse struct {
2222
Ota Ota `json:"ota"`
23-
States []State `json:"states"`
23+
States []State `json:"states,omitempty"`
2424
}
2525

2626
OtaStatusByDeviceResponse struct {
2727
Ota []Ota `json:"ota"`
2828
}
2929

3030
Ota struct {
31+
ID string `json:"id"`
3132
DeviceID string `json:"device_id"`
33+
Status string `json:"status"`
34+
StartedAt string `json:"started_at"`
3235
EndedAt string `json:"ended_at"`
33-
ErrorReason string `json:"error_reason"`
34-
ID string `json:"id"`
36+
ErrorReason string `json:"error_reason,omitempty"`
3537
Sha256 string `json:"sha256"`
36-
StartedAt string `json:"started_at"`
37-
Status string `json:"status"`
3838
}
3939

4040
State struct {

0 commit comments

Comments
 (0)