88 "regexp"
99 "strings"
1010
11- "github.com/Knetic/govaluate"
1211 "github.com/gobwas/glob"
1312 "go.uber.org/zap"
1413
@@ -35,12 +34,8 @@ func (ir *internalRule) init(l *Linter, r Rule) error {
3534 return fmt .Errorf ("path-pattern and path-re are mutuallye exclusive" )
3635 }
3736
38- if len (r .Shell ) != 0 && r .Expr != "" {
39- return fmt .Errorf ("shell and expr are mutually exclusive" )
40- }
41-
42- if len (r .Shell ) == 0 && r .Expr == "" {
43- return fmt .Errorf ("either shell or expr are required" )
37+ if len (r .Shell ) == 0 {
38+ return fmt .Errorf ("shell is expected" )
4439 }
4540
4641 ir .checkPath = func (string ) bool { return true }
@@ -75,59 +70,6 @@ func (ir *internalRule) init(l *Linter, r Rule) error {
7570 }
7671 }
7772
78- if expr := r .Expr ; expr != "" {
79- funcs := map [string ]govaluate.ExpressionFunction {
80- "re_match" : func (vs ... interface {}) (interface {}, error ) {
81- if len (vs ) != 2 {
82- return nil , fmt .Errorf ("expecting two arguments" )
83- }
84-
85- p , ok := vs [0 ].(string )
86- if ! ok {
87- return nil , fmt .Errorf ("pattern argument must be a string" )
88- }
89-
90- s , ok := vs [1 ].(string )
91- if ! ok {
92- return nil , fmt .Errorf ("text argument must be a string" )
93- }
94-
95- return regexp .MatchString (p , s )
96- },
97- "glob_match" : func (vs ... interface {}) (interface {}, error ) {
98- if len (vs ) != 2 {
99- return nil , fmt .Errorf ("expecting two arguments" )
100- }
101-
102- p , ok := vs [0 ].(string )
103- if ! ok {
104- return nil , fmt .Errorf ("pattern argument must be a string" )
105- }
106-
107- s , ok := vs [1 ].(string )
108- if ! ok {
109- return nil , fmt .Errorf ("text argument must be a string" )
110- }
111-
112- g , err := glob .Compile (p )
113- if err != nil {
114- return nil , err
115- }
116-
117- return g .Match (s ), nil
118- },
119- }
120-
121- evalexpr , err := govaluate .NewEvaluableExpressionWithFunctions (expr , funcs )
122- if err != nil {
123- return fmt .Errorf ("expr: %w" , err )
124- }
125-
126- ir .eval = func (_ context.Context , ent * index.Entry ) (bool , error ) {
127- return l .eval (evalexpr , ent )
128- }
129- }
130-
13173 if cmd := r .Shell ; len (cmd ) > 0 {
13274 ir .eval = func (ctx context.Context , ent * index.Entry ) (bool , error ) {
13375 return l .shell (ctx , cmd , ent )
@@ -245,24 +187,3 @@ func (l *Linter) shell(ctx context.Context, cmdParts []string, ent *index.Entry)
245187
246188 return true , nil
247189}
248-
249- func (l * Linter ) eval (eval * govaluate.EvaluableExpression , ent * index.Entry ) (bool , error ) {
250- l .z .Infow ("checking expression" )
251-
252- res , err := eval .Evaluate (map [string ]interface {}{"name" : ent .Name , "path" : ent .Loc .Path , "attrs" : ent .Attrs .ToStruct ()}) // [# govaluate-params #]
253- if err != nil {
254- l .z .Errorw ("eval error" , "err" , err )
255- return false , err
256- }
257-
258- l .z .Infow ("returned" , "res" , res )
259-
260- pass , ok := res .(bool )
261-
262- if ! ok {
263- l .z .Errorw ("non-boolean result" , "res" , res )
264- return false , fmt .Errorf ("expression result is not a boolean: %v" , res )
265- }
266-
267- return pass , nil
268- }
0 commit comments