config.go 1.5 KB

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