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
|
--- a/dashing.go 2015-12-31 15:38:22.000000000 -0600
+++ b/dashing.go 2017-01-05 14:48:00.403357303 -0600
@@ -74,8 +74,13 @@
// Transform structs.
type Transform struct {
Type string
+
+ // Perform a replace operation on the text
Regexp *regexp.Regexp
Replacement string
+
+ // Skip files that don't match this path
+ MatchPath *regexp.Regexp
}
var ignoreHash map[string]bool
@@ -123,7 +128,7 @@
{
Name: "version",
Usage: "Print version and exit.",
- Action: func(c *cli.Context) { fmt.Println(version) },
+ Action: func(c *cli.Context) error { fmt.Println(version);return nil },
Flags: []cli.Flag{
cli.StringFlag{
Name: "config, f",
@@ -134,7 +139,7 @@
}
}
-func create(c *cli.Context) {
+func create(c *cli.Context) error {
f := c.String("config")
if len(f) == 0 {
f = "dashing.json"
@@ -159,10 +164,10 @@
os.Exit(1)
}
fmt.Printf("You may now edit %s", f)
-
+ return nil
}
-func build(c *cli.Context) {
+func build(c *cli.Context) error {
var dashing Dashing
source_depth := 0
@@ -207,10 +212,11 @@
db, err := createDB(name)
if err != nil {
fmt.Printf("Failed to create database: %s\n", err)
- return
+ return nil
}
defer db.Close()
texasRanger(source, source_depth, name, dashing, db)
+ return nil
}
func decodeSelectField(d *Dashing) error {
@@ -224,30 +230,37 @@
}
} else if rv.Kind() == reflect.Map {
val := val.(map[string]interface{})
- var ttype, treg, trep string
- if t, ok := val["type"]; ok {
- ttype = t.(string)
+ var ttype, trep string
+ var creg, cmatchpath *regexp.Regexp
+ var err error
+
+ if r, ok := val["type"]; ok {
+ ttype = r.(string)
}
if r, ok := val["regexp"]; ok {
- treg = r.(string)
+ creg, err = regexp.Compile(r.(string))
+ if err != nil {
+ return fmt.Errorf("failed to compile regexp '%s': %s", r.(string), err)
+ }
}
if r, ok := val["replacement"]; ok {
trep = r.(string)
}
- var creg *regexp.Regexp
- var err error
- if len(treg) > 0 {
- if creg, err = regexp.Compile(treg); err != nil {
- return fmt.Errorf("failed to compile regexp '%s': %s", treg, err)
+ if r, ok := val["matchpath"]; ok {
+ cmatchpath, err = regexp.Compile(r.(string))
+ if err != nil {
+ return fmt.Errorf("failed to compile regexp '%s': %s", r.(string), err)
}
}
trans = &Transform{
Type: ttype,
Regexp: creg,
Replacement: trep,
+ MatchPath: cmatchpath,
}
} else {
fmt.Errorf("Expected string or map. Kind is %s.", rv.Kind().String())
+ return nil
}
d.selectors[sel] = trans
}
@@ -441,6 +454,11 @@
}
for pattern, sel := range dashing.selectors {
+ // Skip this selector if file path doesn't match
+ if sel.MatchPath != nil && ! sel.MatchPath.MatchString(path) {
+ continue
+ }
+
m := css.MustCompile(pattern)
found := m.MatchAll(top)
for _, n := range found {
|