forked from muesli/deckmaster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
65 lines (53 loc) · 1.42 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"bytes"
"io/ioutil"
"github.com/BurntSushi/toml"
)
type DBusConfig struct {
Object string `toml:"object,omitempty"`
Path string `toml:"path,omitempty"`
Method string `toml:"method,omitempty"`
Value string `toml:"value,omitempty"`
}
type ActionConfig struct {
Deck string `toml:"deck,omitempty"`
Keycode string `toml:"keycode,omitempty"`
Exec string `toml:"exec,omitempty"`
Paste string `toml:"paste,omitempty"`
DBus DBusConfig `toml:"dbus,omitempty"`
}
type WidgetConfig struct {
ID string `toml:"id,omitempty"`
Config map[string]string `toml:"config,omitempty"`
}
type KeyConfig struct {
Index uint8 `toml:"index"`
Widget WidgetConfig `toml:"widget"`
Action *ActionConfig `toml:"action,omitempty"`
ActionHold *ActionConfig `toml:"action_hold,omitempty"`
}
type Keys []KeyConfig
type DeckConfig struct {
Keys Keys `toml:"keys"`
}
// LoadConfig loads config from filename.
func LoadConfig(filename string) (DeckConfig, error) {
config := DeckConfig{}
b, err := ioutil.ReadFile(filename)
if err != nil {
return config, err
}
_, err = toml.Decode(string(b), &config)
return config, err
}
// Save writes config as json to filename.
func (c DeckConfig) Save(filename string) error {
var b bytes.Buffer
e := toml.NewEncoder(&b)
err := e.Encode(c)
if err != nil {
return err
}
return ioutil.WriteFile(filename, b.Bytes(), 0644)
}