-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexportfs.go
192 lines (157 loc) · 3.98 KB
/
exportfs.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
// Copyright 2015 Bowery, Inc.
package exportfs
import (
"bufio"
"io"
"regexp"
"strings"
"unicode"
)
var (
comReg = regexp.MustCompile(`#.*$`)
lineContReg = regexp.MustCompile(`\\[ \s]*$`)
whiteReg = regexp.MustCompile(`\s+`)
)
// Parse parses the exports content from the given reader, and returns a map
// with the path as the key, and the value being a list of machine exports
// for the path.
func Parse(input io.Reader) (map[string][]*Export, error) {
scanner := bufio.NewScanner(input)
exports := make(map[string][]*Export, 20)
for scanner.Scan() {
scannedLine := strings.TrimLeftFunc(scanner.Text(), unicode.IsSpace)
line, lineExports, err := parseLine(scannedLine)
if err != nil {
return nil, err
}
// If it's a line continuation read all of the continued lines.
if line != "" && lineExports == nil {
for scanner.Scan() {
newline := scanner.Text()
line, lineExports, err = parseLine(line + newline)
if err != nil {
return nil, err
}
if lineExports != nil {
break
}
}
if line != "" && lineExports == nil {
line, lineExports, err = parseLine(line)
if err != nil {
return nil, err
}
}
}
if lineExports != nil && len(lineExports) > 0 {
path := lineExports[0].Path
list, ok := exports[path]
if !ok {
exports[path] = lineExports
continue
}
exports[path] = append(list, lineExports...)
}
}
return exports, scanner.Err()
}
// parseLine parses the given line, if a line continuation is found, the
// line is returned with no export structure. Otherwise the line is parsed
// and the resulting exports are returned.
func parseLine(line string) (string, []*Export, error) {
line = stripComments(line)
if line == "" {
return "", nil, nil
}
// If the line ends with \ then it's a line continuation.
if lineContReg.MatchString(line) {
return lineContReg.ReplaceAllString(line, ""), nil, nil
}
var exports []*Export
var defaults []*Option
line, path := parsePath(line)
machines := strings.Fields(line)
// Parse each machine and it's options.
for _, machine := range machines {
// A default option list, merge into existing list.
if machine[0] == '-' {
defaults = mergeOpts(defaults, parseOpts(machine[1:]))
continue
}
exports = append(exports, parseMachine(machine, path, defaults))
}
return "", exports, nil
}
func parseMachine(line, path string, defaults []*Option) *Export {
export := &Export{Path: path, Options: defaults}
optIdx := strings.IndexByte(line, '(')
if optIdx == -1 {
export.Machine = line
return export
}
export.Machine = line[:optIdx]
export.Options = parseOpts(line[optIdx+1 : len(line)-1])
return export
}
// parseOpts parses the comma separated option list given.
func parseOpts(line string) []*Option {
vals := strings.Split(line, ",")
opts := make([]*Option, len(vals))
for idx, val := range vals {
opt := new(Option)
list := strings.SplitN(val, "=", 2)
opt.Key = list[0]
if len(list) > 1 {
opt.Value = list[1]
}
opts[idx] = opt
}
return opts
}
// parsePath gets the path from a line and returns the line stripping the
// path from it.
func parsePath(line string) (newline string, path string) {
var pathList []rune
isQuoted := false
idx := 0
for i, r := range line {
if (r == ' ' && !isQuoted) || (r == '"' && isQuoted) {
if isQuoted {
i++
}
idx = i
break
}
if r == '"' && !isQuoted {
isQuoted = true
continue
}
pathList = append(pathList, r)
}
return line[idx:], string(pathList)
}
// stripComments removes comments from the given line.
func stripComments(line string) string {
if comReg.MatchString(line) {
return comReg.ReplaceAllString(line, "")
}
return line
}
// mergeOpts updates options replacing existing keys.
func mergeOpts(base, changes []*Option) []*Option {
for _, opt := range changes {
idx := -1
for i, o := range base {
if o.Key == opt.Key {
idx = i
break
}
}
if idx > -1 {
base[idx] = opt
} else {
base = append(base, opt)
}
}
return base
}