Skip to content

Commit

Permalink
remove govaluate
Browse files Browse the repository at this point in the history
  • Loading branch information
Itay Donanhirsh committed Mar 1, 2021
1 parent 913e680 commit 883cf35
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 99 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module github.com/cluttercode/clutter
go 1.15

require (
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible
github.com/fsnotify/fsnotify v1.4.9
github.com/ghodss/yaml v1.0.1-0.20190212211648-25d852aebe32
github.com/go-git/go-git v4.7.0+incompatible
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible h1:1G1pk05UrOh0NlF1oeaaix1x8XzrfjIDK47TY0Zehcw=
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0=
github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7/go.mod h1:6zEj6s6u/ghQa61ZWa/C2Aw3RkjiTBOix7dkqa1VLIs=
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
Expand All @@ -14,8 +13,6 @@ github.com/emirpasic/gods v1.12.0/go.mod h1:YfzfFFoVP/catgzJb4IKIqXjX78Ha8FMSDh3
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI435gkrCt3MPfRiAkVrwSbHsst4LCFVfpJc=
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/ghodss/yaml v1.0.1-0.20190212211648-25d852aebe32 h1:Mn26/9ZMNWSw9C9ERFA1PUxfmGpolnw2v0bKOREu5ew=
github.com/ghodss/yaml v1.0.1-0.20190212211648-25d852aebe32/go.mod h1:GIjDIg/heH5DOkXY3YJ/wNhfHsQHoXGjl8G8amsYQ1I=
github.com/gliderlabs/ssh v0.2.2/go.mod h1:U7qILu1NlMHj9FlMhZLlkCdDnU1DBEAqr0aevW3Awn0=
Expand Down Expand Up @@ -67,6 +64,7 @@ go.uber.org/atomic v1.6.0 h1:Ezj3JGmsOnG1MoRWQkPBsKLe9DwWD9QeXzTRzzldNVk=
go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
go.uber.org/multierr v1.5.0 h1:KCa4XfM8CWFCpxXRGok+Q0SS/0XBhMDbHHGABQLvD2A=
go.uber.org/multierr v1.5.0/go.mod h1:FeouvMocqHpRaaGuG9EjoKcStLC43Zu/fmqdUMPcKYU=
go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee h1:0mgffUl7nfd+FpvXMVz4IDEaUSmT1ysygQC7qYo7sG4=
go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee/go.mod h1:vJERXedbb3MVM5f9Ejo0C68/HhF8uaILCdgjnY+goOA=
go.uber.org/zap v1.16.0 h1:uFRZXykJGK9lLY4HtgSw44DnIcAM+kRBP7x5m+NpAOM=
go.uber.org/zap v1.16.0/go.mod h1:MA8QOfq0BHJwdXa996Y4dYkAqRKB8/1K1QMMZVaNZjQ=
Expand All @@ -75,6 +73,7 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de h1:5hukYrvBGR8/eNkX5mdUezrA6JiaEZDtJb9Ei+1LlBs=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
Expand Down Expand Up @@ -121,4 +120,5 @@ gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.1-2019.2.3 h1:3JgtbtFHMiCmsznwGVTUWbgGov+pVqnlf1dEJTNAXeM=
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
12 changes: 0 additions & 12 deletions internal/pkg/index/attrs.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,10 @@ import (

type Attrs map[string]string

// used for [# govaluate-params #].
type AttrsStruct struct{ as Attrs }

func (a Attrs) ToStruct() *AttrsStruct {
return &AttrsStruct{as: a}
}

func AttrToString(k, v string) string {
if v == "" {
return k
}

return fmt.Sprintf("%s=%s", k, v)
}

func (a AttrsStruct) Has(k string) bool {
_, ok := a.as[k]
return ok
}
1 change: 0 additions & 1 deletion internal/pkg/linter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ type Rule struct {
PathGlob string `json:"path-glob"`
PathRegexp string `json:"path-re"`
Shell []string `json:"shell"`
Expr string `json:"expr"`
}

type Config struct {
Expand Down
83 changes: 2 additions & 81 deletions internal/pkg/linter/linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"regexp"
"strings"

"github.com/Knetic/govaluate"
"github.com/gobwas/glob"
"go.uber.org/zap"

Expand All @@ -35,12 +34,8 @@ func (ir *internalRule) init(l *Linter, r Rule) error {
return fmt.Errorf("path-pattern and path-re are mutuallye exclusive")
}

if len(r.Shell) != 0 && r.Expr != "" {
return fmt.Errorf("shell and expr are mutually exclusive")
}

if len(r.Shell) == 0 && r.Expr == "" {
return fmt.Errorf("either shell or expr are required")
if len(r.Shell) == 0 {
return fmt.Errorf("shell is expected")
}

ir.checkPath = func(string) bool { return true }
Expand Down Expand Up @@ -75,59 +70,6 @@ func (ir *internalRule) init(l *Linter, r Rule) error {
}
}

if expr := r.Expr; expr != "" {
funcs := map[string]govaluate.ExpressionFunction{
"re_match": func(vs ...interface{}) (interface{}, error) {
if len(vs) != 2 {
return nil, fmt.Errorf("expecting two arguments")
}

p, ok := vs[0].(string)
if !ok {
return nil, fmt.Errorf("pattern argument must be a string")
}

s, ok := vs[1].(string)
if !ok {
return nil, fmt.Errorf("text argument must be a string")
}

return regexp.MatchString(p, s)
},
"glob_match": func(vs ...interface{}) (interface{}, error) {
if len(vs) != 2 {
return nil, fmt.Errorf("expecting two arguments")
}

p, ok := vs[0].(string)
if !ok {
return nil, fmt.Errorf("pattern argument must be a string")
}

s, ok := vs[1].(string)
if !ok {
return nil, fmt.Errorf("text argument must be a string")
}

g, err := glob.Compile(p)
if err != nil {
return nil, err
}

return g.Match(s), nil
},
}

evalexpr, err := govaluate.NewEvaluableExpressionWithFunctions(expr, funcs)
if err != nil {
return fmt.Errorf("expr: %w", err)
}

ir.eval = func(_ context.Context, ent *index.Entry) (bool, error) {
return l.eval(evalexpr, ent)
}
}

if cmd := r.Shell; len(cmd) > 0 {
ir.eval = func(ctx context.Context, ent *index.Entry) (bool, error) {
return l.shell(ctx, cmd, ent)
Expand Down Expand Up @@ -245,24 +187,3 @@ func (l *Linter) shell(ctx context.Context, cmdParts []string, ent *index.Entry)

return true, nil
}

func (l *Linter) eval(eval *govaluate.EvaluableExpression, ent *index.Entry) (bool, error) {
l.z.Infow("checking expression")

res, err := eval.Evaluate(map[string]interface{}{"name": ent.Name, "path": ent.Loc.Path, "attrs": ent.Attrs.ToStruct()}) // [# govaluate-params #]
if err != nil {
l.z.Errorw("eval error", "err", err)
return false, err
}

l.z.Infow("returned", "res", res)

pass, ok := res.(bool)

if !ok {
l.z.Errorw("non-boolean result", "res", res)
return false, fmt.Errorf("expression result is not a boolean: %v", res)
}

return pass, nil
}

0 comments on commit 883cf35

Please sign in to comment.