@@ -9,17 +9,18 @@ import (
9
9
10
10
"github.com/gitql/gitql"
11
11
gitqlgit "github.com/gitql/gitql/git"
12
+ "github.com/gitql/gitql/internal/format"
12
13
"github.com/gitql/gitql/sql"
13
14
14
- "github.com/olekukonko/tablewriter"
15
15
"gopkg.in/src-d/go-git.v4"
16
16
)
17
17
18
18
type CmdQuery struct {
19
19
cmd
20
20
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 {
23
24
SQL string `positional-arg-name:"sql" required:"true" description:"SQL query to execute"`
24
25
} `positional-args:"yes"`
25
26
@@ -86,34 +87,39 @@ func (c *CmdQuery) executeQuery() error {
86
87
return err
87
88
}
88
89
89
- c .printQuery (schema , iter )
90
-
91
- return nil
90
+ return c .printQuery (schema , iter )
92
91
}
93
92
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
+
96
99
headers := []string {}
97
100
for _ , f := range schema {
98
101
headers = append (headers , f .Name )
99
102
}
100
- w .SetHeader (headers )
103
+
104
+ if err := f .WriteHeader (headers ); err != nil {
105
+ return err
106
+ }
107
+
101
108
for {
102
109
row , err := iter .Next ()
103
110
if err == io .EOF {
104
111
break
105
112
}
106
113
if err != nil {
107
- fmt .Printf ("Error: %v\n " , err )
108
- return
114
+ return err
109
115
}
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
113
119
}
114
- w .Append (rowStrings )
115
120
}
116
- w .Render ()
121
+
122
+ return f .Close ()
117
123
}
118
124
119
125
func findDotGitFolder (path string ) (string , error ) {
0 commit comments