2022-10-10 01:20:18 +02:00
|
|
|
package main
|
|
|
|
|
2022-10-10 01:30:39 +02:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"git.sr.ht/~emersion/go-scfg"
|
|
|
|
)
|
2022-10-10 01:20:18 +02:00
|
|
|
|
|
|
|
type config struct {
|
|
|
|
HTTPAddress string
|
|
|
|
LogLevel string
|
2022-10-10 14:04:51 +02:00
|
|
|
User string
|
|
|
|
Password string
|
2022-10-10 02:42:13 +02:00
|
|
|
ntfy ntfyConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
type ntfyConfig struct {
|
|
|
|
Topic string
|
|
|
|
User string
|
|
|
|
Password string
|
2022-10-10 01:20:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func readConfig(path string) (*config, error) {
|
|
|
|
cfg, err := scfg.Load(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
config := new(config)
|
|
|
|
// Set default values
|
|
|
|
config.HTTPAddress = "127.0.0.1:8080"
|
|
|
|
config.LogLevel = "info"
|
|
|
|
|
|
|
|
d := cfg.Get("log-level")
|
|
|
|
if d != nil {
|
|
|
|
if err := d.ParseParams(&config.LogLevel); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
d = cfg.Get("http-address")
|
|
|
|
if d != nil {
|
|
|
|
if err := d.ParseParams(&config.HTTPAddress); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-10 14:04:51 +02:00
|
|
|
d = cfg.Get("user")
|
|
|
|
if d != nil {
|
|
|
|
if err := d.ParseParams(&config.User); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
d = cfg.Get("password")
|
|
|
|
if d != nil {
|
|
|
|
if err := d.ParseParams(&config.Password); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-10 02:42:13 +02:00
|
|
|
ntfyDir := cfg.Get("ntfy")
|
|
|
|
if ntfyDir == nil {
|
|
|
|
return nil, fmt.Errorf("%q directive missing", "ntfy")
|
|
|
|
}
|
|
|
|
|
|
|
|
d = ntfyDir.Children.Get("topic")
|
2022-10-10 01:30:39 +02:00
|
|
|
if d == nil {
|
2022-10-10 02:42:13 +02:00
|
|
|
return nil, fmt.Errorf("%q missing from %q directive", "topic", "ntfy")
|
2022-10-10 01:30:39 +02:00
|
|
|
}
|
2022-10-10 02:42:13 +02:00
|
|
|
if err := d.ParseParams(&config.ntfy.Topic); err != nil {
|
2022-10-10 01:30:39 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-10-10 02:42:13 +02:00
|
|
|
d = ntfyDir.Children.Get("user")
|
|
|
|
if d != nil {
|
|
|
|
if err := d.ParseParams(&config.ntfy.User); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
d = ntfyDir.Children.Get("password")
|
|
|
|
if d != nil {
|
|
|
|
if err := d.ParseParams(&config.ntfy.Password); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-10 01:20:18 +02:00
|
|
|
return config, nil
|
|
|
|
}
|