diff options
| author | Ophestra Umiker <cat@ophivana.moe> | 2024-09-27 23:53:08 +0900 |
|---|---|---|
| committer | Ophestra Umiker <cat@ophivana.moe> | 2024-09-27 23:53:08 +0900 |
| commit | aa2be18f471892bdb103e58cbe850dafb5b6099c (patch) | |
| tree | 293e1acf054dd418ceb68c3d2be884a20053281c /dbus/config.go | |
| parent | 84d8c27b5f17ab8567b0b3667d95030f022a44e7 (diff) | |
dbus/config: implement file loading functions
Signed-off-by: Ophestra Umiker <cat@ophivana.moe>
Diffstat (limited to 'dbus/config.go')
| -rw-r--r-- | dbus/config.go | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/dbus/config.go b/dbus/config.go index 27b5273b..160a7ca2 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) { |
