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