Skip to content

Commit c5f14b0

Browse files
committed
refactored
1 parent 435af5b commit c5f14b0

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed

command/ota/status.go

+13-16
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,13 @@ import (
1010
"gopkg.in/yaml.v3"
1111
)
1212

13-
func formatOutput(wideOutput bool, status any) {
14-
if st, ok := status.(otaapi.Ota); ok {
15-
if !wideOutput {
16-
st.DeviceID = ""
17-
st.ErrorReason = ""
18-
st.Sha256 = ""
19-
}
20-
printOut(st, wideOutput)
13+
func formatOtaByIdOutput(wideOutput bool, st *otaapi.OtaStatusResponse) {
14+
if !wideOutput {
15+
st.Ota.DeviceID = ""
16+
st.Ota.ErrorReason = ""
17+
st.Ota.Sha256 = ""
2118
}
19+
printOut(st, wideOutput)
2220
}
2321

2422
func formatOutputSlice(wideOutput bool, st []otaapi.Ota) {
@@ -50,13 +48,12 @@ func printOut(res any, wideOutput bool) {
5048
}
5149
}
5250
if outputformat == feedback.Text {
53-
if st, ok := res.(otaapi.Ota); ok {
54-
if wideOutput {
55-
fmt.Println(st.GetWideCSVHeader())
56-
fmt.Println(st.ToWideCSV())
57-
} else {
58-
fmt.Println(st.GetCSVHeader())
59-
fmt.Println(st.ToCSV())
51+
if st, ok := res.(*otaapi.OtaStatusResponse); ok {
52+
fmt.Println(st.Ota.ToText())
53+
fmt.Println()
54+
fmt.Println("Details:")
55+
for i := range st.States {
56+
fmt.Println(st.States[i].ToText())
6057
}
6158
}
6259
if st, ok := res.([]otaapi.Ota); ok {
@@ -92,7 +89,7 @@ func PrintOtaStatus(otaid, device, otaidpath string, wideOutput bool, cred *conf
9289
} else if otaid != "" {
9390
res, err := otapi.GetOtaStatusByOtaID(otaid)
9491
if err == nil && res != nil {
95-
formatOutput(wideOutput, res.Ota)
92+
formatOtaByIdOutput(wideOutput, res)
9693
} else if err != nil {
9794
return err
9895
}

internal/ota-api/dto.go

+12
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,15 @@ func (o Ota) GetCSVHeader() string {
6262
func (o Ota) ToCSV() string {
6363
return fmt.Sprintf("%s,%s,%s,%s", o.ID, o.Status, o.StartedAt, o.EndedAt)
6464
}
65+
66+
func (o Ota) ToText() string {
67+
out := fmt.Sprintf("ID: %s\nStatus: %s\nStartedAt: %s\nEndedAt: %s", o.ID, o.Status, o.StartedAt, o.EndedAt)
68+
if o.ErrorReason != "" {
69+
out += fmt.Sprintf("\nErrorReason: %s", o.ErrorReason)
70+
}
71+
return out
72+
}
73+
74+
func (s State) ToText() string {
75+
return fmt.Sprintf("Timestamp: %s, State: %s", s.Timestamp, s.State)
76+
}

0 commit comments

Comments
 (0)