-
-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathmain.go
86 lines (71 loc) · 1.93 KB
/
main.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
package main
import (
"flag"
"fmt"
"io"
"log"
"os"
"github.com/go-sql-driver/mysql"
"github.com/jorgerojas26/lazysql/app"
"github.com/jorgerojas26/lazysql/components"
"github.com/jorgerojas26/lazysql/helpers/logger"
)
var version = "dev"
func main() {
flag.Usage = func() {
f := flag.CommandLine.Output()
fmt.Fprintln(f, "lazysql")
fmt.Fprintln(f, "")
fmt.Fprintf(f, "Usage: %s [options] [connection_url]\n\n", os.Args[0])
fmt.Fprintln(f, " connection_url")
fmt.Fprintln(f, " database URL to connect to. Omit to start in picker mode")
fmt.Fprintln(f, "")
fmt.Fprintln(f, "Options:")
flag.PrintDefaults()
}
printVersion := flag.Bool("version", false, "Show version")
logLevel := flag.String("loglevel", "info", "Log level")
logFile := flag.String("logfile", "", "Log file")
flag.Parse()
if *printVersion {
println("LazySQL version: ", version)
os.Exit(0)
}
slogLevel, err := logger.ParseLogLevel(*logLevel)
if err != nil {
log.Fatalf("Error parsing log level: %v", err)
}
logger.SetLevel(slogLevel)
if *logFile != "" {
if err := logger.SetFile(*logFile); err != nil {
log.Fatalf("Error setting log file: %v", err)
}
}
logger.Info("Starting LazySQL...", nil)
if err := mysql.SetLogger(log.New(io.Discard, "", 0)); err != nil {
log.Fatalf("Error setting MySQL logger: %v", err)
}
// First load the config.
if err = app.LoadConfig(); err != nil {
log.Fatalf("Error loading config: %v", err)
}
// Now we can initialize the main pages.
mainPages := components.MainPages()
// Parse the command line arguments.
args := flag.Args()
switch len(args) {
case 0:
// Launch into the connection picker.
case 1:
// Set a connection from the command line.
err := components.InitFromArg(args[0])
if err != nil {
log.Fatal(err)
}
default:
log.Fatal("Only a single connection is allowed")
}
if err = app.App.Run(mainPages); err != nil {
log.Fatalf("Error running app: %v", err)
}
}