Skip to content

Commit 89e9776

Browse files
committed
Major output refactoring
1 parent 6ae26b4 commit 89e9776

File tree

6 files changed

+117
-97
lines changed

6 files changed

+117
-97
lines changed

cli/ota/massupload.go

+5-12
Original file line numberDiff line numberDiff line change
@@ -148,22 +148,15 @@ func (r massUploadResult) String() string {
148148
if r.Err != nil {
149149
outcome = fmt.Sprintf("Fail: %s", r.Err.Error())
150150
}
151-
if r.OtaStatus.Status != ""{
152-
switch r.OtaStatus.Status {
153-
case "in_progress":
154-
outcome = "In progress"
155-
case "failed":
156-
outcome = "Failed"
157-
case "pending":
158-
outcome = "Pending"
159-
}
151+
if r.OtaStatus.Status != "" {
152+
outcome = r.OtaStatus.MapStatus()
160153
}
161154

155+
line := []interface{}{r.ID, r.OtaStatus.ID, outcome}
162156
if hasErrorReason {
163-
t.AddRow(r.ID, r.OtaStatus.ID, outcome, r.OtaStatus.ErrorReason)
164-
} else {
165-
t.AddRow(r.ID, r.OtaStatus.ID, outcome)
157+
line = append(line, r.OtaStatus.ErrorReason)
166158
}
159+
t.AddRow(line...)
167160
}
168161
return t.Render()
169162
}

cli/ota/status.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ import (
2929
)
3030

3131
type statusFlags struct {
32-
otaID string
33-
deviceId string
34-
limit int16
35-
sort string
32+
otaID string
33+
deviceId string
34+
limit int16
35+
sort string
3636
}
3737

3838
func initOtaStatusCommand() *cobra.Command {

command/ota/massupload.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ func run(ctx context.Context, uploader otaUploader, otapi otaStatusGetter, ids [
193193
if otaapierr == nil && otaID != nil && len(otaID.Ota) > 0 {
194194
otaResult.OtaStatus = otaID.Ota[0]
195195
}
196-
196+
197197
resCh <- otaResult
198198
}
199199
}()

command/ota/status.go

+2-47
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,13 @@
11
package ota
22

33
import (
4-
"encoding/json"
54
"fmt"
65

76
"github.com/arduino/arduino-cli/cli/feedback"
87
"github.com/arduino/arduino-cloud-cli/config"
98
otaapi "github.com/arduino/arduino-cloud-cli/internal/ota-api"
10-
"gopkg.in/yaml.v3"
119
)
1210

13-
func formatOtaByIdOutput(st *otaapi.OtaStatusPrint) {
14-
printOut(st)
15-
}
16-
17-
func formatOutputSlice(st []otaapi.Ota) {
18-
if st == nil {
19-
st = []otaapi.Ota{}
20-
}
21-
printOut(st)
22-
}
23-
24-
func printOut(res any) {
25-
outputformat := feedback.GetFormat()
26-
if outputformat == feedback.JSON || outputformat == feedback.JSONMini {
27-
jsonRes, _ := json.MarshalIndent(res, "", " ")
28-
if jsonRes != nil {
29-
fmt.Println(string(jsonRes))
30-
}
31-
}
32-
if outputformat == feedback.YAML {
33-
yamlRes, _ := yaml.Marshal(res)
34-
if yamlRes != nil {
35-
fmt.Println(string(yamlRes))
36-
}
37-
}
38-
if outputformat == feedback.Text {
39-
if st, ok := res.(*otaapi.OtaStatusPrint); ok {
40-
fmt.Println(st.Ota.ToText())
41-
fmt.Println()
42-
fmt.Println("Details:")
43-
for i := range st.Details {
44-
fmt.Println(st.Details[i].ToText())
45-
}
46-
}
47-
if st, ok := res.([]otaapi.Ota); ok {
48-
49-
for i := range st {
50-
fmt.Println(st[i].ToLine())
51-
}
52-
}
53-
}
54-
}
55-
5611
func PrintOtaStatus(otaid, device string, cred *config.Credentials, limit int, order string) error {
5712

5813
if feedback.GetFormat() == feedback.JSONMini {
@@ -64,7 +19,7 @@ func PrintOtaStatus(otaid, device string, cred *config.Credentials, limit int, o
6419
if otaid != "" {
6520
res, err := otapi.GetOtaStatusByOtaID(otaid, limit, order)
6621
if err == nil && res != nil {
67-
formatOtaByIdOutput(&otaapi.OtaStatusPrint{
22+
feedback.PrintResult(otaapi.OtaStatusPrint{
6823
Ota: res.Ota,
6924
Details: res.States,
7025
})
@@ -74,7 +29,7 @@ func PrintOtaStatus(otaid, device string, cred *config.Credentials, limit int, o
7429
} else if device != "" {
7530
res, err := otapi.GetOtaStatusByDeviceID(device, limit, order)
7631
if err == nil && res != nil {
77-
formatOutputSlice(res.Ota)
32+
feedback.PrintResult(res)
7833
} else if err != nil {
7934
return err
8035
}

command/ota/upload.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"os"
2424
"path/filepath"
2525

26+
"github.com/arduino/arduino-cli/cli/feedback"
2627
"github.com/arduino/arduino-cloud-cli/config"
2728
"github.com/arduino/arduino-cloud-cli/internal/iot"
2829
otaapi "github.com/arduino/arduino-cloud-cli/internal/ota-api"
@@ -90,7 +91,7 @@ func Upload(ctx context.Context, params *UploadParams, cred *config.Credentials)
9091
return err
9192
}
9293
if otaID != nil && len(otaID.Ota) > 0 {
93-
otaID.Ota[0].Format()
94+
feedback.PrintResult(otaID.Ota[0])
9495
}
9596

9697
return nil

internal/ota-api/dto.go

+103-32
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,9 @@
1818
package otaapi
1919

2020
import (
21-
"encoding/json"
22-
"fmt"
2321
"time"
2422

25-
"github.com/arduino/arduino-cli/cli/feedback"
26-
"gopkg.in/yaml.v3"
23+
"github.com/arduino/arduino-cli/table"
2724
)
2825

2926
type (
@@ -59,46 +56,120 @@ type (
5956
}
6057
)
6158

62-
func (o Ota) ToLine() string {
63-
if o.ErrorReason != "" {
64-
return fmt.Sprintf("ota_id: %s, started_at: %s, ended_at: %s, status: %s, error_reason: %s", o.ID, formatHumanReadableTs(o.StartedAt), formatHumanReadableTs(o.EndedAt), o.Status, o.ErrorReason)
59+
func (r OtaStatusByDeviceResponse) Data() interface{} {
60+
return r.Ota
61+
}
62+
63+
func (r OtaStatusByDeviceResponse) String() string {
64+
if len(r.Ota) == 0 {
65+
return ""
66+
}
67+
t := table.New()
68+
hasErrorReason := false
69+
for _, r := range r.Ota {
70+
if r.ErrorReason != "" {
71+
hasErrorReason = true
72+
break
73+
}
74+
}
75+
76+
if hasErrorReason {
77+
t.SetHeader("Device ID", "Ota ID", "Status", "Started At", "Ended At", "Error Reason")
78+
} else {
79+
t.SetHeader("Device ID", "Ota ID", "Status", "Started At", "Ended At")
6580
}
66-
return fmt.Sprintf("ota_id: %s, started_at: %s, ended_at: %s, status: %s", o.ID, formatHumanReadableTs(o.StartedAt), formatHumanReadableTs(o.EndedAt), o.Status)
81+
82+
// Now print the table
83+
for _, r := range r.Ota {
84+
line := []any{r.DeviceID, r.ID, r.MapStatus(), formatHumanReadableTs(r.StartedAt), formatHumanReadableTs(r.EndedAt)}
85+
if hasErrorReason {
86+
line = append(line, r.ErrorReason)
87+
}
88+
t.AddRow(line...)
89+
}
90+
91+
return t.Render()
6792
}
6893

69-
func (o Ota) ToText() string {
70-
out := fmt.Sprintf("ota_id: %s\nstatus: %s\nstarted_at: %s\nended_at: %s\ndevice_id: %s", o.ID, o.Status, formatHumanReadableTs(o.StartedAt), formatHumanReadableTs(o.EndedAt), o.DeviceID)
71-
if o.ErrorReason != "" {
72-
out += fmt.Sprintf("\nerror_reason: %s", o.ErrorReason)
94+
func (o Ota) MapStatus() string {
95+
switch o.Status {
96+
case "in_progress":
97+
return "In progress"
98+
case "failed":
99+
return "Failed"
100+
case "pending":
101+
return "Pending"
102+
case "succeeded":
103+
return "Succeeded"
104+
default:
105+
return o.Status
73106
}
74-
return out
75107
}
76108

77-
func (o Ota) Format() {
78-
outputformat := feedback.GetFormat()
79-
if outputformat == feedback.JSON || outputformat == feedback.JSONMini {
80-
jsonRes, _ := json.MarshalIndent(o, "", " ")
81-
if jsonRes != nil {
82-
fmt.Println(string(jsonRes))
83-
}
109+
func (r Ota) Data() interface{} {
110+
return r
111+
}
112+
113+
func (r Ota) String() string {
114+
if len(r.ID) == 0 {
115+
return ""
84116
}
85-
if outputformat == feedback.YAML {
86-
yamlRes, _ := yaml.Marshal(o)
87-
if yamlRes != nil {
88-
fmt.Println(string(yamlRes))
89-
}
117+
t := table.New()
118+
hasErrorReason := r.ErrorReason != ""
119+
120+
if hasErrorReason {
121+
t.SetHeader("Device ID", "Ota ID", "Status", "Started At", "Ended At", "Error Reason")
122+
} else {
123+
t.SetHeader("Device ID", "Ota ID", "Status", "Started At", "Ended At")
90124
}
91-
if outputformat == feedback.Text {
92-
fmt.Println(o.ToLine())
125+
126+
// Now print the table
127+
line := []any{r.DeviceID, r.DeviceID, r.MapStatus(), formatHumanReadableTs(r.StartedAt), formatHumanReadableTs(r.EndedAt)}
128+
if hasErrorReason {
129+
line = append(line, r.ErrorReason)
93130
}
131+
t.AddRow(line...)
132+
133+
return t.Render()
94134
}
95135

96-
func (s State) ToText() string {
97-
rep := fmt.Sprintf("time: %s, status: %s", formatHumanReadableTs(s.Timestamp), s.State)
98-
if s.StateData != "" {
99-
rep += fmt.Sprintf(", detail: %s", s.StateData)
136+
func (r OtaStatusPrint) Data() interface{} {
137+
return r.Ota
138+
}
139+
140+
func (r OtaStatusPrint) String() string {
141+
if r.Ota.ID == "" {
142+
return "No OTA found"
143+
}
144+
t := table.New()
145+
hasErrorReason := r.Ota.ErrorReason != ""
146+
147+
if hasErrorReason {
148+
t.SetHeader("Device ID", "Ota ID", "Status", "Started At", "Ended At", "Error Reason")
149+
} else {
150+
t.SetHeader("Device ID", "Ota ID", "Status", "Started At", "Ended At")
100151
}
101-
return rep
152+
153+
// Now print the table
154+
line := []any{r.Ota.DeviceID, r.Ota.ID, r.Ota.MapStatus(), formatHumanReadableTs(r.Ota.StartedAt), formatHumanReadableTs(r.Ota.EndedAt)}
155+
if hasErrorReason {
156+
line = append(line, r.Ota.ErrorReason)
157+
}
158+
t.AddRow(line...)
159+
160+
output := t.Render()
161+
162+
// Add details
163+
if len(r.Details) > 0 {
164+
t = table.New()
165+
t.SetHeader("Time", "Status", "Detail")
166+
for _, s := range r.Details {
167+
t.AddRow(formatHumanReadableTs(s.Timestamp), s.State, s.StateData)
168+
}
169+
output += "\nDetails:\n" + t.Render()
170+
}
171+
172+
return output
102173
}
103174

104175
func formatHumanReadableTs(ts string) string {

0 commit comments

Comments
 (0)