mirror of
https://github.com/XTLS/Xray-core.git
synced 2025-04-30 09:18:34 +00:00
Add YAML Support (#86)
This commit is contained in:
parent
f073456ac0
commit
85619b5a29
7 changed files with 96 additions and 0 deletions
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue