diff --git a/lib/anubis_test.go b/lib/anubis_test.go index e9c9effc..903a4670 100644 --- a/lib/anubis_test.go +++ b/lib/anubis_test.go @@ -169,7 +169,7 @@ func httpClient(t *testing.T) *http.Client { } func TestLoadPolicies(t *testing.T) { - for _, fname := range []string{"botPolicies.json", "botPolicies.yaml"} { + for _, fname := range []string{"botPolicies.yaml"} { t.Run(fname, func(t *testing.T) { fin, err := data.BotPolicies.Open(fname) if err != nil { diff --git a/lib/policy/config/check.go b/lib/policy/config/check.go new file mode 100644 index 00000000..9e220855 --- /dev/null +++ b/lib/policy/config/check.go @@ -0,0 +1,53 @@ +//go:build ignore + +package config + +import ( + "context" + "encoding/json" + "errors" + "fmt" + + "github.com/TecharoHQ/anubis/lib/checker" +) + +var ( + ErrUnknownCheckType = errors.New("config.Bot.Check: unknown check type") +) + +type AllChecks struct { + All []Check `json:"all"` +} + +type AnyChecks struct { + All []Check `json:"any"` +} + +type Check struct { + Type string `json:"type"` + Args json.RawMessage `json:"args"` +} + +func (c *Check) Valid(ctx context.Context) error { + var errs []error + + if len(c.Type) == 0 { + errs = append(errs, ErrNoStoreBackend) + } + + fac, ok := checker.Get(c.Type) + switch ok { + case true: + if err := fac.Valid(ctx, c.Args); err != nil { + errs = append(errs, err) + } + case false: + errs = append(errs, fmt.Errorf("%w: %q", ErrUnknownCheckType, c.Type)) + } + + if len(errs) != 0 { + return errors.Join(errs...) + } + + return nil +} diff --git a/lib/policy/policy_test.go b/lib/policy/policy_test.go index 9d64d5cc..103728ac 100644 --- a/lib/policy/policy_test.go +++ b/lib/policy/policy_test.go @@ -13,13 +13,13 @@ import ( func TestDefaultPolicyMustParse(t *testing.T) { ctx := thothmock.WithMockThoth(t) - fin, err := data.BotPolicies.Open("botPolicies.json") + fin, err := data.BotPolicies.Open("botPolicies.yaml") if err != nil { t.Fatal(err) } defer fin.Close() - if _, err := ParseConfig(ctx, fin, "botPolicies.json", anubis.DefaultDifficulty); err != nil { + if _, err := ParseConfig(ctx, fin, "botPolicies.yaml", anubis.DefaultDifficulty); err != nil { t.Fatalf("can't parse config: %v", err) } }