Skip to content

Commit 03c8a2e

Browse files
committed
simplify filter
1 parent fcb5ca3 commit 03c8a2e

File tree

3 files changed

+26
-39
lines changed

3 files changed

+26
-39
lines changed

cmd/debug.go

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@ import (
1919
"strings"
2020

2121
"github.com/anduintransaction/rivendell/project"
22-
"github.com/anduintransaction/rivendell/project/filters"
22+
pfilters "github.com/anduintransaction/rivendell/project/filters"
2323
"github.com/anduintransaction/rivendell/project/formatters"
2424
"github.com/anduintransaction/rivendell/utils"
2525
"github.com/spf13/cobra"
2626
)
2727

2828
var (
2929
outputFormat string
30-
filterGroups []string
31-
filterGroupsRegex string
30+
filterGroup string
31+
filterExact bool
3232
debugPrintResource bool
3333
)
3434

@@ -45,23 +45,8 @@ var debugCmd = &cobra.Command{
4545
}
4646

4747
var filterFns []project.FilterFunc
48-
49-
if len(filterGroups) > 0 {
50-
filterFns = append(filterFns, filters.FilterBySlice(filterGroups))
51-
}
52-
53-
var filterPattern *regexp.Regexp
54-
if fgr := strings.TrimSpace(filterGroupsRegex); fgr != "" {
55-
filterPattern, err = regexp.Compile(fgr)
56-
if err != nil {
57-
utils.Warn("Invalid filter group regex. Ignore pattern")
58-
}
59-
if filterPattern != nil {
60-
filterFns = append(filterFns, filters.FilterByRegex(filterPattern))
61-
}
62-
}
63-
64-
p.SetFilter(filters.CombineFilter(filterFns...))
48+
addFilterRegex(&filterFns)
49+
p.SetFilter(pfilters.CombineFilter(filterFns...))
6550

6651
var formatter project.Formatter
6752
switch strings.ToLower(outputFormat) {
@@ -84,11 +69,30 @@ var debugCmd = &cobra.Command{
8469
},
8570
}
8671

72+
func addFilterRegex(filters *[]project.FilterFunc) {
73+
p := strings.TrimSpace(filterGroup)
74+
if p == "" {
75+
return
76+
}
77+
if filterExact && !strings.HasPrefix("^", p) {
78+
p = "^" + p
79+
}
80+
if filterExact && !strings.HasSuffix("$", p) {
81+
p = p + "$"
82+
}
83+
rexp, err := regexp.Compile(p)
84+
if err != nil {
85+
utils.Warn("Invalid filter group pattern. Ignoring filter")
86+
return
87+
}
88+
*filters = append(*filters, pfilters.FilterByRegex(rexp))
89+
}
90+
8791
func init() {
8892
RootCmd.AddCommand(debugCmd)
8993

9094
debugCmd.Flags().StringVarP(&outputFormat, "output", "o", "console", "print format. One of: console|yaml|tree|config")
91-
debugCmd.Flags().StringSliceVar(&filterGroups, "filter-groups", []string{}, "Only print resource groups")
92-
debugCmd.Flags().StringVar(&filterGroupsRegex, "filter-groups-regex", "", "Only print resource groups matching pattern")
95+
debugCmd.Flags().StringVar(&filterGroup, "filter-group", "", "Only print resource groups")
96+
debugCmd.Flags().BoolVar(&filterExact, "exact", false, "Filter group by exact match")
9397
debugCmd.Flags().BoolVar(&debugPrintResource, "print-resource", false, "Print resource in tree format")
9498
}

go.sum

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,6 @@ golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96b
467467
golang.org/x/net v0.0.0-20210410081132-afb366fc7cd1/go.mod h1:9tjilg8BloeKEkVJvy7fQ90B1CfIiPueXVOjqfkSzI8=
468468
golang.org/x/net v0.0.0-20210503060351-7fd8e65b6420/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
469469
golang.org/x/net v0.0.0-20210813160813-60bc85c4be6d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
470-
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
471470
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
472471
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
473472
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=

project/filters/slice.go

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)