config.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. DefaultUser string `yaml:"default-user"`
  14. DefaultPassword string `yaml:"default-password"`
  15. DefaultCommand string `yaml:"default-command"`
  16. Subscribe []struct {
  17. Topic string `yaml:"topic"`
  18. User string `yaml:"user"`
  19. Password string `yaml:"password"`
  20. Command string `yaml:"command"`
  21. If map[string]string `yaml:"if"`
  22. } `yaml:"subscribe"`
  23. }
  24. // NewConfig creates a new Config struct for a Client
  25. func NewConfig() *Config {
  26. return &Config{
  27. DefaultHost: DefaultBaseURL,
  28. DefaultUser: "",
  29. DefaultPassword: "",
  30. DefaultCommand: "",
  31. Subscribe: nil,
  32. }
  33. }
  34. // LoadConfig loads the Client config from a yaml file
  35. func LoadConfig(filename string) (*Config, error) {
  36. b, err := os.ReadFile(filename)
  37. if err != nil {
  38. return nil, err
  39. }
  40. c := NewConfig()
  41. if err := yaml.Unmarshal(b, c); err != nil {
  42. return nil, err
  43. }
  44. return c, nil
  45. }