config.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. // DefaultConfigFile is the default path to the client config file (set in config_*.go)
  12. var DefaultConfigFile string
  13. // Config is the config struct for a Client
  14. type Config struct {
  15. DefaultHost string `yaml:"default-host"`
  16. DefaultUser string `yaml:"default-user"`
  17. DefaultPassword *string `yaml:"default-password"`
  18. DefaultToken string `yaml:"default-token"`
  19. DefaultCommand string `yaml:"default-command"`
  20. Subscribe []Subscribe `yaml:"subscribe"`
  21. }
  22. // Subscribe is the struct for a Subscription within Config
  23. type Subscribe struct {
  24. Topic string `yaml:"topic"`
  25. User *string `yaml:"user"`
  26. Password *string `yaml:"password"`
  27. Token *string `yaml:"token"`
  28. Command string `yaml:"command"`
  29. If map[string]string `yaml:"if"`
  30. }
  31. // NewConfig creates a new Config struct for a Client
  32. func NewConfig() *Config {
  33. return &Config{
  34. DefaultHost: DefaultBaseURL,
  35. DefaultUser: "",
  36. DefaultPassword: nil,
  37. DefaultToken: "",
  38. DefaultCommand: "",
  39. Subscribe: nil,
  40. }
  41. }
  42. // LoadConfig loads the Client config from a yaml file
  43. func LoadConfig(filename string) (*Config, error) {
  44. log.Debug("Loading client config from %s", filename)
  45. b, err := os.ReadFile(filename)
  46. if err != nil {
  47. return nil, err
  48. }
  49. c := NewConfig()
  50. if err := yaml.Unmarshal(b, c); err != nil {
  51. return nil, err
  52. }
  53. return c, nil
  54. }