Skip to content

Commit 1a3fe4b

Browse files
committed
Support doc of kind
1 parent bb62c25 commit 1a3fe4b

File tree

4 files changed

+65
-18
lines changed

4 files changed

+65
-18
lines changed

cmd/patch_evaluator/main.go

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,70 @@ import (
66
"os"
77

88
"github.com/DaoCloud-OpenSource/patch_evaluator"
9+
pflag "github.com/spf13/pflag"
10+
)
11+
12+
var (
13+
kind string // code or doc
14+
)
15+
16+
func init() {
17+
pflag.StringVar(&kind, "kind", "code", "code or doc")
18+
pflag.Parse()
19+
}
20+
21+
type ValueDefine struct {
22+
LowValue []patch_evaluator.Filterer
23+
NoValue []patch_evaluator.Filterer
24+
}
25+
26+
var (
27+
define = map[string]ValueDefine{
28+
"code": {
29+
LowValue: []patch_evaluator.Filterer{
30+
patch_evaluator.FocusSuffixFilterer{".sh", ".bash", ".c", ".go", ".py", ".java", ".cpp", ".h", ".hpp", ".yaml", ".yml"},
31+
patch_evaluator.PrefixFilterer{"test/", "tests/"},
32+
patch_evaluator.SuffixFilterer{"_test.go"},
33+
},
34+
NoValue: []patch_evaluator.Filterer{
35+
patch_evaluator.SuffixFilterer{".md"},
36+
patch_evaluator.PrefixFilterer{"vendor/"},
37+
patch_evaluator.ContainsFilterer{"generated", "testdata"},
38+
patch_evaluator.CommentFilterer{},
39+
patch_evaluator.EmptyLineFilterer{},
40+
},
41+
},
42+
"doc": {
43+
NoValue: []patch_evaluator.Filterer{
44+
patch_evaluator.PrefixFilterer{
45+
"content/de", "content/es", "content/fr", "content/hi", "content/id", "content/it", "content/ja", "content/ko", "content/no", "content/pl", "content/pt-br", "content/ru", "content/uk", "content/vi",
46+
},
47+
patch_evaluator.CommentFilterer{},
48+
patch_evaluator.EmptyLineFilterer{},
49+
},
50+
},
51+
}
952
)
1053

1154
func main() {
12-
if len(os.Args) < 2 {
55+
valueDefine, ok := define[kind]
56+
if !ok {
57+
log.Fatalf("kind %q not found", kind)
58+
}
59+
60+
args := pflag.Args()
61+
if len(args) < 1 {
1362
log.Fatal("need args")
1463
}
1564

16-
patch, err := os.Open(os.Args[1])
65+
patch, err := os.Open(args[0])
1766
if err != nil {
1867
log.Fatal(err)
1968
}
2069
defer patch.Close()
2170

2271
evaluator := patch_evaluator.Evaluator{}
23-
files, reasonsLowValue, reasonsNoValue, err := evaluator.Evaluate(patch)
72+
files, reasonsLowValue, reasonsNoValue, err := evaluator.Evaluate(patch, valueDefine.LowValue, valueDefine.NoValue)
2473
if err != nil {
2574
log.Fatal(err)
2675
}

evaluator.go

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,12 @@ import (
1111
type Evaluator struct {
1212
}
1313

14-
func (e Evaluator) Evaluate(r io.Reader) ([]*gitdiff.File, []*Reasons, []*Reasons, error) {
14+
func (e Evaluator) Evaluate(r io.Reader, lowValue []Filterer, noValue []Filterer) ([]*gitdiff.File, []*Reasons, []*Reasons, error) {
1515
files, _, err := gitdiff.Parse(r)
1616
if err != nil {
1717
return nil, nil, nil, err
1818
}
1919

20-
lowValue := []Filterer{
21-
FocusSuffixFilterer{".sh", ".bash", ".c", ".go", ".py", ".java", ".cpp", ".h", ".hpp", ".yaml", ".yml"},
22-
PrefixFilterer{"test/", "tests/"},
23-
SuffixFilterer{"_test.go"},
24-
}
25-
26-
noValue := []Filterer{
27-
SuffixFilterer{".md"},
28-
PrefixFilterer{"vendor/"},
29-
ContainsFilterer{"generated", "testdata"},
30-
CommentFilterer{},
31-
EmptyLineFilterer{},
32-
}
33-
3420
filtered := []*gitdiff.File{}
3521

3622
reasonsLowValue := []*Reasons{}
@@ -208,3 +194,12 @@ func (s ContainsFilterer) Filter(file *gitdiff.File) *Reasons {
208194
}
209195
return nil
210196
}
197+
198+
type NotFilter struct {
199+
Filterer Filterer
200+
}
201+
202+
func (s NotFilter) Filter(file *gitdiff.File) *Reasons {
203+
s.Filterer.Filter(file)
204+
return nil
205+
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ go 1.17
55
require (
66
github.com/bluekeyes/go-gitdiff v0.6.0
77
github.com/sergi/go-diff v1.2.0
8+
github.com/spf13/pflag v1.0.5
89
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
1010
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
1111
github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=
1212
github.com/sergi/go-diff v1.2.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
13+
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
14+
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
1315
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
1416
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
1517
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=

0 commit comments

Comments
 (0)