-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
225 lines (204 loc) · 5.44 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package main
import (
"bufio"
"bytes"
"fmt"
"io/ioutil"
"github.com/johan-bolmsjo/pot"
)
type Config struct {
env Environment
cmd map[string]*Command
}
type Environment map[string]string
type Command struct {
name string
exec string
logfile string
rtags_logfile string
append []string
prepend []string
filterOut []string
}
// Create a new configuration.
func NewConfig() *Config {
return &Config{
env: make(Environment),
cmd: make(map[string]*Command),
}
}
// Read configuration from file
func (config *Config) ReadFile(filename string) error {
buf, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
err = config.Parse(buf)
if err, ok := err.(*pot.ParseError); ok {
err.Identifier = filename
}
return err
}
// Parse configuration from byte slice.
func (config *Config) Parse(buf []byte) (err error) {
// Line number mapping taking removed comments into account.
locationAdjustments := make(map[uint32]pot.Location)
var locationAdjustment pot.Location
var lineNumber uint32
// Strip comments (lines beginning with "//").
// Comments are not parsable by the POT parser.
ioscanner := bufio.NewScanner(bytes.NewBuffer(buf))
buf = make([]byte, 0)
for ioscanner.Scan() {
line := ioscanner.Bytes()
if bytes.Index(line, []byte("//")) == 0 {
locationAdjustment.Line++
} else {
buf = append(buf, line...)
buf = append(buf, '\n')
locationAdjustments[lineNumber] = locationAdjustment
lineNumber++
}
}
if err = ioscanner.Err(); err != nil {
return err
}
var key *pot.DictKey
scanner := pot.NewParserScanner(pot.NewDictParser(buf))
for scanner.Scan() {
switch parser := scanner.SubParser().(type) {
case *pot.DictKey:
key = parser
default:
switch string(key.Bytes()) {
case "environment":
err = config.parseEnvironment(parser)
case "command":
err = config.parseCommand(parser)
default:
err = key.Location().Errorf("unrecognized section name '%s'", string(key.Bytes()))
}
scanner.InjectError(err)
}
}
if err = scanner.Err(); err != nil {
if err, ok := err.(*pot.ParseError); ok {
locationAdjustment = locationAdjustments[err.Location.Line]
err.Location.Add(&locationAdjustment)
}
}
return err
}
func (config *Config) GetEnvironment() Environment {
return config.env
}
func (config *Config) GetCommand(name string) (*Command, error) {
if cmd := config.cmd[name]; cmd != nil {
return cmd, nil
}
return nil, fmt.Errorf("command '%s' has not been configured")
}
func (config *Config) parseEnvironment(parser pot.Parser) error {
if _, ok := parser.(*pot.Dict); !ok {
return parser.Location().Errorf("expected dictionary")
}
var keyStr string
scanner := pot.NewParserScanner(parser)
for scanner.Scan() {
switch parser := scanner.SubParser().(type) {
case *pot.DictKey:
keyStr = string(parser.Bytes())
case *pot.String:
config.env.SetExpanded(keyStr, string(parser.Bytes()))
default:
scanner.InjectError(parser.Location().Errorf("expected string"))
}
}
return scanner.Err()
}
func parseListOfStrings(out *[]string, parser pot.Parser) error {
var err error
switch parser := parser.(type) {
case *pot.String:
*out = append(*out, string(parser.Bytes()))
case *pot.List:
scanner := pot.NewParserScanner(parser)
for scanner.Scan() {
switch parser := scanner.SubParser().(type) {
case *pot.String:
*out = append(*out, string(parser.Bytes()))
default:
scanner.InjectError(parser.Location().Errorf("expected string"))
}
}
err = scanner.Err()
default:
err = parser.Location().Errorf("expected string or list of strings")
}
return err
}
func parseString(out *string, parser pot.Parser) error {
s, ok := parser.(*pot.String)
if !ok {
return parser.Location().Errorf("expected string")
}
*out = string(s.Bytes())
return nil
}
func (config *Config) parseCommand(parser pot.Parser) error {
if _, ok := parser.(*pot.Dict); !ok {
return parser.Location().Errorf("expected dictionary")
}
var cmdNames []string
var cmdTmp Command
var key *pot.DictKey
scanner := pot.NewParserScanner(parser)
for scanner.Scan() {
switch parser := scanner.SubParser().(type) {
case *pot.DictKey:
key = parser
default:
var err error
switch string(key.Bytes()) {
case "append":
err = parseListOfStrings(&cmdTmp.append, parser)
case "exec":
err = parseString(&cmdTmp.exec, parser)
case "filter-out":
err = parseListOfStrings(&cmdTmp.filterOut, parser)
case "logfile":
err = parseString(&cmdTmp.logfile, parser)
case "name":
err = parseListOfStrings(&cmdNames, parser)
case "prepend":
err = parseListOfStrings(&cmdTmp.prepend, parser)
case "rtags-logfile":
err = parseString(&cmdTmp.rtags_logfile, parser)
default:
err = key.Location().Errorf("unrecognized parameter name '%s'", string(key.Bytes()))
}
scanner.InjectError(err)
}
}
setString := func(a *string, b string) {
if b != "" {
*a = b
}
}
for _, name := range cmdNames {
// Expand any environment references in the command name
name = config.env.Expand(name)
cmd := config.cmd[name]
if cmd == nil {
cmd = &Command{name: name}
config.cmd[name] = cmd
}
setString(&cmd.exec, cmdTmp.exec)
setString(&cmd.logfile, cmdTmp.logfile)
setString(&cmd.rtags_logfile, cmdTmp.rtags_logfile)
cmd.append = append(cmd.append, cmdTmp.append...)
cmd.prepend = append(cmd.prepend, cmdTmp.prepend...)
cmd.filterOut = append(cmd.filterOut, cmdTmp.filterOut...)
}
return scanner.Err()
}