forked from ngaut/sqltop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
117 lines (99 loc) · 1.86 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
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
113
114
115
116
117
package main
import (
"log"
"os"
"os/exec"
"time"
ui "github.com/gizak/termui/v3"
flag "github.com/spf13/pflag"
_ "github.com/go-sql-driver/mysql"
)
const version = "0.1"
type Conf struct {
Host string
DBPwd string
DBUser string
Port int
NumProcessToShow int
}
var (
host = flag.StringP("host", "h", "127.0.0.1", "host")
pwd = flag.StringP("password", "p", "", "pwd")
user = flag.StringP("user", "u", "root", "user")
port = flag.IntP("port", "P", 3306, "port")
count = flag.IntP("count", "n", 50, "Number of process to show")
cfg *Conf
)
func Config() *Conf {
return cfg
}
func InitConfig() {
flag.Parse()
cfg = &Conf{}
cfg.DBUser = *user
cfg.DBPwd = *pwd
cfg.Host = *host
cfg.Port = *port
cfg.NumProcessToShow = *count
}
func main() {
InitConfig()
if err := ui.Init(); err != nil {
log.Print(err)
os.Exit(-1)
}
if err := InitDB(); err != nil {
cleanExit(err)
}
defer func() {
ui.Close()
DB().Close()
}()
go refreshWorker()
refreshUI()
}
func cleanExit(err error) {
ui.Close()
exec.Command("clear").Run()
if err != nil {
log.Print(err)
}
os.Exit(0)
}
// refreshUI periodically refreshes the screen.
func refreshUI() {
controllers := []UIController{
newProcessListController(),
newOverviewController(),
newIOStatController(),
}
redraw := make(chan struct{})
go func() {
for {
for _, c := range controllers {
c.UpdateData()
}
redraw <- struct{}{}
// update every 2 seconds
time.Sleep(2 * time.Second)
}
}()
evt := ui.PollEvents()
for {
select {
case e := <-evt:
if e.Type == ui.KeyboardEvent && (e.ID == "q" || e.ID == "<C-c>") {
cleanExit(nil)
}
if e.ID == "<Resize>" {
for _, c := range controllers {
c.OnResize(e.Payload.(ui.Resize))
}
}
case <-redraw:
for _, c := range controllers {
c.Render()
}
}
}
}