@@ -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
2828var (
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+
8791func 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}
0 commit comments