-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.go
219 lines (186 loc) · 4.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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package main
import (
"fmt"
"io/ioutil"
"os"
"sync"
"time"
. "github.com/byrnedo/dockdash/logger"
goDocker "github.com/fsouza/go-dockerclient"
ui "github.com/gizak/termui/v3"
flag "github.com/ogier/pflag"
)
var (
newContainerChan chan goDocker.Container
removeContainerChan chan string
uiEventChan chan uiEvent
drawStatsChan chan StatsMsg
)
var logFileFlag = flag.String("log-file", "", "Path to log file")
var dockerEndpoint = flag.String("docker-endpoint", "", "Docker connection endpoint")
var helpFlag = flag.Bool("help", false, "help")
var versionFlag = flag.Bool("version", false, "print version")
func init() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: dockdash [options]\n\n")
flag.PrintDefaults()
}
flag.Parse()
if *helpFlag {
flag.Usage()
os.Exit(1)
}
if *versionFlag {
fmt.Println(VERSION)
os.Exit(0)
}
}
func main() {
if len(*logFileFlag) > 0 {
file, err := os.OpenFile(*logFileFlag, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
panic("Failed to open log file " + *logFileFlag + ":" + err.Error())
}
InitLog(ioutil.Discard, file, file, file)
} else {
InitLog(ioutil.Discard, ioutil.Discard, ioutil.Discard, ioutil.Discard)
}
var (
docker *goDocker.Client
err error
)
if len(*dockerEndpoint) > 0 {
docker, err = goDocker.NewClient(*dockerEndpoint)
} else {
docker, err = goDocker.NewClientFromEnv()
}
if err != nil {
panic(err)
}
err = ui.Init()
if err != nil {
panic(err)
}
defer ui.Close()
var uiView = NewView()
uiView.SetLayout()
newContainerChan = make(chan goDocker.Container)
removeContainerChan = make(chan string)
drawStatsChan = make(chan StatsMsg)
uiEventChan = make(chan uiEvent)
// Statistics
sl := &StatsListener{DockerClient: docker}
//setup initial containers
uiView.Render()
go handleUiEvents()
Info.Println("ui event loop running")
wg := sync.WaitGroup{}
go func() {
wg.Add(1)
mainLoop(uiView, sl)
wg.Done()
}()
Info.Println("opening stats listener")
sl.Open(newContainerChan, removeContainerChan, drawStatsChan)
Info.Println("stats listener open")
wg.Wait()
}
func mainLoop(uiView *view, sl *StatsListener) {
var (
inspectMode = false
horizPosition = 0
offset = 0
maxOffset = 0
currentStats *StatsMsg
currentContainers = make(containerMap)
ticker = time.NewTicker(1 * time.Second)
)
for {
select {
case e := <-uiEventChan:
switch e {
case Resize:
uiView.ResetSize()
case KeyQ, KeyCtrlC, KeyCtrlD:
sl.Close()
ui.Close()
os.Exit(0)
case KeyArrowLeft:
if horizPosition > 0 {
horizPosition--
}
uiView.RenderContainers(currentContainers, dockerInfoType(horizPosition), offset, inspectMode)
case KeyArrowRight:
if horizPosition < maxHorizPos {
horizPosition++
}
uiView.RenderContainers(currentContainers, dockerInfoType(horizPosition), offset, inspectMode)
case KeyArrowDown:
if offset < maxOffset && offset < maxContainers {
offset++
}
uiView.RenderContainers(currentContainers, dockerInfoType(horizPosition), offset, inspectMode)
//shift the list down
case KeyArrowUp:
if offset > 0 {
offset--
}
uiView.RenderContainers(currentContainers, dockerInfoType(horizPosition), offset, inspectMode)
//shift the list up
case KeyI:
inspectMode = !inspectMode
uiView.RenderContainers(currentContainers, dockerInfoType(horizPosition), offset, inspectMode)
default:
Info.Printf("Got unhandled key %+v\n", e)
}
case cont := <-newContainerChan:
Info.Println("Got new containers event")
currentContainers[cont.ID] = container{&cont}
maxOffset = len(currentContainers) - 1
uiView.RenderContainers(currentContainers, dockerInfoType(horizPosition), offset, inspectMode)
case removedContainerID := <-removeContainerChan:
maxOffset = len(currentContainers) - 1
if offset >= maxOffset {
offset = maxOffset
}
Info.Printf("%d, %d, %d", offset, maxOffset, horizPosition)
Info.Println("Got dead container event")
delete(currentContainers, removedContainerID)
uiView.RenderContainers(currentContainers, dockerInfoType(horizPosition), offset, inspectMode)
case newStatsCharts := <-drawStatsChan:
currentStats = &newStatsCharts
uiView.UpdateStats(currentStats, offset)
case <-ticker.C:
uiView.UpdateInfoBar(currentContainers, currentStats)
}
}
}
func handleUiEvents() {
uiEvents := ui.PollEvents()
for {
select {
case e := <-uiEvents:
Info.Printf("%s - %s\n", e.ID, e.Type)
switch e.ID {
case "q":
uiEventChan <- KeyQ
case "<C-c>":
uiEventChan <- KeyCtrlC
case "<C-d>":
uiEventChan <- KeyCtrlD
case "<Left>":
uiEventChan <- KeyArrowLeft
case "<Right>":
uiEventChan <- KeyArrowRight
case "<Down>":
uiEventChan <- KeyArrowDown
case "<Up>":
uiEventChan <- KeyArrowUp
case "<Resize>":
uiEventChan <- Resize
case "i":
uiEventChan <- KeyI
}
}
}
}