-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
267 lines (218 loc) · 5.26 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// @author: Brian Wojtczak
// @copyright: 2024 by Brian Wojtczak
// @license: BSD-style license found in the LICENSE file
package main
import (
"flag"
"fmt"
"github.com/awesome-gocui/gocui"
"github.com/pkg/errors"
"log"
"os"
"path"
"strings"
"sync/atomic"
"time"
tailEngine "github.com/k42-software/tailwatch/engine"
)
type layoutModeType int
const (
layoutModeTail layoutModeType = iota
layoutModeDialog
)
var (
currentLayoutMode = &atomic.Value{}
)
func main() {
// Parse Command Line Arguments
size, filename, filters := parseArguments()
// Start Tail Engine
engine, err := startTailEngine(size, filename, filters)
if engine != nil {
defer func() {
if err := engine.Close(); err != nil {
log.Printf("ERROR: %+v", err)
}
}()
}
if err != nil {
log.Fatalf("ERROR: %+v", err)
}
// Start GUI
if err = startGui(engine); err != nil {
log.Panicf("%+v", err)
}
}
func parseArguments() (
size int,
filename string,
filters []string,
) {
flag.Usage = func() {
app := path.Base(os.Args[0])
w := flag.CommandLine.Output()
_, _ = fmt.Fprintf(w, "Usage: %s [options] filename\n\n", app)
flag.PrintDefaults()
_, _ = fmt.Fprintf(w, "\n")
}
flag.BoolFunc(
"version",
"Show the version of the program",
func(_ string) error {
fmt.Printf("TailWatch\n")
fmt.Printf("Version: 0.0.1\n")
fmt.Printf("Author: Brian Wojtczak <[email protected]>\n")
os.Exit(0)
return nil
})
flag.IntVar(
&size,
"size",
128,
"The number of lines to buffer",
)
flag.Func(
"filter",
"Keyword(s) for filtering (can specify multiple times or as CSV)",
func(text string) error {
text = strings.TrimSpace(text)
filters = append(filters, strings.Split(text, ",")...)
return nil
})
flag.Parse()
if size <= 1 {
fmt.Println("ERROR: size must be greater than 1")
flag.Usage()
os.Exit(1)
}
if flag.NArg() < 1 {
fmt.Println("ERROR: filename is required")
flag.Usage()
os.Exit(1)
} else if flag.NArg() == 1 {
filename = flag.Arg(0)
} else {
fmt.Println("ERROR: too many positional arguments")
flag.Usage()
os.Exit(1)
}
return size, filename, filters
}
func startTailEngine(
size int,
filename string,
filters []string,
) (engine *tailEngine.Engine, err error) {
// Initialize Tail Engine
engine = tailEngine.NewTailEngine(size)
// Initialize the empty filter which gives us our "everything" view.
if _, err := engine.Filter(""); err != nil {
return engine, errors.WithStack(err)
}
// Initialize Filters from the command line arguments
if len(filters) > 0 {
for _, text := range filters {
if _, err := engine.Filter(text); err != nil {
return engine, errors.Wrapf(
err, "engine.Filter(%s)", text,
)
}
}
}
// Start Tail
err = engine.TailFile(filename)
if err != nil {
return engine, errors.WithStack(err)
}
return engine, nil
}
func startGui(engine *tailEngine.Engine) error {
// Initialize GUI
gui, err := gocui.NewGui(gocui.OutputNormal, false)
if err != nil {
return errors.WithStack(err)
}
defer gui.Close()
// Initialize the layout mode
if err = switchLayoutMode(gui, engine, layoutModeTail); err != nil {
return errors.WithStack(err)
}
// Updates
go func() {
ticker := time.NewTicker(time.Second / 15)
for range ticker.C {
if currentLayoutMode.Load() == layoutModeTail && engine.IsDirty() {
gui.Update(func(gui *gocui.Gui) error {
engine.SetDirty(false)
return nil
})
}
}
}()
// Main Loop
if err = gui.MainLoop(); err != nil && !errors.Is(err, gocui.ErrQuit) {
return errors.WithStack(err)
}
return nil
}
func switchLayoutMode(
gui *gocui.Gui,
engine *tailEngine.Engine,
targetLayout layoutModeType,
) error {
switch targetLayout {
case layoutModeTail:
if err := startLayoutModeTail(gui, engine); err != nil {
return errors.WithStack(err)
}
case layoutModeDialog:
if err := startLayoutModeDialog(gui, engine); err != nil {
return errors.WithStack(err)
}
default:
return errors.Errorf("unknown layout mode: %d", targetLayout)
}
if err := setGlobalKeybindings(gui); err != nil {
return errors.WithStack(err)
}
return nil
}
func startLayoutModeTail(gui *gocui.Gui, engine *tailEngine.Engine) error {
currentLayoutMode.Store(layoutModeTail)
layout := NewTailLayoutManager(engine)
gui.SetManager(layout)
layout.SetEventHandler("quit", func() error {
return quit(gui, nil)
})
layout.SetEventHandler("search", func() error {
return switchLayoutMode(gui, engine, layoutModeDialog)
})
if err := layout.SetLayoutKeybindings(gui); err != nil {
return errors.WithStack(err)
}
return nil
}
func startLayoutModeDialog(gui *gocui.Gui, engine *tailEngine.Engine) error {
currentLayoutMode.Store(layoutModeDialog)
layout := NewDialogLayoutManager(gui, engine)
gui.SetManager(layout)
layout.SetEventHandler("quit", func() error {
return quit(gui, nil)
})
layout.SetEventHandler("close", func() error {
return switchLayoutMode(gui, engine, layoutModeTail)
})
if err := layout.SetLayoutKeybindings(gui); err != nil {
return errors.WithStack(err)
}
return nil
}
func setGlobalKeybindings(gui *gocui.Gui) error {
if err := gui.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil {
return errors.WithStack(err)
}
return nil
}
func quit(_ *gocui.Gui, _ *gocui.View) error {
return gocui.ErrQuit
}