mirror of
https://github.com/TecharoHQ/anubis.git
synced 2026-04-16 05:15:03 +00:00
feat(checker): port path checker
Signed-off-by: Xe Iaso <me@xeiaso.net>
This commit is contained in:
58
lib/checker/path/factory.go
Normal file
58
lib/checker/path/factory.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package path
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/TecharoHQ/anubis/internal"
|
||||
"github.com/TecharoHQ/anubis/lib/checker"
|
||||
)
|
||||
|
||||
func init() {
|
||||
checker.Register("path", Factory{})
|
||||
}
|
||||
|
||||
type Factory struct{}
|
||||
|
||||
func (f Factory) Build(ctx context.Context, data json.RawMessage) (checker.Interface, error) {
|
||||
var fc fileConfig
|
||||
|
||||
if err := json.Unmarshal([]byte(data), &fc); err != nil {
|
||||
return nil, errors.Join(checker.ErrUnparseableConfig, err)
|
||||
}
|
||||
|
||||
if err := fc.Valid(); err != nil {
|
||||
return nil, errors.Join(checker.ErrInvalidConfig, err)
|
||||
}
|
||||
|
||||
pathRex, err := regexp.Compile(strings.TrimSpace(fc.Regex))
|
||||
if err != nil {
|
||||
return nil, errors.Join(ErrInvalidRegex, err)
|
||||
}
|
||||
|
||||
return &Checker{
|
||||
regexp: pathRex,
|
||||
hash: internal.FastHash(fc.String()),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (f Factory) Valid(ctx context.Context, data json.RawMessage) error {
|
||||
var fc fileConfig
|
||||
|
||||
if err := json.Unmarshal([]byte(data), &fc); err != nil {
|
||||
return errors.Join(checker.ErrUnparseableConfig, err)
|
||||
}
|
||||
|
||||
return fc.Valid()
|
||||
}
|
||||
|
||||
func Valid(pathRex string) error {
|
||||
fc := fileConfig{
|
||||
Regex: pathRex,
|
||||
}
|
||||
|
||||
return fc.Valid()
|
||||
}
|
||||
Reference in New Issue
Block a user