config.go 848 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package presence
  2. import (
  3. "net"
  4. "os"
  5. "time"
  6. "gopkg.in/yaml.v3"
  7. )
  8. type (
  9. MACAddress struct {
  10. net.HardwareAddr
  11. }
  12. Config struct {
  13. Interval time.Duration `yaml:"interval"`
  14. MACAddresses []MACAddress `yaml:"mac_addresses"`
  15. PingCount uint `yaml:"ping_count"`
  16. }
  17. )
  18. func ParseConfig(name string) (*Config, error) {
  19. f, err := os.Open(name)
  20. if err != nil {
  21. return nil, err
  22. }
  23. defer f.Close()
  24. d := yaml.NewDecoder(f)
  25. d.KnownFields(true)
  26. c := &Config{}
  27. err = d.Decode(c)
  28. if err != nil {
  29. return nil, err
  30. }
  31. if c.Interval == 0 {
  32. c.Interval = 30 * time.Second
  33. }
  34. if c.PingCount == 0 {
  35. c.PingCount = 1
  36. }
  37. return c, nil
  38. }
  39. func (a *MACAddress) UnmarshalYAML(value *yaml.Node) (err error) {
  40. var s string
  41. err = value.Decode(&s)
  42. if err != nil {
  43. return
  44. }
  45. a.HardwareAddr, err = net.ParseMAC(s)
  46. return
  47. }