-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdisplay.go
More file actions
215 lines (189 loc) · 5.67 KB
/
display.go
File metadata and controls
215 lines (189 loc) · 5.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package main
import (
"encoding/json"
"fmt"
"os"
encodingcsv "encoding/csv"
"github.com/golang/protobuf/ptypes"
"github.com/spf13/viper"
"github.com/turbot/steampipe-plugin-sdk/v5/grpc/proto"
"google.golang.org/protobuf/reflect/protoreflect"
)
type displayRowFunc func(row *proto.ExecuteResponse, columns []string) error
// Global variables to manage the state of JSON output
var isFirstJSONRow = true
var isJSONStarted = false
var rowCount = 0
// displayCSVRow formats and outputs the row data in CSV format, managing headers and selected columns.
func displayCSVRow(displayRow *proto.ExecuteResponse, columns []string) error {
row := displayRow.Row
selectColumns := viper.GetStringSlice("select")
// Process each column and store values in a map
res := make(map[string]string, len(row.Columns))
for columnName, column := range row.Columns {
var val interface{}
if bytes := column.GetJsonValue(); bytes != nil {
val = string(bytes)
} else if timestamp := column.GetTimestampValue(); timestamp != nil {
val = ptypes.TimestampString(timestamp)
} else {
column.ProtoReflect().Range(func(descriptor protoreflect.FieldDescriptor, v protoreflect.Value) bool {
if descriptor.JSONName() == "nullValue" {
val = nil
} else {
val = v.Interface()
}
return false
})
}
res[columnName] = fmt.Sprintf("%v", val)
}
// Prepare CSV writer
writer := encodingcsv.NewWriter(os.Stdout)
defer writer.Flush()
// Write headers
if rowCount == 0 {
if len(selectColumns) > 0 {
// Write headers based on selectColumns
if err := writer.Write(selectColumns); err != nil {
return fmt.Errorf("error writing headers: %v", err)
}
} else {
// Write all headers
if err := writer.Write(columns); err != nil {
return fmt.Errorf("error writing headers: %v", err)
}
}
writer.Flush()
if err := writer.Error(); err != nil {
return fmt.Errorf("error flushing headers: %v", err)
}
}
rowCount++
// Generate row data
var colVals []string
if len(selectColumns) > 0 {
colVals = make([]string, len(selectColumns))
for i, columnName := range selectColumns {
colVals[i], _ = res[columnName] // Using _ to ignore whether columnName is present in res
}
} else {
colVals = make([]string, len(columns))
for i, columnName := range columns {
colVals[i], _ = res[columnName]
}
}
// Write the row data
if err := writer.Write(colVals); err != nil {
return fmt.Errorf("error writing row data: %v", err)
}
writer.Flush()
// Handle potential errors from the writer
if err := writer.Error(); err != nil {
return fmt.Errorf("error flushing row data: %v", err)
}
return nil
}
// displayJSONRow formats and outputs the row data in JSON format, managing array formatting.
func displayJSONRow(displayRow *proto.ExecuteResponse, columns []string) error {
row := displayRow.Row
selectColumns := viper.GetStringSlice("select")
// Process each column and store values in a map
res := make(map[string]interface{}, len(row.Columns))
for columnName, column := range row.Columns {
var val interface{}
if bytes := column.GetJsonValue(); bytes != nil {
val = string(bytes)
} else if timestamp := column.GetTimestampValue(); timestamp != nil {
val = ptypes.TimestampString(timestamp)
} else {
column.ProtoReflect().Range(func(descriptor protoreflect.FieldDescriptor, v protoreflect.Value) bool {
if descriptor.JSONName() == "nullValue" {
val = nil
} else {
val = v.Interface()
}
return false
})
}
res[columnName] = val
}
// Create a map for the selected columns
selectedRes := make(map[string]interface{})
if len(selectColumns) > 0 {
for _, columnName := range selectColumns {
if val, ok := res[columnName]; ok {
selectedRes[columnName] = val
}
}
} else {
selectedRes = res
}
// Convert to JSON
jsonData, err := json.Marshal(selectedRes)
if err != nil {
return fmt.Errorf("error marshaling JSON: %v", err)
}
// Print the JSON
if isFirstJSONRow {
fmt.Print("[")
isFirstJSONRow = false
isJSONStarted = true
} else {
fmt.Print(",")
}
fmt.Print(string(jsonData))
return nil
}
// finishJSONOutput checks if JSON output has started and closes the JSON array if it has.
// It should be called if the output format is JSON and all rows have been processed.
func finishJSONOutput() error {
if isJSONStarted {
fmt.Println("]")
}
return nil
}
// displayJSONLRow formats and outputs the row data in JSON Lines (JSONL) format for selected columns.
func displayJSONLRow(displayRow *proto.ExecuteResponse, columns []string) error {
row := displayRow.Row
selectColumns := viper.GetStringSlice("select")
// Process each column and store values in a map
res := make(map[string]interface{}, len(row.Columns))
for columnName, column := range row.Columns {
var val interface{}
if bytes := column.GetJsonValue(); bytes != nil {
val = string(bytes)
} else if timestamp := column.GetTimestampValue(); timestamp != nil {
val = ptypes.TimestampString(timestamp)
} else {
column.ProtoReflect().Range(func(descriptor protoreflect.FieldDescriptor, v protoreflect.Value) bool {
if descriptor.JSONName() == "nullValue" {
val = nil
} else {
val = v.Interface()
}
return false
})
}
res[columnName] = val
}
// Create a map for the selected columns
selectedRes := make(map[string]interface{})
if len(selectColumns) > 0 {
for _, columnName := range selectColumns {
if val, ok := res[columnName]; ok {
selectedRes[columnName] = val
}
}
} else {
selectedRes = res
}
// Convert to JSON
jsonData, err := json.Marshal(selectedRes)
if err != nil {
return fmt.Errorf("error marshaling JSON: %v", err)
}
// Print the JSON line
fmt.Println(string(jsonData))
return nil
}