forked from danvergara/dblab
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroot.go
More file actions
112 lines (97 loc) · 2.89 KB
/
Copy pathroot.go
File metadata and controls
112 lines (97 loc) · 2.89 KB
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
package cmd
import (
"github.com/danvergara/dblab/pkg/app"
"github.com/danvergara/dblab/pkg/command"
"github.com/danvergara/dblab/pkg/config"
"github.com/danvergara/dblab/pkg/connection"
"github.com/danvergara/dblab/pkg/form"
"github.com/danvergara/gocui"
"github.com/spf13/cobra"
)
// Define all the global flags.
var (
cfg bool
driver string
url string
host string
port string
user string
pass string
db string
ssl string
limit int
)
// NewRootCmd returns the root command.
func NewRootCmd() *cobra.Command {
return &cobra.Command{
Use: "dblab",
Short: "Interactive database client",
Long: `dblab is a terminal UI based interactive database client for Postgres and MySQL.`,
RunE: func(cmd *cobra.Command, args []string) error {
var opts command.Options
var err error
if cfg {
opts, err = config.Init()
if err != nil {
return err
}
} else {
opts = command.Options{
Driver: driver,
URL: url,
Host: host,
Port: port,
User: user,
Pass: pass,
DBName: db,
SSL: ssl,
Limit: limit,
}
if form.IsEmpty(opts) {
opts, err = form.Run()
if err != nil {
return err
}
}
}
if err := connection.ValidateOpts(opts); err != nil {
return err
}
gcui, err := gocui.NewGui(gocui.OutputNormal)
if err != nil {
return err
}
app, err := app.New(gcui, opts)
if err != nil {
return err
}
if err := app.Run(); err != nil {
return err
}
return nil
},
}
}
// rootCmd represents the base command when called without any subcommands.
var rootCmd = NewRootCmd()
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
cobra.CheckErr(rootCmd.Execute())
}
func init() {
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.
rootCmd.PersistentFlags().BoolVarP(&cfg, "config", "", false, "get the connection data from a config file (default is $HOME/.dblab.yaml or the current directory)")
// global flags used to open a database connection.
rootCmd.Flags().StringVarP(&driver, "driver", "", "", "Database driver")
rootCmd.Flags().StringVarP(&url, "url", "u", "", "Database connection string")
rootCmd.Flags().StringVarP(&host, "host", "", "", "Server host name or IP")
rootCmd.Flags().StringVarP(&port, "port", "", "", "Server port")
rootCmd.Flags().StringVarP(&user, "user", "", "", "Database user")
rootCmd.Flags().StringVarP(&pass, "pass", "", "", "Password for user")
rootCmd.Flags().StringVarP(&db, "db", "", "", "Database name")
rootCmd.Flags().StringVarP(&ssl, "ssl", "", "", "SSL mode")
rootCmd.Flags().IntVarP(&limit, "limit", "", 100, "Size of the result set from the table content query")
}