|
8 | 8 | package main |
9 | 9 |
|
10 | 10 | import ( |
| 11 | + "encoding/csv" |
11 | 12 | "encoding/json" |
12 | 13 | "flag" |
13 | 14 | "fmt" |
14 | 15 | "io/ioutil" |
15 | 16 | "os" |
16 | 17 | "path" |
17 | | - "strings" |
18 | 18 |
|
19 | 19 | // Caltech Library Packages |
20 | 20 | "github.com/caltechlibrary/cli" |
@@ -89,7 +89,6 @@ Would yield |
89 | 89 | delimiter = "," |
90 | 90 | expressions []string |
91 | 91 | permissive bool |
92 | | - quote bool |
93 | 92 | ) |
94 | 93 |
|
95 | 94 | func handleError(err error, exitCode int) { |
@@ -119,7 +118,6 @@ func init() { |
119 | 118 | flag.BoolVar(&runInteractive, "r", false, "run interactively") |
120 | 119 | flag.BoolVar(&runInteractive, "repl", false, "run interactively") |
121 | 120 | flag.StringVar(&delimiter, "d", delimiter, "set the delimiter for multi-field output") |
122 | | - flag.BoolVar("e, "quote", false, "if dilimiter is found in column value add quotes") |
123 | 121 | flag.BoolVar(&permissive, "permissive", false, "suppress error messages") |
124 | 122 | flag.BoolVar(&permissive, "quiet", false, "suppress error messages") |
125 | 123 | } |
@@ -189,38 +187,37 @@ func main() { |
189 | 187 | } |
190 | 188 |
|
191 | 189 | // For each dotpath expression return a result |
192 | | - for i, qry := range expressions { |
193 | | - if i > 0 { |
194 | | - fmt.Fprintf(out, "%s", delimiter) |
195 | | - } |
196 | | - if qry == "." { |
197 | | - fmt.Fprintf(out, "%s", buf) |
198 | | - } else { |
199 | | - result, err := dotpath.Eval(qry, data) |
200 | | - if err == nil { |
201 | | - switch result.(type) { |
202 | | - case string: |
203 | | - if quote == true && strings.Contains(result.(string), delimiter) == true { |
204 | | - fmt.Fprintf(out, "%q", result) |
205 | | - } else { |
206 | | - fmt.Fprintf(out, "%s", result) |
207 | | - } |
208 | | - case json.Number: |
209 | | - fmt.Fprintf(out, "%s", result.(json.Number).String()) |
210 | | - default: |
211 | | - src, err := json.Marshal(result) |
212 | | - if err != nil { |
213 | | - handleError(err, 1) |
214 | | - } |
215 | | - if quote == true { |
216 | | - fmt.Fprintf(out, "%q", src) |
217 | | - } else { |
218 | | - fmt.Fprintf(out, "%s", src) |
219 | | - } |
| 190 | + row := []string{} |
| 191 | + for _, qry := range expressions { |
| 192 | + result, err := dotpath.Eval(qry, data) |
| 193 | + if err == nil { |
| 194 | + switch result.(type) { |
| 195 | + case string: |
| 196 | + row = append(row, result.(string)) |
| 197 | + case json.Number: |
| 198 | + row = append(row, result.(json.Number).String()) |
| 199 | + default: |
| 200 | + src, err := json.Marshal(result) |
| 201 | + if err != nil { |
| 202 | + handleError(err, 1) |
220 | 203 | } |
221 | | - } else { |
222 | | - handleError(err, 1) |
| 204 | + row = append(row, fmt.Sprintf("%s", src)) |
223 | 205 | } |
| 206 | + } else { |
| 207 | + handleError(err, 1) |
224 | 208 | } |
225 | 209 | } |
| 210 | + |
| 211 | + // Setup the CSV output |
| 212 | + w := csv.NewWriter(out) |
| 213 | + if delimiter != "" { |
| 214 | + w.Comma = datatools.NormalizeDelimiterRune(delimiter) |
| 215 | + } |
| 216 | + if err := w.Write(row); err != nil { |
| 217 | + handleError(err, 1) |
| 218 | + } |
| 219 | + w.Flush() |
| 220 | + if err := w.Error(); err != nil { |
| 221 | + handleError(err, 1) |
| 222 | + } |
226 | 223 | } |
0 commit comments