Skip to content

Commit 2a519f0

Browse files
mcuadrosajnavarro
authored andcommitted
CLI (#28)
* git: change NewDatabase, now accepts a git.Repository, tests now using a fixture * cli: basic cli implementation, fake SQL execution
1 parent d1560a8 commit 2a519f0

File tree

5 files changed

+172
-7
lines changed

5 files changed

+172
-7
lines changed

cmd/gitql/main.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/jessevdk/go-flags"
8+
)
9+
10+
func main() {
11+
parser := flags.NewNamedParser("gitql", flags.Default)
12+
parser.AddCommand("query", "Execute a SQL query a repository.", "", &CmdQuery{})
13+
parser.AddCommand("version", "Show the version information.", "", &CmdVersion{})
14+
15+
_, err := parser.Parse()
16+
if err != nil {
17+
if e, ok := err.(*flags.Error); ok && e.Type == flags.ErrCommandRequired {
18+
parser.WriteHelp(os.Stdout)
19+
}
20+
21+
os.Exit(1)
22+
}
23+
}
24+
25+
type cmd struct {
26+
Verbose bool `short:"v" description:"Activates the verbose mode"`
27+
}
28+
29+
func (c *cmd) print(format string, a ...interface{}) {
30+
if !c.Verbose {
31+
return
32+
}
33+
34+
fmt.Printf(format, a...)
35+
}

cmd/gitql/query.go

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package main
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"os"
7+
"path/filepath"
8+
9+
gitql "github.com/mvader/gitql/git"
10+
"github.com/mvader/gitql/sql"
11+
12+
"gopkg.in/src-d/go-git.v4"
13+
)
14+
15+
type CmdQuery struct {
16+
cmd
17+
18+
Path string `short:"p" long:"path" description:"Path where the git repository is located"`
19+
Args struct {
20+
SQL string `positional-arg-name:"sql" required:"true" description:"SQL query to execute"`
21+
} `positional-args:"yes"`
22+
23+
r *git.Repository
24+
db sql.Database
25+
}
26+
27+
func (c *CmdQuery) Execute(args []string) error {
28+
if err := c.validate(); err != nil {
29+
return err
30+
}
31+
32+
if err := c.buildDatabase(); err != nil {
33+
return err
34+
}
35+
36+
if err := c.executeQuery(); err != nil {
37+
return err
38+
}
39+
40+
return nil
41+
}
42+
43+
func (c *CmdQuery) validate() error {
44+
var err error
45+
c.Path, err = findDotGitFolder(c.Path)
46+
if err != nil {
47+
return err
48+
}
49+
50+
return nil
51+
}
52+
func (c *CmdQuery) buildDatabase() error {
53+
c.print("opening %q repository...\n", c.Path)
54+
55+
var err error
56+
c.r, err = git.NewFilesystemRepository(c.Path)
57+
if err != nil {
58+
return err
59+
}
60+
61+
empty, err := c.r.IsEmpty()
62+
if err != nil {
63+
return err
64+
}
65+
66+
if empty {
67+
return errors.New("error: the repository is empty")
68+
}
69+
70+
head, err := c.r.Head()
71+
if err != nil {
72+
return err
73+
}
74+
75+
c.print("current HEAD %q\n", head.Hash())
76+
77+
name := filepath.Base(filepath.Join(c.Path, ".."))
78+
c.db = gitql.NewDatabase(name, c.r)
79+
return nil
80+
}
81+
82+
func (c *CmdQuery) executeQuery() error {
83+
c.print("executing %q at %q\n", c.Args.SQL, c.db.Name())
84+
85+
fmt.Println(c.Args.SQL)
86+
return nil
87+
}
88+
89+
func findDotGitFolder(path string) (string, error) {
90+
if path == "" {
91+
var err error
92+
path, err = os.Getwd()
93+
if err != nil {
94+
return "", err
95+
}
96+
}
97+
98+
git := filepath.Join(path, ".git")
99+
_, err := os.Stat(git)
100+
if err == nil {
101+
return git, nil
102+
}
103+
104+
if !os.IsNotExist(err) {
105+
return "", err
106+
}
107+
108+
next := filepath.Join(path, "..")
109+
if next == path {
110+
return "", errors.New("unable to find a git repository")
111+
}
112+
113+
return findDotGitFolder(next)
114+
}

cmd/gitql/version.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
var version string
8+
var build string
9+
10+
type CmdVersion struct{}
11+
12+
func (c *CmdVersion) Execute(args []string) error {
13+
fmt.Printf("gitql (%s) - build %s\n", version, build)
14+
15+
return nil
16+
}

git/database.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ func NewDatabase(name string, r *git.Repository) sql.Database {
2222
}
2323
}
2424

25-
func (d Database) Name() string {
25+
func (d *Database) Name() string {
2626
return d.name
2727
}
2828

29-
func (d Database) Relations() map[string]sql.PhysicalRelation {
29+
func (d *Database) Relations() map[string]sql.PhysicalRelation {
3030
return map[string]sql.PhysicalRelation{
3131
commitsRelationName: d.cr,
3232
}

mem/database.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,25 @@ package mem
33
import "github.com/mvader/gitql/sql"
44

55
type Database struct {
6-
name string
6+
name string
77
tables map[string]sql.PhysicalRelation
88
}
99

1010
func NewDatabase(name string) Database {
1111
return Database{
12-
name: name,
12+
name: name,
1313
tables: map[string]sql.PhysicalRelation{},
1414
}
1515
}
1616

17-
func (d Database) Name() string {
17+
func (d *Database) Name() string {
1818
return d.name
1919
}
2020

21-
func (d Database) Relations() map[string]sql.PhysicalRelation {
21+
func (d *Database) Relations() map[string]sql.PhysicalRelation {
2222
return d.tables
2323
}
2424

25-
func (d Database) AddTable(name string, t *Table) {
25+
func (d *Database) AddTable(name string, t *Table) {
2626
d.tables[name] = t
2727
}

0 commit comments

Comments
 (0)