Skip to content

Commit 44b333e

Browse files
committed
output refactoring
1 parent 6d5e6f5 commit 44b333e

File tree

3 files changed

+18
-56
lines changed

3 files changed

+18
-56
lines changed

cli/ota/status.go

+2-6
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ import (
3131
type statusFlags struct {
3232
otaID string
3333
deviceId string
34-
otaIdsFilePath string
35-
wide bool
3634
}
3735

3836
func initOtaStatusCommand() *cobra.Command {
@@ -50,14 +48,12 @@ func initOtaStatusCommand() *cobra.Command {
5048
}
5149
uploadCommand.Flags().StringVarP(&flags.otaID, "ota-id", "o", "", "OTA ID")
5250
uploadCommand.Flags().StringVarP(&flags.deviceId, "device-id", "d", "", "Device ID")
53-
uploadCommand.Flags().StringVarP(&flags.otaIdsFilePath, "ota-ids", "i", "", "OTA IDs file path. If set, the command will read the OTA IDs from the file and get the status for each OTA ID.")
54-
uploadCommand.Flags().BoolVarP(&flags.wide, "wide", "w", false, "Show more status details")
5551

5652
return uploadCommand
5753
}
5854

5955
func runOtaStatusCommand(flags *statusFlags) error {
60-
if flags.otaID == "" && flags.deviceId == "" && flags.otaIdsFilePath == "" {
56+
if flags.otaID == "" && flags.deviceId == "" {
6157
return fmt.Errorf("required flag(s) \"ota-id\" or \"device-id\" or \"ota-ids\" not set")
6258
}
6359

@@ -66,5 +62,5 @@ func runOtaStatusCommand(flags *statusFlags) error {
6662
return fmt.Errorf("retrieving credentials: %w", err)
6763
}
6864

69-
return ota.PrintOtaStatus(flags.otaID, flags.deviceId, flags.otaIdsFilePath, flags.wide, cred)
65+
return ota.PrintOtaStatus(flags.otaID, flags.deviceId, cred)
7066
}

command/ota/status.go

+10-35
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,18 @@ import (
1010
"gopkg.in/yaml.v3"
1111
)
1212

13-
func formatOtaByIdOutput(wideOutput bool, st *otaapi.OtaStatusPrint) {
14-
if !wideOutput {
15-
st.Ota.DeviceID = ""
16-
st.Ota.ErrorReason = ""
17-
st.Ota.Sha256 = ""
18-
}
19-
printOut(st, wideOutput)
13+
func formatOtaByIdOutput( st *otaapi.OtaStatusPrint) {
14+
printOut(st)
2015
}
2116

22-
func formatOutputSlice(wideOutput bool, st []otaapi.Ota) {
17+
func formatOutputSlice(st []otaapi.Ota) {
2318
if st == nil {
2419
st = []otaapi.Ota{}
2520
}
26-
for i := range st {
27-
if !wideOutput {
28-
st[i].DeviceID = ""
29-
st[i].ErrorReason = ""
30-
st[i].Sha256 = ""
31-
}
32-
}
33-
printOut(st, wideOutput)
21+
printOut(st)
3422
}
3523

36-
func printOut(res any, wideOutput bool) {
24+
func printOut(res any) {
3725
outputformat := feedback.GetFormat()
3826
if outputformat == feedback.JSON || outputformat == feedback.JSONMini {
3927
jsonRes, _ := json.MarshalIndent(res, "", " ")
@@ -59,37 +47,24 @@ func printOut(res any, wideOutput bool) {
5947
if st, ok := res.([]otaapi.Ota); ok {
6048

6149
for i := range st {
62-
if i == 0 {
63-
if wideOutput {
64-
fmt.Println(st[i].GetWideCSVHeader())
65-
} else {
66-
fmt.Println(st[i].GetCSVHeader())
67-
}
68-
}
69-
if wideOutput {
70-
fmt.Println(st[i].ToWideCSV())
71-
} else {
72-
fmt.Println(st[i].ToCSV())
73-
}
50+
fmt.Println(st[i].ToLine())
7451
}
7552
}
7653
}
7754
}
7855

79-
func PrintOtaStatus(otaid, device, otaidpath string, wideOutput bool, cred *config.Credentials) error {
56+
func PrintOtaStatus(otaid, device string, cred *config.Credentials) error {
8057

8158
if feedback.GetFormat() == feedback.JSONMini {
8259
return fmt.Errorf("jsonmini format is not supported for this command")
8360
}
8461

8562
otapi := otaapi.NewClient(cred)
8663

87-
if otaidpath != "" {
88-
// TODO: implement
89-
} else if otaid != "" {
64+
if otaid != "" {
9065
res, err := otapi.GetOtaStatusByOtaID(otaid)
9166
if err == nil && res != nil {
92-
formatOtaByIdOutput(wideOutput, &otaapi.OtaStatusPrint{
67+
formatOtaByIdOutput(&otaapi.OtaStatusPrint{
9368
Ota: res.Ota,
9469
Details: res.States,
9570
})
@@ -99,7 +74,7 @@ func PrintOtaStatus(otaid, device, otaidpath string, wideOutput bool, cred *conf
9974
} else if device != "" {
10075
res, err := otapi.GetOtaStatusByDeviceID(device)
10176
if err == nil && res != nil {
102-
formatOutputSlice(wideOutput, res.Ota)
77+
formatOutputSlice(res.Ota)
10378
} else if err != nil {
10479
return err
10580
}

internal/ota-api/dto.go

+6-15
Original file line numberDiff line numberDiff line change
@@ -52,24 +52,15 @@ type (
5252
}
5353
)
5454

55-
func (o Ota) GetWideCSVHeader() string {
56-
return "ota_id,device_id,status,started_at,ended_at,error_reason"
57-
}
58-
59-
func (o Ota) ToWideCSV() string {
60-
return fmt.Sprintf("%s,%s,%s,%s,%s,%s", o.ID, o.DeviceID, o.Status, o.StartedAt, o.EndedAt, o.ErrorReason)
61-
}
62-
63-
func (o Ota) GetCSVHeader() string {
64-
return "ota_id,status,started_at,ended_at"
65-
}
66-
67-
func (o Ota) ToCSV() string {
68-
return fmt.Sprintf("%s,%s,%s,%s", o.ID, o.Status, o.StartedAt, o.EndedAt)
55+
func (o Ota) ToLine() string {
56+
if o.ErrorReason != "" {
57+
return fmt.Sprintf("ota_id: %s, started_at: %s, ended_at: %s, status: %s, error_reason: %s", o.ID, o.StartedAt, o.EndedAt, o.Status, o.ErrorReason)
58+
}
59+
return fmt.Sprintf("ota_id: %s, started_at: %s, ended_at: %s, status: %s", o.ID, o.StartedAt, o.EndedAt, o.Status)
6960
}
7061

7162
func (o Ota) ToText() string {
72-
out := fmt.Sprintf("ota_id: %s\nstatus: %s\nstarted_at: %s\nended_at: %s", o.ID, o.Status, o.StartedAt, o.EndedAt)
63+
out := fmt.Sprintf("ota_id: %s\nstatus: %s\nstarted_at: %s\nended_at: %s\ndevice_id: %s", o.ID, o.Status, o.StartedAt, o.EndedAt, o.DeviceID)
7364
if o.ErrorReason != "" {
7465
out += fmt.Sprintf("\nerror_reason: %s", o.ErrorReason)
7566
}

0 commit comments

Comments
 (0)