Skip to content

Commit 8949f5e

Browse files
authored
Merge pull request #243 from mcuadros/cli-renamed
cli: small refactor to align the other products
2 parents b93182e + 2a13758 commit 8949f5e

File tree

5 files changed

+117
-87
lines changed

5 files changed

+117
-87
lines changed
Lines changed: 56 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,57 @@
1-
package main
1+
package cmd
22

33
import (
44
"net"
5-
"os"
65
"strconv"
76

8-
"github.com/sirupsen/logrus"
9-
107
"github.com/src-d/gitbase"
118
"github.com/src-d/gitbase/internal/function"
129
"github.com/src-d/gitbase/internal/rule"
1310

11+
"github.com/sirupsen/logrus"
1412
sqle "gopkg.in/src-d/go-mysql-server.v0"
1513
"gopkg.in/src-d/go-mysql-server.v0/server"
1614
"gopkg.in/src-d/go-vitess.v0/mysql"
1715
)
1816

19-
// Squashing tables and pushing down join conditions is still a work in
20-
// progress and unstable. To enable it, the UNSTABLE_SQUASH_ENABLE must
21-
// not be empty.
22-
var enableUnstableSquash = os.Getenv("UNSTABLE_SQUASH_ENABLE") != ""
23-
24-
// By default when gitbase encounters and error in a repository it stops
25-
// the query. With this parameter it won't complain and just skip those
26-
// rows or repositories.
27-
var skipGitErrors = os.Getenv("GITBASE_SKIP_GIT_ERRORS") != ""
28-
29-
type cmdServer struct {
30-
Verbose bool `short:"v" description:"Activates the verbose mode"`
17+
const (
18+
ServerDescription = "Starts a gitbase server instance"
19+
ServerHelp = ServerDescription + "\n\n" +
20+
"The squashing tables and pushing down join conditions is still a\n" +
21+
"work in progress and unstable,disable by default can be enabled\n" +
22+
"using a not empty value at UNSTABLE_SQUASH_ENABLE env variable.\n\n" +
23+
"By default when gitbase encounters and error in a repository it\n" +
24+
"stops the query. With GITBASE_SKIP_GIT_ERRORS variable it won't\n" +
25+
"complain and just skip those rows or repositories."
26+
)
3127

28+
// Server represents the `server` command of gitbase cli tool.
29+
type Server struct {
30+
Verbose bool `short:"v" description:"Activates the verbose mode"`
3231
Git []string `short:"g" long:"git" description:"Path where the git repositories are located, multiple directories can be defined"`
3332
Siva []string `long:"siva" description:"Path where the siva repositories are located, multiple directories can be defined"`
3433
Host string `short:"h" long:"host" default:"localhost" description:"Host where the server is going to listen"`
3534
Port int `short:"p" long:"port" default:"3306" description:"Port where the server is going to listen"`
3635
User string `short:"u" long:"user" default:"root" description:"User name used for connection"`
3736
Password string `short:"P" long:"password" default:"" description:"Password used for connection"`
3837

38+
// UnstableSquash quashing tables and pushing down join conditions is still
39+
// a work in progress and unstable. To enable it, the UNSTABLE_SQUASH_ENABLE
40+
// must not be empty.
41+
UnstableSquash bool
42+
// IgnoreGitErrors by default when gitbase encounters and error in a
43+
// repository it stops the query. With this parameter it won't complain and
44+
// just skip those rows or repositories.
45+
SkipGitErrors bool
46+
3947
engine *sqle.Engine
4048
pool *gitbase.RepositoryPool
4149
name string
4250
}
4351

44-
func (c *cmdServer) buildDatabase() error {
45-
if c.engine == nil {
46-
c.engine = sqle.New()
47-
}
48-
49-
c.pool = gitbase.NewRepositoryPool()
50-
51-
if err := c.addDirectories(); err != nil {
52-
return err
53-
}
54-
55-
c.engine.AddDatabase(gitbase.NewDatabase(c.name))
56-
logrus.WithField("db", c.name).Debug("registered database to catalog")
57-
58-
c.engine.Catalog.RegisterFunctions(function.Functions)
59-
logrus.Debug("registered all available functions in catalog")
60-
61-
if enableUnstableSquash {
62-
logrus.Warn("unstable squash tables rule is enabled")
63-
c.engine.Analyzer.AddRule(rule.SquashJoinsRule, rule.SquashJoins)
64-
}
65-
66-
return nil
67-
}
68-
69-
func (c *cmdServer) Execute(args []string) error {
52+
// Execute starts a new gitbase server based on provided configuration, it
53+
// honors the go-flags.Commander interface.
54+
func (c *Server) Execute(args []string) error {
7055
if c.Verbose {
7156
logrus.SetLevel(logrus.DebugLevel)
7257
}
@@ -90,7 +75,7 @@ func (c *cmdServer) Execute(args []string) error {
9075
},
9176
c.engine,
9277
gitbase.NewSessionBuilder(c.pool,
93-
gitbase.WithSkipGitErrors(skipGitErrors),
78+
gitbase.WithSkipGitErrors(c.SkipGitErrors),
9479
),
9580
)
9681
if err != nil {
@@ -101,7 +86,32 @@ func (c *cmdServer) Execute(args []string) error {
10186
return s.Start()
10287
}
10388

104-
func (c *cmdServer) addDirectories() error {
89+
func (c *Server) buildDatabase() error {
90+
if c.engine == nil {
91+
c.engine = sqle.New()
92+
}
93+
94+
c.pool = gitbase.NewRepositoryPool()
95+
96+
if err := c.addDirectories(); err != nil {
97+
return err
98+
}
99+
100+
c.engine.AddDatabase(gitbase.NewDatabase(c.name))
101+
logrus.WithField("db", c.name).Debug("registered database to catalog")
102+
103+
c.engine.Catalog.RegisterFunctions(function.Functions)
104+
logrus.Debug("registered all available functions in catalog")
105+
106+
if c.UnstableSquash {
107+
logrus.Warn("unstable squash tables rule is enabled")
108+
c.engine.Analyzer.AddRule(rule.SquashJoinsRule, rule.SquashJoins)
109+
}
110+
111+
return nil
112+
}
113+
114+
func (c *Server) addDirectories() error {
105115
if len(c.Git) == 0 && len(c.Siva) == 0 {
106116
logrus.Error("At least one git folder or siva folder should be provided.")
107117
}
@@ -121,12 +131,12 @@ func (c *cmdServer) addDirectories() error {
121131
return nil
122132
}
123133

124-
func (c *cmdServer) addGitDirectory(folder string) error {
134+
func (c *Server) addGitDirectory(folder string) error {
125135
logrus.WithField("dir", c.Git).Debug("git repositories directory added")
126136
return c.pool.AddDir(folder)
127137
}
128138

129-
func (c *cmdServer) addSivaDirectory(folder string) error {
139+
func (c *Server) addSivaDirectory(folder string) error {
130140
logrus.WithField("dir", c.Git).Debug("siva repositories directory added")
131141
return c.pool.AddSivaDir(folder)
132142
}

cli/gitbase/cmd/version.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package cmd
2+
3+
import "fmt"
4+
5+
const (
6+
VersionDescription = "Show the version information"
7+
VersionHelp = VersionDescription
8+
)
9+
10+
var (
11+
version = "undefined"
12+
build = "undefined"
13+
)
14+
15+
// Version represents the `version` command of gitbase cli tool.
16+
type Version struct {
17+
// Name of the cli binary
18+
Name string
19+
}
20+
21+
// Execute prints the build information provided by the compilation tools, it
22+
// honors the go-flags.Commander interface.
23+
func (c *Version) Execute(args []string) error {
24+
fmt.Printf("%s (%s) - build %s\n", c.Name, version, build)
25+
return nil
26+
}

cli/gitbase/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+
"os"
5+
6+
"github.com/src-d/gitbase/cli/gitbase/cmd"
7+
8+
"github.com/jessevdk/go-flags"
9+
)
10+
11+
const (
12+
name = "gitbase"
13+
)
14+
15+
func main() {
16+
parser := flags.NewNamedParser(name, flags.Default)
17+
18+
parser.AddCommand("server", cmd.ServerDescription, cmd.ServerHelp, &cmd.Server{
19+
UnstableSquash: os.Getenv("UNSTABLE_SQUASH_ENABLE") != "",
20+
SkipGitErrors: os.Getenv("GITBASE_SKIP_GIT_ERRORS") != "",
21+
})
22+
23+
parser.AddCommand("version", cmd.VersionDescription, cmd.VersionHelp, &cmd.Version{
24+
Name: name,
25+
})
26+
27+
_, err := parser.Parse()
28+
if err != nil {
29+
if e, ok := err.(*flags.Error); ok && e.Type == flags.ErrCommandRequired {
30+
parser.WriteHelp(os.Stdout)
31+
}
32+
33+
os.Exit(1)
34+
}
35+
}

cmd/gitbase/main.go

Lines changed: 0 additions & 26 deletions
This file was deleted.

cmd/gitbase/version.go

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)