check.go 855 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. //go:build ignore
  2. package config
  3. import (
  4. "context"
  5. "encoding/json"
  6. "errors"
  7. "fmt"
  8. "github.com/TecharoHQ/anubis/lib/checker"
  9. )
  10. var (
  11. ErrUnknownCheckType = errors.New("config.Bot.Check: unknown check type")
  12. )
  13. type AllChecks struct {
  14. All []Check `json:"all"`
  15. }
  16. type AnyChecks struct {
  17. All []Check `json:"any"`
  18. }
  19. type Check struct {
  20. Type string `json:"type"`
  21. Args json.RawMessage `json:"args"`
  22. }
  23. func (c *Check) Valid(ctx context.Context) error {
  24. var errs []error
  25. if len(c.Type) == 0 {
  26. errs = append(errs, ErrNoStoreBackend)
  27. }
  28. fac, ok := checker.Get(c.Type)
  29. switch ok {
  30. case true:
  31. if err := fac.Valid(ctx, c.Args); err != nil {
  32. errs = append(errs, err)
  33. }
  34. case false:
  35. errs = append(errs, fmt.Errorf("%w: %q", ErrUnknownCheckType, c.Type))
  36. }
  37. if len(errs) != 0 {
  38. return errors.Join(errs...)
  39. }
  40. return nil
  41. }