@@ -23,7 +23,9 @@ import (
23
23
"fmt"
24
24
"io"
25
25
"net/http"
26
+ "sort"
26
27
"strings"
28
+ "time"
27
29
28
30
"github.com/arduino/arduino-cloud-cli/config"
29
31
"github.com/arduino/arduino-cloud-cli/internal/iot"
@@ -65,7 +67,7 @@ func (c *OtaApiClient) performGetRequest(endpoint, token string) (*http.Response
65
67
return res , nil
66
68
}
67
69
68
- func (c * OtaApiClient ) GetOtaStatusByOtaID (otaid string ) (* OtaStatusResponse , error ) {
70
+ func (c * OtaApiClient ) GetOtaStatusByOtaID (otaid string , limit int , order string ) (* OtaStatusResponse , error ) {
69
71
70
72
if otaid == "" {
71
73
return nil , fmt .Errorf ("invalid ota-id: empty" )
@@ -95,6 +97,28 @@ func (c *OtaApiClient) GetOtaStatusByOtaID(otaid string) (*OtaStatusResponse, er
95
97
return nil , err
96
98
}
97
99
}
100
+
101
+ if len (otaResponse .States ) > 0 {
102
+ // Sort output by StartedAt
103
+ sort .Slice (otaResponse .States , func (i , j int ) bool {
104
+ t1 , err := time .Parse (time .RFC3339 , otaResponse .States [i ].Timestamp )
105
+ if err != nil {
106
+ return false
107
+ }
108
+ t2 , err := time .Parse (time .RFC3339 , otaResponse .States [j ].Timestamp )
109
+ if err != nil {
110
+ return false
111
+ }
112
+ if order == "asc" {
113
+ return t1 .Before (t2 )
114
+ }
115
+ return t1 .After (t2 )
116
+ })
117
+ if limit > 0 && len (otaResponse .States ) > limit {
118
+ otaResponse .States = otaResponse .States [:limit ]
119
+ }
120
+ }
121
+
98
122
return & otaResponse , nil
99
123
} else if res .StatusCode == 404 || res .StatusCode == 400 {
100
124
return nil , fmt .Errorf ("ota-id %s not found" , otaid )
@@ -139,23 +163,6 @@ func (c *OtaApiClient) GetOtaStatusByDeviceID(deviceID string, limit int, order
139
163
return nil , err
140
164
}
141
165
}
142
-
143
- /*
144
- if len(otaResponse.Ota) > 0 {
145
- // Sort output by StartedAt
146
- sort.Slice(otaResponse.Ota, func(i, j int) bool {
147
- t1, err := time.Parse(time.RFC3339, otaResponse.Ota[i].StartedAt)
148
- if err != nil {
149
- return false
150
- }
151
- t2, err := time.Parse(time.RFC3339, otaResponse.Ota[j].StartedAt)
152
- if err != nil {
153
- return false
154
- }
155
- return t1.After(t2)
156
- })
157
- }
158
- */
159
166
return & otaResponse , nil
160
167
} else if res .StatusCode == 404 || res .StatusCode == 400 {
161
168
return nil , fmt .Errorf ("device-id %s not found" , deviceID )
0 commit comments