github.com/xraypb/Xray-core@v1.8.1/main/toml/toml.go (about)

     1  package toml
     2  
     3  import (
     4  	"io"
     5  
     6  	"github.com/xraypb/Xray-core/common"
     7  	"github.com/xraypb/Xray-core/common/cmdarg"
     8  	"github.com/xraypb/Xray-core/core"
     9  	"github.com/xraypb/Xray-core/infra/conf"
    10  	"github.com/xraypb/Xray-core/infra/conf/serial"
    11  	"github.com/xraypb/Xray-core/main/confloader"
    12  )
    13  
    14  func init() {
    15  	common.Must(core.RegisterConfigLoader(&core.ConfigFormat{
    16  		Name:      "TOML",
    17  		Extension: []string{"toml"},
    18  		Loader: func(input interface{}) (*core.Config, error) {
    19  			switch v := input.(type) {
    20  			case cmdarg.Arg:
    21  				cf := &conf.Config{}
    22  				for i, arg := range v {
    23  					newError("Reading config: ", arg).AtInfo().WriteToLog()
    24  					r, err := confloader.LoadConfig(arg)
    25  					if err != nil {
    26  						return nil, newError("failed to read config: ", arg).Base(err)
    27  					}
    28  					c, err := serial.DecodeTOMLConfig(r)
    29  					if err != nil {
    30  						return nil, newError("failed to decode config: ", arg).Base(err)
    31  					}
    32  					if i == 0 {
    33  						// This ensure even if the muti-json parser do not support a setting,
    34  						// It is still respected automatically for the first configure file
    35  						*cf = *c
    36  						continue
    37  					}
    38  					cf.Override(c, arg)
    39  				}
    40  				return cf.Build()
    41  			case io.Reader:
    42  				return serial.LoadTOMLConfig(v)
    43  			default:
    44  				return nil, newError("unknow type")
    45  			}
    46  		},
    47  	}))
    48  }