config.go 888 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. Command string `yaml:"command"`
  16. If map[string]string `yaml:"if"`
  17. } `yaml:"subscribe"`
  18. }
  19. // NewConfig creates a new Config struct for a Client
  20. func NewConfig() *Config {
  21. return &Config{
  22. DefaultHost: DefaultBaseURL,
  23. Subscribe: nil,
  24. }
  25. }
  26. // LoadConfig loads the Client config from a yaml file
  27. func LoadConfig(filename string) (*Config, error) {
  28. b, err := os.ReadFile(filename)
  29. if err != nil {
  30. return nil, err
  31. }
  32. c := NewConfig()
  33. if err := yaml.Unmarshal(b, c); err != nil {
  34. return nil, err
  35. }
  36. return c, nil
  37. }