@@ -66,7 +66,7 @@ func NewRulesetFromEnv() RuleSet {
66
66
}
67
67
ruleSet , err := NewRuleset (rulesPath )
68
68
if err != nil {
69
- log .Panicln ( ruleSet )
69
+ log .Println ( err )
70
70
}
71
71
return ruleSet
72
72
}
@@ -79,26 +79,27 @@ func NewRuleset(rulePaths string) (RuleSet, error) {
79
79
errs := []error {}
80
80
81
81
rp := strings .Split (rulePaths , ";" )
82
+ var remoteRegex = regexp .MustCompile (`^https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()!@:%_\+.~#?&\/\/=]*)` )
82
83
for _ , rule := range rp {
83
84
rulePath := strings .Trim (rule , " " )
84
85
var err error
85
86
86
- isRemote , _ := regexp .MatchString (`^https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()!@:%_\+.~#?&\/\/=]*)` , rulePath )
87
+ isRemote := remoteRegex .MatchString (rulePath )
87
88
if isRemote {
88
89
err = ruleSet .loadRulesFromRemoteFile (rulePath )
89
90
} else {
90
91
err = ruleSet .loadRulesFromLocalDir (rulePath )
91
92
}
92
93
93
94
if err != nil {
94
- e := errors . New ( fmt .Sprintf ("WARN: failed to load ruleset from ''%s " , rulePath ) )
95
+ e := fmt .Errorf ("WARN: failed to load ruleset from '%s' " , rulePath )
95
96
errs = append (errs , errors .Join (e , err ))
96
97
continue
97
98
}
98
99
}
99
100
100
101
if len (errs ) != 0 {
101
- e := errors . New ( fmt .Sprintf ("WARN: failed to load %d rulesets" , len (rp ) ))
102
+ e := fmt .Errorf ("WARN: failed to load %d rulesets" , len (rp ))
102
103
errs = append (errs , e )
103
104
// panic if the user specified a local ruleset, but it wasn't found on disk
104
105
// don't fail silently
@@ -160,14 +161,14 @@ func (rs *RuleSet) loadRulesFromLocalDir(path string) error {
160
161
func (rs * RuleSet ) loadRulesFromLocalFile (path string ) error {
161
162
yamlFile , err := os .ReadFile (path )
162
163
if err != nil {
163
- e := errors . New ( fmt .Sprintf ("failed to read rules from local file: '%s'" , path ) )
164
+ e := fmt .Errorf ("failed to read rules from local file: '%s'" , path )
164
165
return errors .Join (e , err )
165
166
}
166
167
167
168
var r RuleSet
168
169
err = yaml .Unmarshal (yamlFile , & r )
169
170
if err != nil {
170
- e := errors . New ( fmt .Sprintf ("failed to load rules from local file, possible syntax error in '%s'" , path ) )
171
+ e := fmt .Errorf ("failed to load rules from local file, possible syntax error in '%s'" , path )
171
172
ee := errors .Join (e , err )
172
173
if _ , ok := os .LookupEnv ("DEBUG" ); ok {
173
174
debugPrintRule (string (yamlFile ), ee )
@@ -185,13 +186,13 @@ func (rs *RuleSet) loadRulesFromRemoteFile(rulesUrl string) error {
185
186
var r RuleSet
186
187
resp , err := http .Get (rulesUrl )
187
188
if err != nil {
188
- e := errors . New ( fmt .Sprintf ("failed to load rules from remote url '%s'" , rulesUrl ) )
189
+ e := fmt .Errorf ("failed to load rules from remote url '%s'" , rulesUrl )
189
190
return errors .Join (e , err )
190
191
}
191
192
defer resp .Body .Close ()
192
193
193
194
if resp .StatusCode >= 400 {
194
- e := errors . New ( fmt .Sprintf ("failed to load rules from remote url (%s) on '%s'" , resp .Status , rulesUrl ) )
195
+ e := fmt .Errorf ("failed to load rules from remote url (%s) on '%s'" , resp .Status , rulesUrl )
195
196
return errors .Join (e , err )
196
197
}
197
198
@@ -210,7 +211,7 @@ func (rs *RuleSet) loadRulesFromRemoteFile(rulesUrl string) error {
210
211
err = yaml .NewDecoder (reader ).Decode (& r )
211
212
212
213
if err != nil {
213
- e := errors . New ( fmt .Sprintf ("failed to load rules from remote url '%s' with status code '%s' and possible syntax error" , rulesUrl , resp .Status ) )
214
+ e := fmt .Errorf ("failed to load rules from remote url '%s' with status code '%s' and possible syntax error" , rulesUrl , resp .Status )
214
215
ee := errors .Join (e , err )
215
216
return ee
216
217
}
0 commit comments