Skip to content

Commit 6ccbe1a

Browse files
authored
Avoids adding newline to list/map output data for output format other than 'text' (#87)
1 parent 6a21591 commit 6ccbe1a

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

cmd/output.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,18 @@ import (
3030
"github.com/olekukonko/tablewriter"
3131
)
3232

33-
func jsonify(value interface{}) string {
33+
func jsonify(value interface{}, format string) string {
3434
if value == nil {
3535
return ""
3636
}
3737
if reflect.TypeOf(value).Kind() == reflect.Map || reflect.TypeOf(value).Kind() == reflect.Slice {
38-
jsonStr, err := json.MarshalIndent(value, "", "")
38+
var jsonStr []byte
39+
var err error
40+
if (format == "text") {
41+
jsonStr, err = json.MarshalIndent(value, "", "")
42+
} else {
43+
jsonStr, err = json.Marshal(value)
44+
}
3945
if err == nil {
4046
value = string(jsonStr)
4147
}
@@ -51,6 +57,7 @@ func printJSON(response map[string]interface{}) {
5157
}
5258

5359
func printText(response map[string]interface{}) {
60+
format := "text"
5461
for k, v := range response {
5562
valueType := reflect.TypeOf(v)
5663
if valueType.Kind() == reflect.Slice {
@@ -62,19 +69,20 @@ func printText(response map[string]interface{}) {
6269
row, isMap := item.(map[string]interface{})
6370
if isMap {
6471
for field, value := range row {
65-
fmt.Printf("%s = %v\n", field, jsonify(value))
72+
fmt.Printf("%s = %v\n", field, jsonify(value, format))
6673
}
6774
} else {
6875
fmt.Printf("%v\n", item)
6976
}
7077
}
7178
} else {
72-
fmt.Printf("%v = %v\n", k, jsonify(v))
79+
fmt.Printf("%v = %v\n", k, jsonify(v, format))
7380
}
7481
}
7582
}
7683

7784
func printTable(response map[string]interface{}, filter []string) {
85+
format := "table"
7886
table := tablewriter.NewWriter(os.Stdout)
7987
for k, v := range response {
8088
valueType := reflect.TypeOf(v)
@@ -103,7 +111,7 @@ func printTable(response map[string]interface{}, filter []string) {
103111
}
104112
var rowArray []string
105113
for _, field := range header {
106-
rowArray = append(rowArray, jsonify(row[field]))
114+
rowArray = append(rowArray, jsonify(row[field], format))
107115
}
108116
table.Append(rowArray)
109117
}
@@ -115,6 +123,7 @@ func printTable(response map[string]interface{}, filter []string) {
115123
}
116124

117125
func printColumn(response map[string]interface{}, filter []string) {
126+
format := "column"
118127
w := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', tabwriter.DiscardEmptyColumns)
119128
for _, v := range response {
120129
valueType := reflect.TypeOf(v)
@@ -143,7 +152,7 @@ func printColumn(response map[string]interface{}, filter []string) {
143152
}
144153
var values []string
145154
for _, key := range header {
146-
values = append(values, jsonify(row[strings.ToLower(key)]))
155+
values = append(values, jsonify(row[strings.ToLower(key)], format))
147156
}
148157
fmt.Fprintln(w, strings.Join(values, "\t"))
149158
}
@@ -153,6 +162,7 @@ func printColumn(response map[string]interface{}, filter []string) {
153162
}
154163

155164
func printCsv(response map[string]interface{}, filter []string) {
165+
format := "csv"
156166
for _, v := range response {
157167
valueType := reflect.TypeOf(v)
158168
if valueType.Kind() == reflect.Slice || valueType.Kind() == reflect.Map {
@@ -180,7 +190,7 @@ func printCsv(response map[string]interface{}, filter []string) {
180190
}
181191
var values []string
182192
for _, key := range header {
183-
values = append(values, jsonify(row[key]))
193+
values = append(values, jsonify(row[key], format))
184194
}
185195
fmt.Println(strings.Join(values, ","))
186196
}

0 commit comments

Comments
 (0)