-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
249 lines (233 loc) · 5.27 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
package main
import (
"bufio"
"context"
"encoding/json"
"flag"
"io"
"os"
"os/signal"
"path"
"sync"
"syscall"
"time"
"github.com/fsnotify/fsnotify"
"github.com/kraftwerk28/gost/blocks"
"github.com/kraftwerk28/gost/core"
)
const programName = "gost"
func getConfigPath(flag string) string {
if len(flag) > 0 {
return flag
}
xdg, hasXdg := os.LookupEnv("XDG_CONFIG_HOME")
if !hasXdg {
home, err := os.UserHomeDir()
if err != nil {
return ""
}
xdg = path.Join(home, ".config")
}
p := path.Join(xdg, programName, "config.yml")
if _, err := os.Stat(p); err != nil {
p = path.Join(xdg, programName, "config.yaml")
}
if _, err := os.Stat(p); err != nil {
return ""
}
return p
}
func readEvents(ch chan *core.I3barClickEvent) {
log := core.Log
sc := bufio.NewScanner(os.Stdin)
sc.Scan() // Skip "["
for sc.Scan() {
raw := sc.Bytes()
ev, err := core.NewEventFromRaw(raw)
if err != nil {
log.Println(err)
continue
}
log.Printf("Click event: %+v\n", *ev)
ch <- ev
}
if err := sc.Err(); err != nil {
log.Fatal(err)
}
}
var isFirstFrame = true
func feedBlocks(o io.Writer, blocks []core.I3barBlock) (err error) {
if isFirstFrame {
isFirstFrame = false
} else {
if _, err = os.Stdout.WriteString(","); err != nil {
return
}
}
e := json.NewEncoder(os.Stdout)
e.SetEscapeHTML(false)
if err = e.Encode(blocks); err != nil {
return
}
return
}
func setupWatcher(path string) (w *fsnotify.Watcher, err error) {
if w, err = fsnotify.NewWatcher(); err != nil {
return
}
w.Add(path)
return
}
func processEvents(
ctx context.Context,
managers []core.BlockletMgr,
wg *sync.WaitGroup,
events chan *core.I3barClickEvent,
) {
for {
select {
case e := <-events:
for i := range managers {
m := &managers[i]
if m.MatchesEvent(e) {
wg.Add(1)
go m.ProcessEvent(e, ctx, wg)
}
}
case <-ctx.Done():
return
}
}
}
func stopWithTimeout(cancel context.CancelFunc, wg *sync.WaitGroup) bool {
wgDoneCh := make(chan struct{})
go func() {
wg.Wait()
wgDoneCh <- struct{}{}
}()
cancel()
tout := time.NewTimer(time.Second * 3)
select {
case <-wgDoneCh:
tout.Stop()
return true
case <-tout.C:
return false
}
}
func main() {
var cfgPathFlag, logPath string
var err error
flag.StringVar(&cfgPathFlag, "config", "", "Path to config.yml")
flag.StringVar(&logPath, "log", "", "Path to log file")
flag.Parse()
signalChan := make(chan os.Signal, 1)
signal.Notify(
signalChan,
syscall.SIGHUP, // reload config
syscall.SIGTERM, syscall.SIGINT, // exit
)
logWriter := os.Stderr
if logPath != "" {
if logWriter, err = os.OpenFile(
logPath,
os.O_CREATE|os.O_WRONLY|os.O_TRUNC,
0644,
); err != nil {
panic(err)
}
}
core.InitializeLogger(logWriter)
// Logger initialized at this point
log := core.Log
{
header := core.I3barHeader{
Version: 1,
ClickEvents: true,
}
b, _ := json.Marshal(header)
b = append(b, []byte("\n[\n")...)
os.Stdout.Write(b)
}
eventChan := make(chan *core.I3barClickEvent)
go readEvents(eventChan)
outerLoop:
for {
ctx, ctxCancel := context.WithCancel(context.Background())
wg := sync.WaitGroup{}
cfgPath := getConfigPath(cfgPathFlag)
if cfgPath == "" {
log.Fatalln("No config file found")
}
var cfg *core.AppConfig
var configWatcher *fsnotify.Watcher
var fileWatchChan chan fsnotify.Event
var managers []core.BlockletMgr
if cfg, err = core.LoadConfigFromFile(cfgPath); err != nil {
log.Println(err)
b := blocks.NewStaticBlock("Error loading the config: " + err.Error())
managers = []core.BlockletMgr{core.MakeBlockletMgr("error", b, nil)}
} else {
managers = cfg.CreateManagers(ctx)
if cfg.WatchConfig == nil || *cfg.WatchConfig {
configWatcher, err = setupWatcher(cfgPath)
if err == nil {
fileWatchChan = configWatcher.Events
log.Println("Watching config for changes")
} else {
log.Print(err)
}
}
}
wg.Add(len(managers))
updateChan := make(chan string)
for i := range managers {
go managers[i].Run(updateChan, ctx, &wg)
}
go processEvents(ctx, managers, &wg, eventChan)
renderLoop:
for {
blocks := make([]core.I3barBlock, 0, len(managers))
for i := range managers {
blocks = append(blocks, managers[i].Render()...)
}
if err := feedBlocks(os.Stdout, blocks); err != nil {
log.Print(err)
}
select {
case updateData := <-updateChan:
for i := range managers {
managers[i].TryInvalidate(updateData)
}
case signal := <-signalChan:
switch signal {
case syscall.SIGHUP:
log.Println("Waiting for blocklets to stop")
if !stopWithTimeout(ctxCancel, &wg) {
log.Println("Blocklets failed to stop")
}
break renderLoop
case syscall.SIGTERM, syscall.SIGINT:
log.Println("Waiting for blocklets to finish")
if !stopWithTimeout(ctxCancel, &wg) {
log.Println("Blocklets failed to stop")
}
break outerLoop
}
case e := <-fileWatchChan:
if e.Op == fsnotify.Write {
log.Println("Config change detected")
log.Println("Waiting for blocklets to finish")
if !stopWithTimeout(ctxCancel, &wg) {
log.Println("Blocklets failed to stop")
break outerLoop
} else {
configWatcher.Close()
break renderLoop
}
}
}
}
}
log.Println("Auf Wiedersehen")
}