-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
81 lines (67 loc) · 2.12 KB
/
config.go
File metadata and controls
81 lines (67 loc) · 2.12 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package adbi
import (
"encoding/json"
"io/ioutil"
"os"
"strings"
"unicode/utf8"
log "github.com/sirupsen/logrus"
)
// LoadConfigFile attempts to read a config-file from configDir.
// Failing that, LoadConfigFile parses defaultBindings for the
// configuration.
func LoadConfigFile(configDir, defaultBindings string) map[rune]Keyevent {
configFile := os.ExpandEnv(configDir) + "/config.json"
// 1. Open configFile; fallback to defaultBindings on error
// 2. get io.ReadCloser from either.
// 3. Unmarshal the json.
// 4. Cast values as strings.
keymapAsBytes, err := ioutil.ReadFile(configFile)
if err != nil {
log.WithFields(log.Fields{
"error": err,
}).Debug("Error reading config file. Using default keybindings.")
keymapAsBytes = []byte(defaultBindings)
}
keymapReader := strings.NewReader(string(keymapAsBytes))
keybindingsConfig, err := ioutil.ReadAll(keymapReader)
if err != nil {
log.WithFields(log.Fields{
"error": err,
}).Fatal("Error reading keybindings. Aborting startup.")
}
// Now json.Unmarshal.
var keybindingsContainer map[string]interface{}
err = json.Unmarshal(keybindingsConfig, &keybindingsContainer)
if err != nil {
log.WithFields(log.Fields{
"error": err,
}).Fatal("Error unmarshaling the configuration. Aborting startup.")
}
keybindings, ok := keybindingsContainer["keybindings"].(map[string]interface{})
if !ok {
log.WithFields(log.Fields{
"error": err,
}).Fatal("Malformed configuration. Aborting startup.")
}
keymap := map[rune]Keyevent{}
// e.g., "h": "KEYCODE_HOME"
for keycodeNewMapping, keycodeName := range keybindings {
// Skip on empty value
if len(keycodeNewMapping) == 0 {
continue
}
keycodeName = strings.ToUpper(keycodeName.(string))
// Is the specified keycodeName valid? If not, KEYCODE_UNKNOWN.
newKeyCode := Key(keycodeName.(string))
// Extract the first rune from keycodeNewMapping.
// That's the key we want to set.
key, _ := utf8.DecodeRuneInString(keycodeNewMapping)
keymap[key] = newKeyCode
}
// If no keys are defined, fail.
if len(keymap) == 0 {
log.Fatal("No keybindings are defined; aborting.")
}
return keymap
}