label_printer/config/config.go
TT-392 4a0a055234 Squashed 'faxmachine/' content from commit d23200b
git-subtree-dir: faxmachine
git-subtree-split: d23200bcfdedb9f8cc57e6a3c65b5ef93fcbfd19
2024-09-12 20:45:00 +02:00

80 lines
1.7 KiB
Go

package config
import (
"log"
"os"
"github.com/BurntSushi/toml"
"github.com/adrg/xdg"
)
type Toplevel struct {
Ntfy Ntfy `toml:"ntfy"`
Matrix Matrix `toml:"matrix"`
Devices Devices `toml:"devices"`
}
type Ntfy struct {
Host string `toml:"host"`
AccessToken string `toml:"access_token"`
Topics map[string]Topic `toml:"topics"`
}
type Topic struct {
Name string `toml:"name"`
}
type Matrix struct {
UserID string `toml:"user_id"`
DeviceID string `toml:"device_id"`
AccessToken string `toml:"access_token"`
AdminRoom string `toml:"admin_room"`
Users map[string]MatrixUser `toml:"users"`
FeelgoodKey string `toml:"feelgood_key"`
Database string `toml:"database"`
MautrixLog string `toml:"mautrix_log"`
}
type MatrixUser struct{}
type Devices struct {
Printer string `toml:"printer"`
Keyboard string `toml:"keyboard"`
}
func open(altpath string) *os.File {
if altpath != "" {
file, err := os.Open(altpath)
if err != nil {
log.Fatalf("unable to open %s: %v\n", altpath, file)
}
return file
}
path, err := xdg.SearchConfigFile("faxmachine/config.toml")
if err != nil {
log.Fatalf("unable to locate config: %v\n", err)
}
file, err := os.Open(path)
if err != nil {
log.Fatalf("unable to open %s: %v\n", path, file)
}
return file
}
func Load(altpath string) *Toplevel {
file := open(altpath)
decoder := toml.NewDecoder(file)
var toplevel Toplevel
_, err := decoder.Decode(&toplevel)
if err != nil {
log.Fatalf("failed to parse config: %v\n", err)
}
return &toplevel
}