@@ -15,6 +15,7 @@ import (
1515 "io/ioutil"
1616 "os"
1717 "path"
18+ "strings"
1819
1920 // Caltech Library Packages
2021 "github.com/caltechlibrary/cli"
@@ -86,9 +87,11 @@ Would yield
8687 // Application Specific Options
8788 monochrome bool
8889 runInteractive bool
90+ csvOutput bool
8991 delimiter = ","
9092 expressions []string
9193 permissive bool
94+ quote bool
9295)
9396
9497func handleError (err error , exitCode int ) {
@@ -117,7 +120,10 @@ func init() {
117120 flag .BoolVar (& monochrome , "m" , false , "display output in monochrome" )
118121 flag .BoolVar (& runInteractive , "r" , false , "run interactively" )
119122 flag .BoolVar (& runInteractive , "repl" , false , "run interactively" )
120- flag .StringVar (& delimiter , "d" , delimiter , "set the delimiter for multi-field output" )
123+ flag .BoolVar (& csvOutput , "csv" , false , "output as CSV or other flat delimiter row" )
124+ flag .StringVar (& delimiter , "d" , delimiter , "set the delimiter for multi-field csv output" )
125+ flag .StringVar (& delimiter , "dimiter" , delimiter , "set the delimiter for multi-field csv output" )
126+ flag .BoolVar (& quote , "quote" , false , "if dilimiter is found in column value add quotes for non-CSV output" )
121127 flag .BoolVar (& permissive , "permissive" , false , "suppress error messages" )
122128 flag .BoolVar (& permissive , "quiet" , false , "suppress error messages" )
123129}
@@ -186,38 +192,78 @@ func main() {
186192 handleError (err , 1 )
187193 }
188194
189- // For each dotpath expression return a result
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 )
195+ if csvOutput == true {
196+ // For each dotpath expression return a result
197+ row := []string {}
198+ for _ , qry := range expressions {
199+ result , err := dotpath .Eval (qry , data )
200+ if err == nil {
201+ switch result .(type ) {
202+ case string :
203+ row = append (row , result .(string ))
204+ case json.Number :
205+ row = append (row , result .(json.Number ).String ())
206+ default :
207+ src , err := json .Marshal (result )
208+ if err != nil {
209+ handleError (err , 1 )
210+ }
211+ row = append (row , fmt .Sprintf ("%s" , src ))
203212 }
204- row = append (row , fmt .Sprintf ("%s" , src ))
213+ } else {
214+ handleError (err , 1 )
205215 }
206- } else {
216+ }
217+
218+ // Setup the CSV output
219+ w := csv .NewWriter (out )
220+ if delimiter != "" {
221+ w .Comma = datatools .NormalizeDelimiterRune (delimiter )
222+ }
223+ if err := w .Write (row ); err != nil {
207224 handleError (err , 1 )
208225 }
226+ w .Flush ()
227+ if err := w .Error (); err != nil {
228+ handleError (err , 1 )
229+ }
230+ os .Exit (0 )
209231 }
210232
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 )
233+ // Output JSON format (default)
234+ // For each dotpath expression return a result
235+ for i , qry := range expressions {
236+ if i > 0 {
237+ fmt .Fprintf (out , "%s" , delimiter )
238+ }
239+ if qry == "." {
240+ fmt .Fprintf (out , "%s" , buf )
241+ } else {
242+ result , err := dotpath .Eval (qry , data )
243+ if err == nil {
244+ switch result .(type ) {
245+ case string :
246+ if quote == true && strings .Contains (result .(string ), delimiter ) == true {
247+ fmt .Fprintf (out , "%q" , result )
248+ } else {
249+ fmt .Fprintf (out , "%s" , result )
250+ }
251+ case json.Number :
252+ fmt .Fprintf (out , "%s" , result .(json.Number ).String ())
253+ default :
254+ src , err := json .Marshal (result )
255+ if err != nil {
256+ handleError (err , 1 )
257+ }
258+ if quote == true {
259+ fmt .Fprintf (out , "%q" , src )
260+ } else {
261+ fmt .Fprintf (out , "%s" , src )
262+ }
263+ }
264+ } else {
265+ handleError (err , 1 )
266+ }
267+ }
222268 }
223269}
0 commit comments