Skip to content

Commit 235dca8

Browse files
committed
minor ruleset improvements
1 parent 191279c commit 235dca8

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

pkg/ruleset/ruleset.go

+10-9
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func NewRulesetFromEnv() RuleSet {
6666
}
6767
ruleSet, err := NewRuleset(rulesPath)
6868
if err != nil {
69-
log.Panicln(ruleSet)
69+
log.Println(err)
7070
}
7171
return ruleSet
7272
}
@@ -79,26 +79,27 @@ func NewRuleset(rulePaths string) (RuleSet, error) {
7979
errs := []error{}
8080

8181
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()!@:%_\+.~#?&\/\/=]*)`)
8283
for _, rule := range rp {
8384
rulePath := strings.Trim(rule, " ")
8485
var err error
8586

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)
8788
if isRemote {
8889
err = ruleSet.loadRulesFromRemoteFile(rulePath)
8990
} else {
9091
err = ruleSet.loadRulesFromLocalDir(rulePath)
9192
}
9293

9394
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)
9596
errs = append(errs, errors.Join(e, err))
9697
continue
9798
}
9899
}
99100

100101
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))
102103
errs = append(errs, e)
103104
// panic if the user specified a local ruleset, but it wasn't found on disk
104105
// don't fail silently
@@ -160,14 +161,14 @@ func (rs *RuleSet) loadRulesFromLocalDir(path string) error {
160161
func (rs *RuleSet) loadRulesFromLocalFile(path string) error {
161162
yamlFile, err := os.ReadFile(path)
162163
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)
164165
return errors.Join(e, err)
165166
}
166167

167168
var r RuleSet
168169
err = yaml.Unmarshal(yamlFile, &r)
169170
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)
171172
ee := errors.Join(e, err)
172173
if _, ok := os.LookupEnv("DEBUG"); ok {
173174
debugPrintRule(string(yamlFile), ee)
@@ -185,13 +186,13 @@ func (rs *RuleSet) loadRulesFromRemoteFile(rulesUrl string) error {
185186
var r RuleSet
186187
resp, err := http.Get(rulesUrl)
187188
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)
189190
return errors.Join(e, err)
190191
}
191192
defer resp.Body.Close()
192193

193194
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)
195196
return errors.Join(e, err)
196197
}
197198

@@ -210,7 +211,7 @@ func (rs *RuleSet) loadRulesFromRemoteFile(rulesUrl string) error {
210211
err = yaml.NewDecoder(reader).Decode(&r)
211212

212213
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)
214215
ee := errors.Join(e, err)
215216
return ee
216217
}

0 commit comments

Comments
 (0)