/
main.go
220 lines (199 loc) · 5.45 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
// Package main provides the hyprpanel host binary
package main
import (
"errors"
"fmt"
"os"
"os/signal"
"path/filepath"
"runtime/debug"
"syscall"
"time"
"github.com/fsnotify/fsnotify"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/go-plugin"
"github.com/pdf/hyprpanel/config"
"github.com/pdf/hyprpanel/style"
"github.com/peterbourgon/ff/v4"
"github.com/peterbourgon/ff/v4/ffhelp"
"google.golang.org/protobuf/encoding/protojson"
)
const (
name = `hyprpanel`
crashTimeout = 2 * time.Second
crashRetry = 200 * time.Millisecond
crashCount = 3
)
func sigHandler(log hclog.Logger, h *host) {
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGTERM, syscall.SIGUSR1, syscall.SIGINT)
for s := range sigChan {
switch s {
case syscall.SIGTERM, syscall.SIGINT:
log.Warn(`Quitting`, `sig`, s.String())
h.Close()
default:
log.Warn(`Unhandled signal`, `sig`, s.String())
}
}
}
func main() {
fs := ff.NewFlagSet(name)
configPath := os.Getenv(`XDG_CONFIG_HOME`)
if configPath == `` {
configPath = filepath.Join(os.Getenv(`HOME`), `.config`, `hyprpanel`)
}
configFileDefault := filepath.Join(configPath, `config.json`)
configFile := fs.String('c', `config`, configFileDefault, `Path to configuration file`)
styleFileDefault := filepath.Join(configPath, `style.css`)
styleFile := fs.String('s', `style`, styleFileDefault, `Path to stylesheet`)
version := fs.BoolLong(`version`, `Display the application version`)
log := hclog.New(&hclog.LoggerOptions{
Name: `host`,
Output: os.Stdout,
})
if err := ff.Parse(fs, os.Args[1:], ff.WithEnvVarPrefix(`HYPRPANEL`)); err != nil {
fmt.Printf("%s\n", ffhelp.Flags(fs))
if errors.Is(err, ff.ErrHelp) {
os.Exit(0)
}
fmt.Printf("err=%v\n", err)
os.Exit(1)
}
if *version {
info, ok := debug.ReadBuildInfo()
if !ok {
fmt.Printf("%s unknown version", name)
os.Exit(1)
}
fmt.Printf("%s version %s built with %s\n", name, info.Main.Version, info.GoVersion)
os.Exit(0)
}
cfg, err := config.Load(*configFile)
if err != nil {
if !errors.Is(err, os.ErrNotExist) {
log.Error(`Failed loading configuration file`, `file`, *configFile, `err`, err)
os.Exit(1)
}
log.Warn(`Failed loading configuration file, creating with defaults`, `file`, *configFile)
cfg, err = config.Default()
if err != nil {
log.Error(`Failed loading default configuration file`, `err`, err)
os.Exit(1)
}
if err := os.MkdirAll(configPath, 0755); err != nil && err != os.ErrExist {
log.Error(`Failed creating configuration directory`, `path`, configPath, `err`, err)
os.Exit(1)
}
marshal := protojson.MarshalOptions{
Multiline: true,
Indent: "\t",
EmitUnpopulated: true,
UseProtoNames: true,
}
b, err := marshal.Marshal(cfg)
if err != nil {
log.Error(`Failed encoding default configuration file`, `err`, err)
os.Exit(1)
}
if err := os.WriteFile(*configFile, b, 0644); err != nil {
log.Error(`Failed writing default configuration file`, `file`, *configFile, `err`, err)
os.Exit(1)
}
}
log.SetLevel(hclog.Level(cfg.LogLevel))
stylesheet, err := style.Load(*styleFile)
if err != nil {
if !errors.Is(err, os.ErrNotExist) {
log.Error(`Failed loading stylesheet`, `file`, *styleFile, `err`, err)
os.Exit(1)
}
log.Warn(`Failed loading stylesheet, continuing with defaults`, `file`, *styleFile)
}
h, err := newHost(cfg, stylesheet, log)
if err != nil {
log.Error(`Failed initializing hyprpanel`, `err`, err)
os.Exit(1)
}
go sigHandler(log, h)
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Error(`Failed initializaing filesystem watcher`, `err`, err)
os.Exit(1)
}
defer watcher.Close()
go func() {
for {
select {
case evt := <-watcher.Events:
if !evt.Has(fsnotify.Write) && !evt.Has(fsnotify.Create) && !evt.Has(fsnotify.Remove) {
continue
}
switch evt.Name {
case *configFile:
cfg, err := config.Load(*configFile)
if os.IsNotExist(err) {
cfg, err = config.Default()
}
if err != nil {
log.Error(`Failed reloading config`, `err`, err)
continue
}
log.SetLevel(hclog.Level(cfg.LogLevel))
h.updateConfig(cfg)
case *styleFile:
stylesheet, err := style.Load(*styleFile)
if os.IsNotExist(err) {
stylesheet = style.Default
} else if err != nil {
log.Error(`Failed reloading stylesheet`, `err`, err)
continue
}
h.updateStyle(stylesheet)
}
case err := <-watcher.Errors:
if err != nil {
log.Error(`Filesystem watcher failed`, `err`, err)
os.Exit(1)
}
}
}
}()
configDir := filepath.Dir(*configFile)
styleDir := filepath.Dir(*styleFile)
if err := watcher.Add(configDir); err != nil {
log.Error(`Failed adding filesystem watch path`, `path`, configDir, `err`, err)
os.Exit(1)
}
if configDir != styleDir {
if err := watcher.Add(styleDir); err != nil {
log.Error(`Failed adding filesystem watch path`, `path`, styleDir, `err`, err)
os.Exit(1)
}
}
defer plugin.CleanupClients()
count := 0
timer := time.NewTimer(crashTimeout)
for {
err := h.run()
if err != nil && err != errReload {
count += 1
log.Error(`Clients failed`, `err`, err)
select {
case <-timer.C:
if count >= crashCount {
log.Error(`Restarting too quickly, terminating`)
os.Exit(1)
}
count = 0
timer.Reset(crashTimeout)
default:
}
time.Sleep(crashRetry)
continue
} else if err != nil && err == errReload {
continue
}
os.Exit(0)
}
}