@@ -4,16 +4,22 @@ import (
44	"github.com/bivas/rivi/util" 
55	"github.com/spf13/viper" 
66	"regexp" 
7+ 	"strings" 
78)
89
910type  Condition  struct  {
1011	Order          int       `mapstructure:"order,omitempty"` 
1112	IfLabeled      []string  `mapstructure:"if-labeled,omitempty"` 
1213	SkipIfLabeled  []string  `mapstructure:"skip-if-labeled,omitempty"` 
13- 	Filter         struct  {
14+ 	Files           struct  {
1415		Patterns    []string  `mapstructure:"patterns,omitempty"` 
1516		Extensions  []string  `mapstructure:"extensions,omitempty"` 
16- 	} `mapstructure:"filter,omitempty"` 
17+ 	} `mapstructure:"files,omitempty"` 
18+ 	Title  struct  {
19+ 		StartsWith  string    `mapstructure:"starts-with,omitempty"` 
20+ 		EndsWith    string    `mapstructure:"ends-with,omitempty"` 
21+ 		Patterns    []string  `mapstructure:"patterns,omitempty"` 
22+ 	} `mapstructure:"title,omitempty"` 
1723}
1824
1925func  (c  * Condition ) checkIfLabeled (meta  EventData ) bool  {
@@ -32,11 +38,11 @@ func (c *Condition) checkIfLabeled(meta EventData) bool {
3238}
3339
3440func  (c  * Condition ) checkPattern (meta  EventData ) bool  {
35- 	if  len (c .Filter .Patterns ) ==  0  {
41+ 	if  len (c .Files .Patterns ) ==  0  {
3642		return  false 
3743	} else  {
3844		compiled  :=  make ([]* regexp.Regexp , 0 )
39- 		for  _ , pattern  :=  range  c .Filter .Patterns  {
45+ 		for  _ , pattern  :=  range  c .Files .Patterns  {
4046			re , err  :=  regexp .Compile (pattern )
4147			if  err  !=  nil  {
4248				util .Logger .Warning ("Unable to compile regex '%s'. %s" , pattern , err )
@@ -60,11 +66,11 @@ func (c *Condition) checkPattern(meta EventData) bool {
6066}
6167
6268func  (c  * Condition ) checkExt (meta  EventData ) bool  {
63- 	if  len (c .Filter .Extensions ) ==  0  {
69+ 	if  len (c .Files .Extensions ) ==  0  {
6470		return  false 
6571	} else  {
6672		for  _ , check  :=  range  meta .GetFileExtensions () {
67- 			for  _ , ext  :=  range  c .Filter .Extensions  {
73+ 			for  _ , ext  :=  range  c .Files .Extensions  {
6874				if  ext  ==  check  {
6975					return  true 
7076				}
@@ -74,12 +80,54 @@ func (c *Condition) checkExt(meta EventData) bool {
7480	return  false 
7581}
7682
83+ func  (c  * Condition ) checkTitle (meta  EventData ) bool  {
84+ 	titleCondition  :=  c .Title 
85+ 	if  titleCondition .StartsWith  ==  ""  &&  titleCondition .EndsWith  ==  ""  &&  len (titleCondition .Patterns ) ==  0  {
86+ 		return  false 
87+ 	} else  {
88+ 		title  :=  meta .GetTitle ()
89+ 		if  titleCondition .StartsWith  !=  ""  &&  strings .HasPrefix (title , titleCondition .StartsWith ) {
90+ 			return  true 
91+ 		}
92+ 		if  titleCondition .EndsWith  !=  ""  &&  strings .HasSuffix (title , titleCondition .EndsWith ) {
93+ 			return  true 
94+ 		}
95+ 		if  len (titleCondition .Patterns ) >  0  {
96+ 			compiled  :=  make ([]* regexp.Regexp , 0 )
97+ 			for  _ , pattern  :=  range  titleCondition .Patterns  {
98+ 				re , err  :=  regexp .Compile (pattern )
99+ 				if  err  !=  nil  {
100+ 					util .Logger .Warning ("Unable to compile regex '%s'. %s" , pattern , err )
101+ 					continue 
102+ 				}
103+ 				compiled  =  append (compiled , re )
104+ 			}
105+ 			if  len (compiled ) ==  0  {
106+ 				util .Logger .Error ("All configured patterns have failed to compile" )
107+ 				return  false 
108+ 			}
109+ 			for  _ , reg  :=  range  compiled  {
110+ 				if  reg .MatchString (title ) {
111+ 					return  true 
112+ 				}
113+ 			}
114+ 		}
115+ 	}
116+ 	return  false 
117+ }
118+ 
119+ func  (c  * Condition ) checkFiles (meta  EventData ) bool  {
120+ 	return  c .checkPattern (meta ) ||  c .checkExt (meta )
121+ }
122+ 
77123func  (c  * Condition ) checkAllEmpty (meta  EventData ) bool  {
78- 	return  len (c .IfLabeled ) ==  0  &&  len (c .Filter .Patterns ) ==  0  &&  len (c .Filter .Extensions ) ==  0 
124+ 	return  len (c .IfLabeled ) ==  0  && 
125+ 		len (c .Files .Patterns ) ==  0  &&  len (c .Files .Extensions ) ==  0  && 
126+ 		c .Title .StartsWith  ==  ""  &&  c .Title .EndsWith  ==  ""  &&  len (c .Title .Patterns ) ==  0 
79127}
80128
81129func  (c  * Condition ) Match (meta  EventData ) bool  {
82- 	match  :=  c .checkAllEmpty (meta ) ||  c .checkIfLabeled (meta ) ||  c .checkPattern (meta ) ||  c .checkExt (meta )
130+ 	match  :=  c .checkAllEmpty (meta ) ||  c .checkIfLabeled (meta ) ||  c .checkTitle (meta ) ||  c .checkFiles (meta )
83131
84132	if  match  {
85133		for  _ , check  :=  range  c .SkipIfLabeled  {
0 commit comments