-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbundle.go
More file actions
244 lines (211 loc) · 5.27 KB
/
Copy pathbundle.go
File metadata and controls
244 lines (211 loc) · 5.27 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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package bundleparse
import (
"fmt"
"strings"
)
type Bundle struct {
Name string
Kind string
Branch string
Path string
Pin string
Conditional string
Autoload string
Pre string
Post string
FpathRule string
Line int
ExtraAnnotations map[string]string
}
const (
KeyKind = "kind"
KeyBranch = "branch"
KeyPath = "path"
KeyPin = "pin"
KeyConditional = "conditional"
KeyAutoload = "autoload"
KeyPre = "pre"
KeyPost = "post"
KeyFpathRule = "fpath-rule"
KindZsh = "zsh"
KindPath = "path"
KindFpath = "fpath"
KindDefer = "defer"
KindClone = "clone"
KindAutoload = "autoload"
FpathRuleAppend = "append"
FpathRulePrepend = "prepend"
)
var validKindValues = map[string]struct{}{
KindZsh: {},
KindPath: {},
KindFpath: {},
KindDefer: {},
KindClone: {},
KindAutoload: {},
}
var validFpathRules = map[string]struct{}{
FpathRuleAppend: {},
FpathRulePrepend: {},
}
type ParseError struct {
Line int
Err error
}
func (e ParseError) Error() string {
return fmt.Sprintf("line %d: %v", e.Line, e.Err)
}
func (e ParseError) Unwrap() error {
return e.Err
}
// ParseBundles parses a multi-line bundle specification.
// Each non-empty line is passed through ParseLine, then validated and
// converted into a Bundle struct.
func ParseBundles(input string) ([]Bundle, error) {
input = strings.ReplaceAll(input, "\r\n", "\n")
lines := strings.Split(input, "\n")
bundles := make([]Bundle, 0, len(lines))
var usingDirective *ParsedLine
for idx, line := range lines {
if strings.TrimSpace(line) == "" {
continue
}
parsed, err := ParseLine(line)
if err != nil {
return nil, ParseError{Line: idx + 1, Err: err}
}
if parsed.Directive == UsingDirective {
usingDirective = &parsed
continue
}
if parsed.Directive != BundleDirective || parsed.Name == "" {
continue
}
if usingDirective != nil && !strings.Contains(parsed.Name, "/") {
parsed = applyUsingDirective(parsed, *usingDirective)
}
bundle, err := bundleFromParsed(parsed)
if err != nil {
return nil, ParseError{Line: idx + 1, Err: err}
}
bundle.Line = idx + 1
bundles = append(bundles, bundle)
}
return bundles, nil
}
// ParseBundleLine parses a single bundle definition line.
func ParseBundleLine(line string) (Bundle, error) {
parsed, err := ParseLine(line)
if err != nil {
return Bundle{}, err
}
if parsed.Directive != BundleDirective {
return Bundle{}, nil
}
if parsed.Name == "" {
return Bundle{}, nil
}
bundle, err := bundleFromParsed(parsed)
if err != nil {
return Bundle{}, err
}
return bundle, nil
}
func applyUsingDirective(parsed ParsedLine, using ParsedLine) ParsedLine {
originalName := parsed.Name
parsed.Name = using.Name
if _, ok := parsed.Annotations[KeyPath]; !ok {
pathBase := using.Name
if usingPath, ok2 := using.Annotations[KeyPath]; ok2 && usingPath != "" {
pathBase = usingPath
}
if pathBase != "" {
pathBase = strings.TrimSuffix(pathBase, "/")
parsed.Annotations[KeyPath] = pathBase + "/" + originalName
}
}
for key, value := range using.Annotations {
if key == KeyPath {
continue
}
if _, ok := parsed.Annotations[key]; ok {
continue
}
parsed.Annotations[key] = value
}
return parsed
}
func bundleFromParsed(parsed ParsedLine) (Bundle, error) {
if parsed.Directive != BundleDirective {
return Bundle{}, fmt.Errorf("parsed line is not a bundle")
}
if strings.TrimSpace(parsed.Name) == "" {
return Bundle{}, fmt.Errorf("missing bundle name")
}
bundle := Bundle{Name: parsed.Name}
var extras map[string]string
if value, ok := parsed.Annotations[KeyKind]; ok {
if err := validateKind(value); err != nil {
return Bundle{}, err
}
bundle.Kind = value
}
if value, ok := parsed.Annotations[KeyBranch]; ok {
bundle.Branch = value
}
if value, ok := parsed.Annotations[KeyPath]; ok {
bundle.Path = value
}
if value, ok := parsed.Annotations[KeyPin]; ok {
bundle.Pin = value
}
if value, ok := parsed.Annotations[KeyConditional]; ok {
bundle.Conditional = value
}
if value, ok := parsed.Annotations[KeyAutoload]; ok {
bundle.Autoload = value
}
if value, ok := parsed.Annotations[KeyPre]; ok {
bundle.Pre = value
}
if value, ok := parsed.Annotations[KeyPost]; ok {
bundle.Post = value
}
if value, ok := parsed.Annotations[KeyFpathRule]; ok {
if err := validateFpathRule(value); err != nil {
return Bundle{}, err
}
bundle.FpathRule = value
}
for key, value := range parsed.Annotations {
switch key {
case KeyKind, KeyBranch, KeyPath, KeyPin, KeyConditional, KeyAutoload, KeyPre, KeyPost, KeyFpathRule:
continue
default:
if extras == nil {
extras = make(map[string]string, len(parsed.Annotations))
}
extras[key] = value
}
}
if extras == nil {
extras = map[string]string{}
}
bundle.ExtraAnnotations = extras
if bundle.Kind == "" {
bundle.Kind = KindZsh
}
return bundle, nil
}
func validateKind(value string) error {
if _, ok := validKindValues[value]; !ok {
return fmt.Errorf("invalid kind %q", value)
}
return nil
}
func validateFpathRule(value string) error {
if _, ok := validFpathRules[value]; !ok {
return fmt.Errorf("invalid fpath-rule %q", value)
}
return nil
}