-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
162 lines (129 loc) · 3.35 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
// Yet Another i3status replacement written in Go.
package main
import (
"flag"
"fmt"
"os"
"os/signal"
"syscall"
"github.com/burik666/yagostatus/internal/config"
"github.com/burik666/yagostatus/internal/logger"
)
var builtinConfig = []byte(`
widgets:
- widget: static
blocks: >
[
{
"full_text": "YaGoStatus",
"color": "#2e9ef4"
}
]
events:
- button: 1
command: xdg-open https://github.com/burik666/yagostatus/
- widget: wrapper
command: /usr/bin/i3status
- widget: clock
format: Jan _2 Mon 15:04:05 # https://golang.org/pkg/time/#Time.Format
templates: >
[{
"color": "#ffffff",
"separator": true,
"separator_block_width": 21
}]
`)
func main() {
logger := logger.New()
var configFile string
flag.StringVar(&configFile, "config", "", `config file (default "yagostatus.yml")`)
versionFlag := flag.Bool("version", false, "print version information and exit")
swayFlag := flag.Bool("sway", false, "set it when using sway")
dumpConfigFlag := flag.Bool("dump", false, "dump parsed config file to stdout")
flag.Parse()
if *versionFlag {
logger.Infof("YaGoStatus %s", Version)
return
}
var initErrors []error
cfg, cfgError := loadConfig(configFile)
if cfgError != nil {
logger.Errorf("Failed to load config: %s", cfgError)
initErrors = append(initErrors, cfgError)
}
if cfg != nil {
logger.Infof("using config: %s", cfg.File)
} else {
cfg = &config.Config{}
}
if err := config.LoadPlugins(*cfg, logger); err != nil {
logger.Errorf("Failed to load plugins: %s", err)
initErrors = append(initErrors, err)
}
if *dumpConfigFlag {
b, err := config.Dump(cfg)
if err != nil {
logger.Errorf("Failed to dump config: %s", err)
os.Exit(1)
}
_, _ = os.Stdout.Write(b)
os.Exit(0)
}
if err := config.InitPlugins(logger); err != nil {
logger.Errorf("Failed to init plugins: %s", err)
initErrors = append(initErrors, err)
}
yaGoStatus := NewYaGoStatus(*cfg, *swayFlag, logger)
for _, err := range initErrors {
yaGoStatus.errorWidget(err.Error())
}
stopContSignals := make(chan os.Signal, 1)
signal.Notify(stopContSignals, cfg.Signals.StopSignal, cfg.Signals.ContSignal)
go func() {
for {
sig := <-stopContSignals
switch sig {
case cfg.Signals.StopSignal:
yaGoStatus.Stop()
case cfg.Signals.ContSignal:
yaGoStatus.Continue()
}
}
}()
shutdownsignals := make(chan os.Signal, 1)
signal.Notify(shutdownsignals,
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGQUIT,
syscall.SIGPIPE,
)
go func() {
if err := yaGoStatus.Run(); err != nil {
logger.Errorf("Failed to run yagostatus: %s", err)
}
shutdownsignals <- syscall.SIGTERM
}()
<-shutdownsignals
logger.Infof("shutdown")
config.ShutdownPlugins(logger)
yaGoStatus.Shutdown()
logger.Infof("exit")
}
func loadConfig(configFile string) (*config.Config, error) {
if configFile == "" {
configDir, err := os.UserConfigDir()
if err != nil {
return nil, fmt.Errorf("failed to get config dir: %w", err)
}
cfg, err := config.LoadFile(configDir + "/yagostatus/yagostatus.yml")
if os.IsNotExist(err) {
cfg, err := config.LoadFile("yagostatus.yml")
if os.IsNotExist(err) {
return config.Parse(builtinConfig, "builtin")
}
return cfg, err
}
return cfg, err
}
return config.LoadFile(configFile)
}