|
18 | 18 | package otaapi
|
19 | 19 |
|
20 | 20 | import (
|
21 |
| - "encoding/json" |
22 |
| - "fmt" |
23 | 21 | "time"
|
24 | 22 |
|
25 |
| - "github.com/arduino/arduino-cli/cli/feedback" |
26 |
| - "gopkg.in/yaml.v3" |
| 23 | + "github.com/arduino/arduino-cli/table" |
27 | 24 | )
|
28 | 25 |
|
29 | 26 | type (
|
@@ -59,46 +56,120 @@ type (
|
59 | 56 | }
|
60 | 57 | )
|
61 | 58 |
|
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") |
65 | 80 | }
|
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() |
67 | 92 | }
|
68 | 93 |
|
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 |
73 | 106 | }
|
74 |
| - return out |
75 | 107 | }
|
76 | 108 |
|
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 "" |
84 | 116 | }
|
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") |
90 | 124 | }
|
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) |
93 | 130 | }
|
| 131 | + t.AddRow(line...) |
| 132 | + |
| 133 | + return t.Render() |
94 | 134 | }
|
95 | 135 |
|
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") |
100 | 151 | }
|
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 |
102 | 173 | }
|
103 | 174 |
|
104 | 175 | func formatHumanReadableTs(ts string) string {
|
|
0 commit comments