2
0

config.go 1.1 KB

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