dbus/config: implement file loading functions

Signed-off-by: Ophestra Umiker <cat@ophivana.moe>
This commit is contained in:
Ophestra Umiker 2024-09-27 23:53:08 +09:00
parent 84d8c27b5f
commit aa2be18f47
Signed by: cat
SSH Key Fingerprint: SHA256:gQ67O0enBZ7UdZypgtspB2FDM1g3GVw8nX0XSdcFw8Q
1 changed files with 24 additions and 0 deletions

View File

@ -1,5 +1,12 @@
package dbus package dbus
import (
"encoding/json"
"errors"
"io"
"os"
)
type Config struct { type Config struct {
// See set 'see' policy for NAME (--see=NAME) // See set 'see' policy for NAME (--see=NAME)
See []string `json:"see"` See []string `json:"see"`
@ -53,6 +60,23 @@ func (c *Config) Args(bus [2]string) (args []string) {
return 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. // NewConfig returns a reference to a Config struct with optional defaults.
// If id is an empty string own defaults are omitted. // If id is an empty string own defaults are omitted.
func NewConfig(id string, defaults, mpris bool) (c *Config) { func NewConfig(id string, defaults, mpris bool) (c *Config) {