1
- package main
1
+ package cmd
2
2
3
3
import (
4
4
"net"
5
- "os"
6
5
"strconv"
7
6
8
- "github.com/sirupsen/logrus"
9
-
10
7
"github.com/src-d/gitbase"
11
8
"github.com/src-d/gitbase/internal/function"
12
9
"github.com/src-d/gitbase/internal/rule"
13
10
11
+ "github.com/sirupsen/logrus"
14
12
sqle "gopkg.in/src-d/go-mysql-server.v0"
15
13
"gopkg.in/src-d/go-mysql-server.v0/server"
16
14
"gopkg.in/src-d/go-vitess.v0/mysql"
17
15
)
18
16
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
+ )
31
27
28
+ // Server represents the `server` command of gitbase cli tool.
29
+ type Server struct {
30
+ Verbose bool `short:"v" description:"Activates the verbose mode"`
32
31
Git []string `short:"g" long:"git" description:"Path where the git repositories are located, multiple directories can be defined"`
33
32
Siva []string `long:"siva" description:"Path where the siva repositories are located, multiple directories can be defined"`
34
33
Host string `short:"h" long:"host" default:"localhost" description:"Host where the server is going to listen"`
35
34
Port int `short:"p" long:"port" default:"3306" description:"Port where the server is going to listen"`
36
35
User string `short:"u" long:"user" default:"root" description:"User name used for connection"`
37
36
Password string `short:"P" long:"password" default:"" description:"Password used for connection"`
38
37
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
+
39
47
engine * sqle.Engine
40
48
pool * gitbase.RepositoryPool
41
49
name string
42
50
}
43
51
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 {
70
55
if c .Verbose {
71
56
logrus .SetLevel (logrus .DebugLevel )
72
57
}
@@ -90,7 +75,7 @@ func (c *cmdServer) Execute(args []string) error {
90
75
},
91
76
c .engine ,
92
77
gitbase .NewSessionBuilder (c .pool ,
93
- gitbase .WithSkipGitErrors (skipGitErrors ),
78
+ gitbase .WithSkipGitErrors (c . SkipGitErrors ),
94
79
),
95
80
)
96
81
if err != nil {
@@ -101,7 +86,32 @@ func (c *cmdServer) Execute(args []string) error {
101
86
return s .Start ()
102
87
}
103
88
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 {
105
115
if len (c .Git ) == 0 && len (c .Siva ) == 0 {
106
116
logrus .Error ("At least one git folder or siva folder should be provided." )
107
117
}
@@ -121,12 +131,12 @@ func (c *cmdServer) addDirectories() error {
121
131
return nil
122
132
}
123
133
124
- func (c * cmdServer ) addGitDirectory (folder string ) error {
134
+ func (c * Server ) addGitDirectory (folder string ) error {
125
135
logrus .WithField ("dir" , c .Git ).Debug ("git repositories directory added" )
126
136
return c .pool .AddDir (folder )
127
137
}
128
138
129
- func (c * cmdServer ) addSivaDirectory (folder string ) error {
139
+ func (c * Server ) addSivaDirectory (folder string ) error {
130
140
logrus .WithField ("dir" , c .Git ).Debug ("siva repositories directory added" )
131
141
return c .pool .AddSivaDir (folder )
132
142
}
0 commit comments