1
0

closer_config.go 705 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package closer
  2. import (
  3. "context"
  4. "time"
  5. )
  6. type Config struct {
  7. timeout *time.Duration
  8. logger Logger
  9. }
  10. type ConfigOption func(*Config)
  11. type ConfigOptions []ConfigOption
  12. type Logger interface {
  13. Info(ctx context.Context, msg string)
  14. Errorf(ctx context.Context, format string, args ...any)
  15. }
  16. func (s *ConfigOptions) Add(option ConfigOption) {
  17. *s = append(*s, option)
  18. }
  19. func NewConfig(opts ...ConfigOption) Config {
  20. c := &Config{}
  21. for _, opt := range opts {
  22. opt(c)
  23. }
  24. return *c
  25. }
  26. func WithTimeout(timeout time.Duration) ConfigOption {
  27. return func(s *Config) {
  28. s.timeout = &timeout
  29. }
  30. }
  31. func WithLogger(logger Logger) ConfigOption {
  32. return func(s *Config) {
  33. s.logger = logger
  34. }
  35. }