-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprop.go
41 lines (37 loc) · 1.14 KB
/
prop.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
package main
import (
"fmt"
"regexp"
"strings"
)
// Prop defines a property that can be used in the RenameAllFiles to extract
// values from the original file name.
type Prop struct {
Name string
Matcher string
Regex *regexp.Regexp
}
// ParseProp populates a Prop object based on the string format passed to the cli: "propName=/regex/"
func ParseProp(v string) (Prop, error) {
kv := strings.Split(v, "=")
if len(kv) != 2 {
return Prop{}, fmt.Errorf("Invalid property definition %s. Property definitions must contain a name and a matcher: name=/matcher/", v)
}
if _, ok := ReservedVarNames[kv[0]]; ok {
return Prop{}, fmt.Errorf("The property name %s is reserved", kv[0])
}
return NewProp(kv[0], kv[1])
}
// NewProp creates a new Prop object for the given name and matcher regex. The regex string is compiled
// into a RegEx struct.
func NewProp(name, matcher string) (Prop, error) {
regex, err := regexp.Compile(matcher)
if err != nil {
return Prop{}, fmt.Errorf("Invalid matcher for pattern %s: Selector must be valid regular expressions. %v", matcher, err)
}
return Prop{
Name: name,
Matcher: matcher,
Regex: regex,
}, nil
}