forked from src-d/gitbase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery.go
144 lines (117 loc) · 2.55 KB
/
query.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"errors"
"fmt"
"io"
"os"
"path/filepath"
"github.com/gitql/gitql"
gitqlgit "github.com/gitql/gitql/git"
"github.com/gitql/gitql/sql"
"github.com/olekukonko/tablewriter"
"gopkg.in/src-d/go-git.v4"
)
type CmdQuery struct {
cmd
Path string `short:"p" long:"path" description:"Path where the git repository is located"`
Args struct {
SQL string `positional-arg-name:"sql" required:"true" description:"SQL query to execute"`
} `positional-args:"yes"`
r *git.Repository
db sql.Database
}
func (c *CmdQuery) Execute(args []string) error {
if err := c.validate(); err != nil {
return err
}
if err := c.buildDatabase(); err != nil {
return err
}
return c.executeQuery()
}
func (c *CmdQuery) validate() error {
var err error
c.Path, err = findDotGitFolder(c.Path)
return err
}
func (c *CmdQuery) buildDatabase() error {
c.print("opening %q repository...\n", c.Path)
var err error
c.r, err = git.NewFilesystemRepository(c.Path)
if err != nil {
return err
}
empty, err := c.r.IsEmpty()
if err != nil {
return err
}
if empty {
return errors.New("error: the repository is empty")
}
head, err := c.r.Head()
if err != nil {
return err
}
c.print("current HEAD %q\n", head.Hash())
name := filepath.Base(filepath.Join(c.Path, ".."))
c.db = gitqlgit.NewDatabase(name, c.r)
return nil
}
func (c *CmdQuery) executeQuery() error {
c.print("executing %q at %q\n", c.Args.SQL, c.db.Name())
fmt.Println(c.Args.SQL)
e := gitql.New()
e.AddDatabase(c.db)
schema, iter, err := e.Query(c.Args.SQL)
if err != nil {
return err
}
c.printQuery(schema, iter)
return nil
}
func (c *CmdQuery) printQuery(schema sql.Schema, iter sql.RowIter) {
w := tablewriter.NewWriter(os.Stdout)
headers := []string{}
for _, f := range schema {
headers = append(headers, f.Name)
}
w.SetHeader(headers)
for {
row, err := iter.Next()
if err == io.EOF {
break
}
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
rowStrings := []string{}
for _, v := range row.Fields() {
rowStrings = append(rowStrings, fmt.Sprintf("%v", v))
}
w.Append(rowStrings)
}
w.Render()
}
func findDotGitFolder(path string) (string, error) {
if path == "" {
var err error
path, err = os.Getwd()
if err != nil {
return "", err
}
}
git := filepath.Join(path, ".git")
_, err := os.Stat(git)
if err == nil {
return git, nil
}
if !os.IsNotExist(err) {
return "", err
}
next := filepath.Join(path, "..")
if next == path {
return "", errors.New("unable to find a git repository")
}
return findDotGitFolder(next)
}