dbus/config: implement file loading functions
Signed-off-by: Ophestra Umiker <cat@ophivana.moe>
This commit is contained in:
parent
84d8c27b5f
commit
aa2be18f47
@ -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) {
|
||||||
|
Loading…
Reference in New Issue
Block a user