github.com/EagleQL/Xray-core@v1.4.3/infra/conf/serial/builder.go (about)

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