github.com/moqsien/xraycore@v1.8.5/infra/conf/serial/builder.go (about)

     1  package serial
     2  
     3  import (
     4  	"io"
     5  
     6  	"github.com/moqsien/xraycore/core"
     7  	"github.com/moqsien/xraycore/infra/conf"
     8  	"github.com/moqsien/xraycore/main/confloader"
     9  )
    10  
    11  func BuildConfig(files []string, formats []string) (*core.Config, error) {
    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 ReaderDecoderByFormat = make(map[string]readerDecoder)
    35  
    36  func init() {
    37  	ReaderDecoderByFormat["json"] = DecodeJSONConfig
    38  	ReaderDecoderByFormat["yaml"] = DecodeYAMLConfig
    39  	ReaderDecoderByFormat["toml"] = DecodeTOMLConfig
    40  
    41  	core.ConfigBuilderForFiles = BuildConfig
    42  }