config.go 981 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package client
  2. import (
  3. "gopkg.in/yaml.v2"
  4. "os"
  5. )
  6. const (
  7. // DefaultBaseURL is the base URL used to expand short topic names
  8. DefaultBaseURL = "https://ntfy.sh"
  9. )
  10. // Config is the config struct for a Client
  11. type Config struct {
  12. DefaultHost string `yaml:"default-host"`
  13. Subscribe []struct {
  14. Topic string `yaml:"topic"`
  15. User string `yaml:"user"`
  16. Password string `yaml:"password"`
  17. Command string `yaml:"command"`
  18. If map[string]string `yaml:"if"`
  19. } `yaml:"subscribe"`
  20. }
  21. // NewConfig creates a new Config struct for a Client
  22. func NewConfig() *Config {
  23. return &Config{
  24. DefaultHost: DefaultBaseURL,
  25. Subscribe: nil,
  26. }
  27. }
  28. // LoadConfig loads the Client config from a yaml file
  29. func LoadConfig(filename string) (*Config, error) {
  30. b, err := os.ReadFile(filename)
  31. if err != nil {
  32. return nil, err
  33. }
  34. c := NewConfig()
  35. if err := yaml.Unmarshal(b, c); err != nil {
  36. return nil, err
  37. }
  38. return c, nil
  39. }