11package main
22
3- import (
4- "errors"
5- "fmt"
6- "io"
7- "os"
8- "path/filepath"
9-
10- "github.com/gitql/gitql"
11- gitqlgit "github.com/gitql/gitql/git"
12- "github.com/gitql/gitql/internal/format"
13- "github.com/gitql/gitql/sql"
14-
15- "gopkg.in/src-d/go-git.v4"
16- )
17-
183type CmdQuery struct {
19- cmd
4+ cmdQueryBase
205
21- Path string `short:"p" long:"path" description:"Path where the git repository is located"`
226 Format string `short:"f" long:"format" default:"pretty" description:"Ouptut format. Formats supported: pretty, csv, json."`
237 Args struct {
248 SQL string `positional-arg-name:"sql" required:"true" description:"SQL query to execute"`
259 } `positional-args:"yes"`
26-
27- r * git.Repository
28- db sql.Database
2910}
3011
3112func (c * CmdQuery ) Execute (args []string ) error {
@@ -37,119 +18,10 @@ func (c *CmdQuery) Execute(args []string) error {
3718 return err
3819 }
3920
40- return c .executeQuery ()
41- }
42-
43- func (c * CmdQuery ) validate () error {
44- var err error
45- c .Path , err = findDotGitFolder (c .Path )
46- return err
47- }
48-
49- func (c * CmdQuery ) buildDatabase () error {
50- c .print ("opening %q repository...\n " , c .Path )
51-
52- var err error
53- c .r , err = git .NewFilesystemRepository (c .Path )
54- if err != nil {
55- return err
56- }
57-
58- empty , err := c .r .IsEmpty ()
59- if err != nil {
60- return err
61- }
62-
63- if empty {
64- return errors .New ("error: the repository is empty" )
65- }
66-
67- head , err := c .r .Head ()
68- if err != nil {
69- return err
70- }
71-
72- c .print ("current HEAD %q\n " , head .Hash ())
73-
74- name := filepath .Base (filepath .Join (c .Path , ".." ))
75- c .db = gitqlgit .NewDatabase (name , c .r )
76- return nil
77- }
78-
79- func (c * CmdQuery ) executeQuery () error {
80- c .print ("executing %q at %q\n " , c .Args .SQL , c .db .Name ())
81-
82- fmt .Println (c .Args .SQL )
83- e := gitql .New ()
84- e .AddDatabase (c .db )
85- schema , iter , err := e .Query (c .Args .SQL )
86- if err != nil {
87- return err
88- }
89-
90- return c .printQuery (schema , iter )
91- }
92-
93- func (c * CmdQuery ) printQuery (schema sql.Schema , iter sql.RowIter ) error {
94- f , err := format .NewFormat (c .Format , os .Stdout )
21+ schema , rowIter , err := c .executeQuery (c .Args .SQL )
9522 if err != nil {
9623 return err
9724 }
9825
99- headers := []string {}
100- for _ , f := range schema {
101- headers = append (headers , f .Name )
102- }
103-
104- if err := f .WriteHeader (headers ); err != nil {
105- return err
106- }
107-
108- for {
109- row , err := iter .Next ()
110- if err == io .EOF {
111- break
112- }
113- if err != nil {
114- return err
115- }
116-
117- record := make ([]interface {}, len (row ))
118- for i := 0 ; i < len (row ); i ++ {
119- record [i ] = row [i ]
120- }
121-
122- if err := f .Write (record ); err != nil {
123- return err
124- }
125- }
126-
127- return f .Close ()
128- }
129-
130- func findDotGitFolder (path string ) (string , error ) {
131- if path == "" {
132- var err error
133- path , err = os .Getwd ()
134- if err != nil {
135- return "" , err
136- }
137- }
138-
139- git := filepath .Join (path , ".git" )
140- _ , err := os .Stat (git )
141- if err == nil {
142- return git , nil
143- }
144-
145- if ! os .IsNotExist (err ) {
146- return "" , err
147- }
148-
149- next := filepath .Join (path , ".." )
150- if next == path {
151- return "" , errors .New ("unable to find a git repository" )
152- }
153-
154- return findDotGitFolder (next )
26+ return c .printQuery (schema , rowIter , c .Format )
15527}
0 commit comments