config.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package daemon
  2. import (
  3. "errors"
  4. "git.beejay.kim/WatchDog/ward/platform"
  5. "git.beejay.kim/tool/service/config"
  6. "git.beejay.kim/tool/service/consumer"
  7. "git.beejay.kim/tool/service/producer"
  8. "reflect"
  9. "strings"
  10. )
  11. type Configuration struct {
  12. config.Configuration `yaml:",inline"`
  13. Consumer consumer.Config `yaml:"consumer"`
  14. Producer producer.Config `yaml:"producer"`
  15. Platform _platform `yaml:"platform"`
  16. }
  17. func (c Configuration) Invalidate() error {
  18. return config.Invalidate(c)
  19. }
  20. type _platform struct {
  21. Afreeca *platform.Afreeca `yaml:"afreeca"`
  22. }
  23. func (r *_platform) Detect(pid, bid string) (platform.Platform, error) {
  24. if pid = strings.TrimSpace(pid); pid == "" {
  25. return nil, errors.New("empty platform")
  26. }
  27. val := reflect.ValueOf(r).Elem()
  28. if val.Kind() != reflect.Struct {
  29. if val.IsZero() {
  30. return nil, errors.New("config is empty")
  31. }
  32. val = reflect.ValueOf(r).Elem()
  33. }
  34. for k := 0; k < val.NumField(); k++ {
  35. if !val.Field(k).CanInterface() || val.Field(k).IsZero() {
  36. continue
  37. }
  38. if elm, ok := val.Field(k).Interface().(platform.Platform); ok && elm.Id() == pid {
  39. if err := elm.Init(bid); err != nil {
  40. return nil, err
  41. }
  42. return elm, nil
  43. }
  44. }
  45. return nil, errors.New("platform not found")
  46. }