Skip to content

Commit 46ab492

Browse files
committed
Support for extended/reduced output
1 parent 3eb0590 commit 46ab492

File tree

4 files changed

+50
-13
lines changed

4 files changed

+50
-13
lines changed

cli/ota/status.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
type statusFlags struct {
3232
otaID string
3333
deviceId string
34+
wide bool
3435
}
3536

3637
func initOtaStatusCommand() *cobra.Command {
@@ -48,6 +49,7 @@ func initOtaStatusCommand() *cobra.Command {
4849
}
4950
uploadCommand.Flags().StringVarP(&flags.otaID, "ota-id", "o", "", "OTA ID")
5051
uploadCommand.Flags().StringVarP(&flags.deviceId, "device-id", "d", "", "Device ID")
52+
uploadCommand.Flags().BoolVarP(&flags.wide, "wide", "w", false, "Show more status details")
5153

5254
return uploadCommand
5355
}
@@ -62,5 +64,5 @@ func runOtaStatusCommand(flags *statusFlags) error {
6264
return fmt.Errorf("retrieving credentials: %w", err)
6365
}
6466

65-
return ota.PrintOtaStatus(flags.otaID, flags.deviceId, cred)
67+
return ota.PrintOtaStatus(flags.otaID, flags.deviceId, flags.wide, cred)
6668
}

command/ota/status.go

+37-6
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,60 @@ import (
88
otaapi "github.com/arduino/arduino-cloud-cli/internal/ota-api"
99
)
1010

11-
func PrintOtaStatus(otaid, device string, cred *config.Credentials) error {
11+
func formatOutput(wideOutput bool, status any) {
12+
if st, ok := status.(otaapi.Ota); ok {
13+
if !wideOutput {
14+
st.ID = ""
15+
st.DeviceID = ""
16+
st.ErrorReason = ""
17+
st.Sha256 = ""
18+
}
19+
printJson(st)
20+
}
21+
}
22+
23+
func formatOutputSlice(wideOutput bool, st []otaapi.Ota) {
24+
if st == nil {
25+
st = []otaapi.Ota{}
26+
}
27+
for i := range st {
28+
if !wideOutput {
29+
st[i].ID = ""
30+
st[i].DeviceID = ""
31+
st[i].ErrorReason = ""
32+
st[i].Sha256 = ""
33+
}
34+
}
35+
printJson(st)
36+
}
37+
38+
func printJson(res any) {
39+
jsonRes, _ := json.MarshalIndent(res, "", " ")
40+
if jsonRes != nil {
41+
fmt.Println(string(jsonRes))
42+
}
43+
}
44+
45+
func PrintOtaStatus(otaid, device string, wideOutput bool, cred *config.Credentials) error {
1246
otapi := otaapi.NewClient(cred)
1347

1448
var res any
1549
var err error
1650
if otaid != "" {
1751
res, err = otapi.GetOtaStatusByOtaID(otaid)
1852
if err == nil {
19-
res = res.(*otaapi.OtaStatusResponse).Ota
53+
formatOutput(wideOutput, res.(*otaapi.OtaStatusResponse).Ota)
2054
}
2155
}
2256
if device != "" {
2357
res, err = otapi.GetOtaStatusByDeviceID(device)
2458
if err == nil {
25-
res = res.(*otaapi.OtaStatusByDeviceResponse).Ota
59+
formatOutputSlice(wideOutput, res.(*otaapi.OtaStatusByDeviceResponse).Ota)
2660
}
2761
}
2862
if err != nil {
2963
return err
3064
}
3165

32-
jsonRes, _ := json.MarshalIndent(res, "", "\t")
33-
fmt.Println(string(jsonRes))
34-
3566
return nil
3667
}

internal/ota-api/client.go

+4
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ func (c *OtaApiClient) GetOtaStatusByOtaID(otaid string) (*OtaStatusResponse, er
9898
}
9999
}
100100
return &otaResponse, nil
101+
} else if res.StatusCode == 404 || res.StatusCode == 400 {
102+
return nil, fmt.Errorf("ota-id %s not found", otaid)
101103
}
102104

103105
return nil, err
@@ -147,6 +149,8 @@ func (c *OtaApiClient) GetOtaStatusByDeviceID(deviceID string) (*OtaStatusByDevi
147149
}
148150
return t1.After(t2)
149151
})
152+
} else if res.StatusCode == 404 || res.StatusCode == 400 {
153+
return nil, fmt.Errorf("device-id %s not found", deviceID)
150154
}
151155

152156
return &otaResponse, nil

internal/ota-api/dto.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,19 @@ type (
2828
}
2929

3030
Ota struct {
31-
ID string `json:"id"`
32-
DeviceID string `json:"device_id"`
31+
ID string `json:"id,omitempty"`
32+
DeviceID string `json:"device_id,omitempty"`
3333
Status string `json:"status"`
3434
StartedAt string `json:"started_at"`
35-
EndedAt string `json:"ended_at"`
35+
EndedAt string `json:"ended_at,omitempty"`
3636
ErrorReason string `json:"error_reason,omitempty"`
37-
Sha256 string `json:"sha256"`
37+
Sha256 string `json:"sha256,omitempty"`
3838
}
3939

4040
State struct {
4141
OtaID string `json:"ota_id"`
4242
State string `json:"state"`
43-
StateData string `json:"state_data"`
44-
Timestamp string `json:"timestamp"`
43+
StateData string `json:"state_data,omitempty"`
44+
Timestamp string `json:"timestamp,omitempty"`
4545
}
4646
)

0 commit comments

Comments
 (0)