builder.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package serial
  2. import (
  3. "github.com/xtls/xray-core/core"
  4. "github.com/xtls/xray-core/infra/conf"
  5. "github.com/xtls/xray-core/main/confloader"
  6. "io"
  7. )
  8. func BuildConfig(files []string, formats []string) (*core.Config, error) {
  9. cf := &conf.Config{}
  10. for i, file := range files {
  11. newError("Reading config: ", file).AtInfo().WriteToLog()
  12. r, err := confloader.LoadConfig(file)
  13. if err != nil {
  14. return nil, newError("failed to read config: ", file).Base(err)
  15. }
  16. c, err := ReaderDecoderByFormat[formats[i]](r)
  17. if err != nil {
  18. return nil, newError("failed to decode config: ", file).Base(err)
  19. }
  20. if i == 0 {
  21. *cf = *c
  22. continue
  23. }
  24. cf.Override(c, file)
  25. }
  26. return cf.Build()
  27. }
  28. type readerDecoder func(io.Reader) (*conf.Config, error)
  29. var (
  30. ReaderDecoderByFormat = make(map[string]readerDecoder)
  31. )
  32. func init() {
  33. ReaderDecoderByFormat["json"] = DecodeJSONConfig
  34. ReaderDecoderByFormat["yaml"] = DecodeYAMLConfig
  35. ReaderDecoderByFormat["toml"] = DecodeTOMLConfig
  36. core.ConfigBuilderForFiles = BuildConfig
  37. }