-
Notifications
You must be signed in to change notification settings - Fork 3
/
config.go
153 lines (133 loc) · 3.56 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
)
type Config struct {
Root *ConfigMap
}
type ConfigMap struct {
Name *string `json:"name,omitempty"`
LoopingKeys []string `json:"loopingKeys,omitempty"`
Keys ConfigBindings `json:"keys,omitempty"`
}
// FindOrAdd returns the config map associated with key in Keys.
//
// If no ConfigMap exists under the given key, a new anonymous
// ConfigMap is inserted and returned.
//
// If the binding at key does not refer to a ConfigMap, a new
// ConfigMap is constructed which overrides the existing binding.
func (c *ConfigMap) FindOrAdd(key string) *ConfigMap {
binding, found := c.Keys[key]
if !found {
c.Keys[key] = &ConfigBinding{}
binding = c.Keys[key]
}
if binding.Child == nil {
binding.ShellCommand = nil
binding.Child = &ConfigMap{
Keys: ConfigBindings{},
}
}
return binding.Child
}
// KeyMapName returns a name suitable for use as the name of a key map.
func (c *ConfigMap) KeyMapName() string {
if c.Name == nil {
return ""
}
return *c.Name
}
// ConfigBindings represents keybindings stored in a config file.
type ConfigBindings map[string]*ConfigBinding
// ConfigBinding is a single entry in ConfigBindings
type ConfigBinding struct {
ShellCommand *string
Child *ConfigMap
}
func (b *ConfigBinding) MarshalJSON() ([]byte, error) {
if b.Child != nil {
return json.Marshal(*b.Child)
}
return json.Marshal(*b.ShellCommand)
}
func (b *ConfigBinding) UnmarshalJSON(data []byte) error {
generic := (interface{})(nil)
if err := json.Unmarshal(data, &generic); err != nil {
return err
}
switch typed := generic.(type) {
case string:
b.ShellCommand = &typed
return nil
case map[string]interface{}:
b.Child = new(ConfigMap)
return json.Unmarshal(data, b.Child)
default:
return fmt.Errorf("Cannot parse %T as valid binding", typed)
}
}
func NewConfig() *Config {
return &Config{
Root: NewConfigMap("root"),
}
}
func NewConfigMap(name string) *ConfigMap {
return &ConfigMap{
Name: &name,
Keys: ConfigBindings{},
}
}
func (cfg *Config) EncodeJSON(out io.Writer) error {
marshaled, err := json.MarshalIndent(cfg.Root, "", " ")
if err != nil {
return err
}
_, err = io.Copy(out, bytes.NewBuffer(marshaled))
return err
}
func (cfg *Config) ParseJSON(in io.Reader) error {
return json.NewDecoder(in).Decode(cfg.Root)
}
// MergeIntoKeyMap merges the configuration settings in cfg into the provided keymap.
func (cfg *Config) MergeIntoKeyMap(context *Context, keymap *KeyMap) {
cfg.mergeConfigMap(context, cfg.Root, keymap)
}
func (cfg *Config) mergeConfigMap(context *Context, configMap *ConfigMap, keymap *KeyMap) {
isLoopingKey := func(key rune) bool {
for _, loopingKey := range configMap.LoopingKeys {
if key == []rune(loopingKey)[0] {
return true
}
}
return false
}
for key, binding := range configMap.Keys {
keyRune := ([]rune(key))[0]
keyBinding := NewKeyBinding(keyRune)
keyBinding.SetLooping(isLoopingKey(keyRune))
if binding.Child != nil {
existingBinding := keymap.LookupKey(keyRune)
if existingBinding == UnboundKey {
existingBinding = keyBinding
keymap.Set(existingBinding)
}
if existingBinding.Children().Name() == "" &&
binding.Child.Name != nil {
existingBinding.Children().Rename(*binding.Child.Name)
}
cfg.mergeConfigMap(context, binding.Child, existingBinding.Children())
continue
}
if binding.ShellCommand == nil {
continue
}
keyBinding.
Do(NewRunShellCommand(context, *binding.ShellCommand).Execute).
Describe(*binding.ShellCommand)
keymap.Set(keyBinding)
}
}