From aa2be18f471892bdb103e58cbe850dafb5b6099c Mon Sep 17 00:00:00 2001 From: Ophestra Umiker Date: Fri, 27 Sep 2024 23:53:08 +0900 Subject: [PATCH] dbus/config: implement file loading functions Signed-off-by: Ophestra Umiker --- dbus/config.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/dbus/config.go b/dbus/config.go index 27b5273..160a7ca 100644 --- a/dbus/config.go +++ b/dbus/config.go @@ -1,5 +1,12 @@ package dbus +import ( + "encoding/json" + "errors" + "io" + "os" +) + type Config struct { // See set 'see' policy for NAME (--see=NAME) See []string `json:"see"` @@ -53,6 +60,23 @@ func (c *Config) Args(bus [2]string) (args []string) { return } +func (c *Config) Load(r io.Reader) error { + return json.NewDecoder(r).Decode(&c) +} + +// NewConfigFromFile opens the target config file at path and parses its contents into *Config. +func NewConfigFromFile(path string) (*Config, error) { + if f, err := os.Open(path); err != nil { + return nil, err + } else { + c := new(Config) + err1 := c.Load(f) + err = f.Close() + + return c, errors.Join(err1, err) + } +} + // NewConfig returns a reference to a Config struct with optional defaults. // If id is an empty string own defaults are omitted. func NewConfig(id string, defaults, mpris bool) (c *Config) {