mirror of
https://github.com/XTLS/Xray-core.git
synced 2025-06-12 11:29:36 +03:00
Add YAML Support (#86)
This commit is contained in:
@ -7,6 +7,8 @@ import (
|
||||
"io/ioutil"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
"github.com/ghodss/yaml"
|
||||
|
||||
"github.com/xtls/xray-core/common/errors"
|
||||
"github.com/xtls/xray-core/core"
|
||||
"github.com/xtls/xray-core/infra/conf"
|
||||
@ -117,3 +119,35 @@ func LoadTOMLConfig(reader io.Reader) (*core.Config, error) {
|
||||
|
||||
return pbConfig, nil
|
||||
}
|
||||
|
||||
// DecodeYAMLConfig reads from reader and decode the config into *conf.Config
|
||||
// using github.com/ghodss/yaml to convert yaml to json
|
||||
// syntax error could be detected.
|
||||
func DecodeYAMLConfig(reader io.Reader) (*conf.Config, error) {
|
||||
|
||||
yamlFile, err := ioutil.ReadAll(reader)
|
||||
if err != nil {
|
||||
return nil, newError("failed to read config file").Base(err)
|
||||
}
|
||||
|
||||
jsonFile, err := yaml.YAMLToJSON(yamlFile)
|
||||
if err != nil {
|
||||
return nil, newError("failed to read config file").Base(err)
|
||||
}
|
||||
|
||||
return DecodeJSONConfig(bytes.NewReader(jsonFile))
|
||||
}
|
||||
|
||||
func LoadYAMLConfig(reader io.Reader) (*core.Config, error) {
|
||||
yamlConfig, err := DecodeYAMLConfig(reader)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
pbConfig, err := yamlConfig.Build()
|
||||
if err != nil {
|
||||
return nil, newError("failed to parse yaml config").Base(err)
|
||||
}
|
||||
|
||||
return pbConfig, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user