config.go 1.4 KB

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