Skip to content

Commit

Permalink
Support doc of kind
Browse files Browse the repository at this point in the history
  • Loading branch information
wzshiming committed Aug 17, 2022
1 parent bb62c25 commit 1a3fe4b
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 18 deletions.
55 changes: 52 additions & 3 deletions cmd/patch_evaluator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,70 @@ import (
"os"

"github.com/DaoCloud-OpenSource/patch_evaluator"
pflag "github.com/spf13/pflag"
)

var (
kind string // code or doc
)

func init() {
pflag.StringVar(&kind, "kind", "code", "code or doc")
pflag.Parse()
}

type ValueDefine struct {
LowValue []patch_evaluator.Filterer
NoValue []patch_evaluator.Filterer
}

var (
define = map[string]ValueDefine{
"code": {
LowValue: []patch_evaluator.Filterer{
patch_evaluator.FocusSuffixFilterer{".sh", ".bash", ".c", ".go", ".py", ".java", ".cpp", ".h", ".hpp", ".yaml", ".yml"},
patch_evaluator.PrefixFilterer{"test/", "tests/"},
patch_evaluator.SuffixFilterer{"_test.go"},
},
NoValue: []patch_evaluator.Filterer{
patch_evaluator.SuffixFilterer{".md"},
patch_evaluator.PrefixFilterer{"vendor/"},
patch_evaluator.ContainsFilterer{"generated", "testdata"},
patch_evaluator.CommentFilterer{},
patch_evaluator.EmptyLineFilterer{},
},
},
"doc": {
NoValue: []patch_evaluator.Filterer{
patch_evaluator.PrefixFilterer{
"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",
},
patch_evaluator.CommentFilterer{},
patch_evaluator.EmptyLineFilterer{},
},
},
}
)

func main() {
if len(os.Args) < 2 {
valueDefine, ok := define[kind]
if !ok {
log.Fatalf("kind %q not found", kind)
}

args := pflag.Args()
if len(args) < 1 {
log.Fatal("need args")
}

patch, err := os.Open(os.Args[1])
patch, err := os.Open(args[0])
if err != nil {
log.Fatal(err)
}
defer patch.Close()

evaluator := patch_evaluator.Evaluator{}
files, reasonsLowValue, reasonsNoValue, err := evaluator.Evaluate(patch)
files, reasonsLowValue, reasonsNoValue, err := evaluator.Evaluate(patch, valueDefine.LowValue, valueDefine.NoValue)
if err != nil {
log.Fatal(err)
}
Expand Down
25 changes: 10 additions & 15 deletions evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,12 @@ import (
type Evaluator struct {
}

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

lowValue := []Filterer{
FocusSuffixFilterer{".sh", ".bash", ".c", ".go", ".py", ".java", ".cpp", ".h", ".hpp", ".yaml", ".yml"},
PrefixFilterer{"test/", "tests/"},
SuffixFilterer{"_test.go"},
}

noValue := []Filterer{
SuffixFilterer{".md"},
PrefixFilterer{"vendor/"},
ContainsFilterer{"generated", "testdata"},
CommentFilterer{},
EmptyLineFilterer{},
}

filtered := []*gitdiff.File{}

reasonsLowValue := []*Reasons{}
Expand Down Expand Up @@ -208,3 +194,12 @@ func (s ContainsFilterer) Filter(file *gitdiff.File) *Reasons {
}
return nil
}

type NotFilter struct {
Filterer Filterer
}

func (s NotFilter) Filter(file *gitdiff.File) *Reasons {
s.Filterer.Filter(file)
return nil
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ go 1.17
require (
github.com/bluekeyes/go-gitdiff v0.6.0
github.com/sergi/go-diff v1.2.0
github.com/spf13/pflag v1.0.5
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=
github.com/sergi/go-diff v1.2.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
Expand Down

0 comments on commit 1a3fe4b

Please sign in to comment.