Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): support negative filters #339

Merged
merged 1 commit into from
Aug 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions docs/docs/targets.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,14 @@ apiVersion: apps/v1
kind: Deployment
# ...
```

## Excluding

Sometimes it may be desirably to exclude a single object, instead of including all others.

To do so, prepend the regular expression with an exclamation mark (`!`), like so:

```bash
# filter out all Deployments
$ tk show . -t '!deployment/.*'
```
46 changes: 45 additions & 1 deletion pkg/process/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@ import (
)

// Filter returns all elements of the list that match at least one expression
// and are not ignored
func Filter(list manifest.List, exprs Matchers) manifest.List {
out := make(manifest.List, 0, len(list))
for _, m := range list {
if !exprs.MatchString(m.KindName()) {
continue
}
if exprs.IgnoreString(m.KindName()) {
continue
}
out = append(out, m)
}
return out
Expand All @@ -26,7 +30,13 @@ type Matcher interface {
MatchString(string) bool
}

// Ignorer is like matcher, but for explicitely ignoring resources
type Ignorer interface {
IgnoreString(string) bool
}

// Matchers is a collection of multiple expressions.
// A matcher may also implement Ignorer to explicitely ignore fields
type Matchers []Matcher

// MatchString returns whether at least one expression (OR) matches the string
Expand All @@ -38,6 +48,18 @@ func (e Matchers) MatchString(s string) bool {
return b
}

func (e Matchers) IgnoreString(s string) bool {
b := false
for _, exp := range e {
i, ok := exp.(Ignorer)
if !ok {
continue
}
b = b || i.IgnoreString(s)
}
return b
}

// RegExps is a helper to construct Matchers from regular expressions
func RegExps(rs []*regexp.Regexp) Matchers {
xprs := make(Matchers, 0, len(rs))
Expand All @@ -50,11 +72,20 @@ func RegExps(rs []*regexp.Regexp) Matchers {
func StrExps(strs ...string) (Matchers, error) {
exps := make(Matchers, 0, len(strs))
for _, raw := range strs {
s := fmt.Sprintf(`(?i)^%s$`, raw)
// trim exlamation mark, not supported by regex
s := fmt.Sprintf(`(?i)^%s$`, strings.TrimPrefix(raw, "!"))

// create regexp matcher
var exp Matcher
exp, err := regexp.Compile(s)
if err != nil {
return nil, ErrBadExpr{err}
}

// if negative (!), invert regex behaviour
if strings.HasPrefix(raw, "!") {
exp = NegMatcher{exp: exp}
}
exps = append(exps, exp)
}
return exps, nil
Expand All @@ -76,3 +107,16 @@ type ErrBadExpr struct {
func (e ErrBadExpr) Error() string {
return fmt.Sprintf("%s.\nSee https://tanka.dev/output-filtering/#regular-expressions for details on regular expressions.", strings.Title(e.inner.Error()))
}

// NexMatcher is a matcher that inverts the original behaviour
type NegMatcher struct {
exp Matcher
}

func (n NegMatcher) MatchString(s string) bool {
return true
}

func (n NegMatcher) IgnoreString(s string) bool {
return n.exp.MatchString(s)
}
9 changes: 9 additions & 0 deletions pkg/process/process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ func TestProcess(t *testing.T) {
`DePlOyMeNt/GrAfAnA`,
),
},
{
name: "targets-negative",
deep: testDataDeep().Deep,
flat: manifest.List{
testDataDeep().Flat[".app.web.frontend.nodejs.express.service"],
testDataDeep().Flat[".app.namespace"],
},
targets: MustStrExps(`!deployment/.*`),
},
{
name: "unwrap-list",
deep: loadFixture("list").Deep,
Expand Down