Skip to content

Commit 6dcc079

Browse files
committed
Added sort/limit
1 parent 44b333e commit 6dcc079

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

cli/ota/status.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ import (
3131
type statusFlags struct {
3232
otaID string
3333
deviceId string
34+
limit int16
35+
sort string
3436
}
3537

3638
func initOtaStatusCommand() *cobra.Command {
@@ -48,6 +50,8 @@ func initOtaStatusCommand() *cobra.Command {
4850
}
4951
uploadCommand.Flags().StringVarP(&flags.otaID, "ota-id", "o", "", "OTA ID")
5052
uploadCommand.Flags().StringVarP(&flags.deviceId, "device-id", "d", "", "Device ID")
53+
uploadCommand.Flags().Int16VarP(&flags.limit, "limit", "l", 10, "Output limit (default: 10)")
54+
uploadCommand.Flags().StringVarP(&flags.sort, "sort", "s", "desc", "Sorting (default: desc)")
5155

5256
return uploadCommand
5357
}
@@ -62,5 +66,5 @@ func runOtaStatusCommand(flags *statusFlags) error {
6266
return fmt.Errorf("retrieving credentials: %w", err)
6367
}
6468

65-
return ota.PrintOtaStatus(flags.otaID, flags.deviceId, cred)
69+
return ota.PrintOtaStatus(flags.otaID, flags.deviceId, cred, int(flags.limit), flags.sort)
6670
}

command/ota/status.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func printOut(res any) {
5353
}
5454
}
5555

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

5858
if feedback.GetFormat() == feedback.JSONMini {
5959
return fmt.Errorf("jsonmini format is not supported for this command")
@@ -72,7 +72,7 @@ func PrintOtaStatus(otaid, device string, cred *config.Credentials) error {
7272
return err
7373
}
7474
} else if device != "" {
75-
res, err := otapi.GetOtaStatusByDeviceID(device)
75+
res, err := otapi.GetOtaStatusByDeviceID(device, limit, order)
7676
if err == nil && res != nil {
7777
formatOutputSlice(res.Ota)
7878
} else if err != nil {

internal/ota-api/client.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ import (
2323
"fmt"
2424
"io"
2525
"net/http"
26-
"sort"
2726
"strings"
28-
"time"
2927

3028
"github.com/arduino/arduino-cloud-cli/config"
3129
"github.com/arduino/arduino-cloud-cli/internal/iot"
@@ -105,7 +103,7 @@ func (c *OtaApiClient) GetOtaStatusByOtaID(otaid string) (*OtaStatusResponse, er
105103
return nil, err
106104
}
107105

108-
func (c *OtaApiClient) GetOtaStatusByDeviceID(deviceID string) (*OtaStatusByDeviceResponse, error) {
106+
func (c *OtaApiClient) GetOtaStatusByDeviceID(deviceID string, limit int, order string) (*OtaStatusByDeviceResponse, error) {
109107

110108
if deviceID == "" {
111109
return nil, fmt.Errorf("invalid device-id: empty")
@@ -120,6 +118,12 @@ func (c *OtaApiClient) GetOtaStatusByDeviceID(deviceID string) (*OtaStatusByDevi
120118
}
121119

122120
endpoint := c.host + "/ota/v1/ota?device_id=" + deviceID
121+
if limit > 0 {
122+
endpoint += "&limit=" + fmt.Sprintf("%d", limit)
123+
}
124+
if order != "" && (order == "asc" || order == "desc") {
125+
endpoint += "&order=" + order
126+
}
123127
res, err := c.performGetRequest(endpoint, userRequestToken.AccessToken)
124128
if err != nil {
125129
return nil, err
@@ -136,6 +140,7 @@ func (c *OtaApiClient) GetOtaStatusByDeviceID(deviceID string) (*OtaStatusByDevi
136140
}
137141
}
138142

143+
/*
139144
if len(otaResponse.Ota) > 0 {
140145
// Sort output by StartedAt
141146
sort.Slice(otaResponse.Ota, func(i, j int) bool {
@@ -150,6 +155,7 @@ func (c *OtaApiClient) GetOtaStatusByDeviceID(deviceID string) (*OtaStatusByDevi
150155
return t1.After(t2)
151156
})
152157
}
158+
*/
153159
return &otaResponse, nil
154160
} else if res.StatusCode == 404 || res.StatusCode == 400 {
155161
return nil, fmt.Errorf("device-id %s not found", deviceID)

0 commit comments

Comments
 (0)