Skip to content

Commit 435af5b

Browse files
committed
Added variable status output
1 parent cf2dbf5 commit 435af5b

File tree

2 files changed

+80
-19
lines changed

2 files changed

+80
-19
lines changed

command/ota/status.go

+55-12
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import (
44
"encoding/json"
55
"fmt"
66

7+
"github.com/arduino/arduino-cli/cli/feedback"
78
"github.com/arduino/arduino-cloud-cli/config"
89
otaapi "github.com/arduino/arduino-cloud-cli/internal/ota-api"
10+
"gopkg.in/yaml.v3"
911
)
1012

1113
func formatOutput(wideOutput bool, status any) {
@@ -15,7 +17,7 @@ func formatOutput(wideOutput bool, status any) {
1517
st.ErrorReason = ""
1618
st.Sha256 = ""
1719
}
18-
printJson(st)
20+
printOut(st, wideOutput)
1921
}
2022
}
2123

@@ -30,37 +32,78 @@ func formatOutputSlice(wideOutput bool, st []otaapi.Ota) {
3032
st[i].Sha256 = ""
3133
}
3234
}
33-
printJson(st)
35+
printOut(st, wideOutput)
3436
}
3537

36-
func printJson(res any) {
37-
jsonRes, _ := json.MarshalIndent(res, "", " ")
38-
if jsonRes != nil {
39-
fmt.Println(string(jsonRes))
38+
func printOut(res any, wideOutput bool) {
39+
outputformat := feedback.GetFormat()
40+
if outputformat == feedback.JSON || outputformat == feedback.JSONMini {
41+
jsonRes, _ := json.MarshalIndent(res, "", " ")
42+
if jsonRes != nil {
43+
fmt.Println(string(jsonRes))
44+
}
45+
}
46+
if outputformat == feedback.YAML {
47+
yamlRes, _ := yaml.Marshal(res)
48+
if yamlRes != nil {
49+
fmt.Println(string(yamlRes))
50+
}
51+
}
52+
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())
60+
}
61+
}
62+
if st, ok := res.([]otaapi.Ota); ok {
63+
64+
for i := range st {
65+
if i == 0 {
66+
if wideOutput {
67+
fmt.Println(st[i].GetWideCSVHeader())
68+
} else {
69+
fmt.Println(st[i].GetCSVHeader())
70+
}
71+
}
72+
if wideOutput {
73+
fmt.Println(st[i].ToWideCSV())
74+
} else {
75+
fmt.Println(st[i].ToCSV())
76+
}
77+
}
78+
}
4079
}
4180
}
4281

4382
func PrintOtaStatus(otaid, device, otaidpath string, wideOutput bool, cred *config.Credentials) error {
83+
84+
if feedback.GetFormat() == feedback.JSONMini {
85+
return fmt.Errorf("jsonmini format is not supported for this command")
86+
}
87+
4488
otapi := otaapi.NewClient(cred)
4589

46-
var err error
4790
if otaidpath != "" {
48-
91+
// TODO: implement
4992
} else if otaid != "" {
5093
res, err := otapi.GetOtaStatusByOtaID(otaid)
5194
if err == nil && res != nil {
5295
formatOutput(wideOutput, res.Ota)
96+
} else if err != nil {
97+
return err
5398
}
5499
} else if device != "" {
55100
res, err := otapi.GetOtaStatusByDeviceID(device)
56101
if err == nil && res != nil {
57102
formatOutputSlice(wideOutput, res.Ota)
103+
} else if err != nil {
104+
return err
58105
}
59106
}
60107

61-
if err != nil {
62-
return err
63-
}
64-
65108
return nil
66109
}

internal/ota-api/dto.go

+25-7
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
package otaapi
1919

20+
import "fmt"
21+
2022
type (
2123
OtaStatusResponse struct {
2224
Ota Ota `json:"ota"`
@@ -28,13 +30,13 @@ type (
2830
}
2931

3032
Ota struct {
31-
ID string `json:"id,omitempty"`
32-
DeviceID string `json:"device_id,omitempty"`
33-
Status string `json:"status"`
34-
StartedAt string `json:"started_at"`
35-
EndedAt string `json:"ended_at,omitempty"`
36-
ErrorReason string `json:"error_reason,omitempty"`
37-
Sha256 string `json:"sha256,omitempty"`
33+
ID string `json:"id,omitempty" yaml:"id,omitempty"`
34+
DeviceID string `json:"device_id,omitempty" yaml:"device_id,omitempty"`
35+
Status string `json:"status" yaml:"status"`
36+
StartedAt string `json:"started_at" yaml:"started_at"`
37+
EndedAt string `json:"ended_at,omitempty" yaml:"ended_at,omitempty"`
38+
ErrorReason string `json:"error_reason,omitempty" yaml:"error_reason,omitempty"`
39+
Sha256 string `json:"sha256,omitempty" yaml:"sha256,omitempty"`
3840
}
3941

4042
State struct {
@@ -44,3 +46,19 @@ type (
4446
Timestamp string `json:"timestamp,omitempty"`
4547
}
4648
)
49+
50+
func (o Ota) GetWideCSVHeader() string {
51+
return "id,device_id,status,started_at,ended_at,error_reason"
52+
}
53+
54+
func (o Ota) ToWideCSV() string {
55+
return fmt.Sprintf("%s,%s,%s,%s,%s,%s", o.ID, o.DeviceID, o.Status, o.StartedAt, o.EndedAt, o.ErrorReason)
56+
}
57+
58+
func (o Ota) GetCSVHeader() string {
59+
return "id,status,started_at,ended_at"
60+
}
61+
62+
func (o Ota) ToCSV() string {
63+
return fmt.Sprintf("%s,%s,%s,%s", o.ID, o.Status, o.StartedAt, o.EndedAt)
64+
}

0 commit comments

Comments
 (0)