@@ -9,17 +9,18 @@ import (
99
1010 "github.com/gitql/gitql"
1111 gitqlgit "github.com/gitql/gitql/git"
12+ "github.com/gitql/gitql/internal/format"
1213 "github.com/gitql/gitql/sql"
1314
14- "github.com/olekukonko/tablewriter"
1515 "gopkg.in/src-d/go-git.v4"
1616)
1717
1818type CmdQuery struct {
1919 cmd
2020
21- Path string `short:"p" long:"path" description:"Path where the git repository is located"`
22- Args struct {
21+ Path string `short:"p" long:"path" description:"Path where the git repository is located"`
22+ Format string `short:"f" long:"format" default:"pretty" description:"Ouptut format. Formats supported: pretty, csv, json."`
23+ Args struct {
2324 SQL string `positional-arg-name:"sql" required:"true" description:"SQL query to execute"`
2425 } `positional-args:"yes"`
2526
@@ -86,34 +87,39 @@ func (c *CmdQuery) executeQuery() error {
8687 return err
8788 }
8889
89- c .printQuery (schema , iter )
90-
91- return nil
90+ return c .printQuery (schema , iter )
9291}
9392
94- func (c * CmdQuery ) printQuery (schema sql.Schema , iter sql.RowIter ) {
95- w := tablewriter .NewWriter (os .Stdout )
93+ func (c * CmdQuery ) printQuery (schema sql.Schema , iter sql.RowIter ) error {
94+ f , err := format .NewFormat (c .Format , os .Stdout )
95+ if err != nil {
96+ return err
97+ }
98+
9699 headers := []string {}
97100 for _ , f := range schema {
98101 headers = append (headers , f .Name )
99102 }
100- w .SetHeader (headers )
103+
104+ if err := f .WriteHeader (headers ); err != nil {
105+ return err
106+ }
107+
101108 for {
102109 row , err := iter .Next ()
103110 if err == io .EOF {
104111 break
105112 }
106113 if err != nil {
107- fmt .Printf ("Error: %v\n " , err )
108- return
114+ return err
109115 }
110- rowStrings := [] string {}
111- for _ , v := range row .Fields () {
112- rowStrings = append ( rowStrings , fmt . Sprintf ( "%v" , v ))
116+
117+ if err := f . Write ( row .Fields ()); err != nil {
118+ return err
113119 }
114- w .Append (rowStrings )
115120 }
116- w .Render ()
121+
122+ return f .Close ()
117123}
118124
119125func findDotGitFolder (path string ) (string , error ) {
0 commit comments