|
|
|
@ -9,12 +9,14 @@ import (
|
|
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Config is for top-level struct for config
|
|
|
|
|
type Config struct {
|
|
|
|
|
Telegram TelegramConfig `yaml:":telegram"`
|
|
|
|
|
Xmpp XmppConfig `yaml:":xmpp"`
|
|
|
|
|
XMPP XMPPConfig `yaml:":xmpp"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type XmppConfig struct {
|
|
|
|
|
// XMPPConfig is for :xmpp: subtree
|
|
|
|
|
type XMPPConfig struct {
|
|
|
|
|
Loglevel string `yaml:":loglevel"`
|
|
|
|
|
Jid string `yaml:":jid"`
|
|
|
|
|
Host string `yaml:":host"`
|
|
|
|
@ -23,6 +25,7 @@ type XmppConfig struct {
|
|
|
|
|
Db string `yaml:":db"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TelegramConfig is for :telegram: subtree
|
|
|
|
|
type TelegramConfig struct {
|
|
|
|
|
Loglevel string `yaml:":loglevel"`
|
|
|
|
|
Content TelegramContentConfig `yaml:":content"`
|
|
|
|
@ -30,26 +33,31 @@ type TelegramConfig struct {
|
|
|
|
|
Tdlib TelegramTdlibConfig `yaml:":tdlib"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TelegramContentConfig is for :content: subtree
|
|
|
|
|
type TelegramContentConfig struct {
|
|
|
|
|
Path string `yaml:":path"`
|
|
|
|
|
Link string `yaml:":link"`
|
|
|
|
|
Upload string `yaml:":upload"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TelegramTdlibConfig is for :tdlib: subtree
|
|
|
|
|
type TelegramTdlibConfig struct {
|
|
|
|
|
Path string `yaml:":lib_path"`
|
|
|
|
|
Client TelegramTdlibClientConfig `yaml:":client"`
|
|
|
|
|
Path string `yaml:":lib_path"`
|
|
|
|
|
Logfile string `yaml:":logfile"`
|
|
|
|
|
Client TelegramTdlibClientConfig `yaml:":client"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TelegramTdlibClientConfig is for :client: subtree
|
|
|
|
|
type TelegramTdlibClientConfig struct {
|
|
|
|
|
ApiId string `yaml:":api_id"`
|
|
|
|
|
ApiHash string `yaml:":api_hash"`
|
|
|
|
|
APIID string `yaml:":api_id"`
|
|
|
|
|
APIHash string `yaml:":api_hash"`
|
|
|
|
|
DeviceModel string `yaml:":device_model"`
|
|
|
|
|
ApplicationVersion string `yaml:":application_version"`
|
|
|
|
|
UseChatInfoDatabase bool `yaml:":use_chat_info_database"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func ReadConfig(path string, schema_path string) (Config, error) {
|
|
|
|
|
// ReadConfig reads the specified config file, validates it and returns a struct
|
|
|
|
|
func ReadConfig(path string, schemaPath string) (Config, error) {
|
|
|
|
|
var config Config
|
|
|
|
|
|
|
|
|
|
file, err := ioutil.ReadFile(path)
|
|
|
|
@ -62,7 +70,7 @@ func ReadConfig(path string, schema_path string) (Config, error) {
|
|
|
|
|
return config, errors.Wrap(err, "Error parsing config")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = validateConfig(file, schema_path)
|
|
|
|
|
err = validateConfig(file, schemaPath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return config, errors.Wrap(err, "Validation error")
|
|
|
|
|
}
|
|
|
|
@ -70,25 +78,25 @@ func ReadConfig(path string, schema_path string) (Config, error) {
|
|
|
|
|
return config, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func validateConfig(file []byte, schema_path string) error {
|
|
|
|
|
schema, err := jsonschema.Compile(schema_path)
|
|
|
|
|
func validateConfig(file []byte, schemaPath string) error {
|
|
|
|
|
schema, err := jsonschema.Compile(schemaPath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return errors.Wrap(err, "Corrupted JSON schema")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var config_generic interface{}
|
|
|
|
|
var configGeneric interface{}
|
|
|
|
|
|
|
|
|
|
err = yaml.Unmarshal(file, &config_generic)
|
|
|
|
|
err = yaml.Unmarshal(file, &configGeneric)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return errors.Wrap(err, "Error re-parsing config")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
config_generic, err = convertToStringKeysRecursive(config_generic, "")
|
|
|
|
|
configGeneric, err = convertToStringKeysRecursive(configGeneric, "")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return errors.Wrap(err, "Config conversion error")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = schema.ValidateInterface(config_generic)
|
|
|
|
|
err = schema.ValidateInterface(configGeneric)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return errors.Wrap(err, "Config validation error")
|
|
|
|
|
}
|
|
|
|
|