github.com/xmplusdev/xmcore@v1.8.11-0.20240412132628-5518b55526af/main/yaml/yaml.go (about)

     1  package yaml
     2  
     3  import (
     4  	"io"
     5  
     6  	"github.com/xmplusdev/xmcore/common"
     7  	"github.com/xmplusdev/xmcore/common/cmdarg"
     8  	"github.com/xmplusdev/xmcore/core"
     9  	"github.com/xmplusdev/xmcore/infra/conf"
    10  	"github.com/xmplusdev/xmcore/infra/conf/serial"
    11  	"github.com/xmplusdev/xmcore/main/confloader"
    12  )
    13  
    14  func init() {
    15  	common.Must(core.RegisterConfigLoader(&core.ConfigFormat{
    16  		Name:      "YAML",
    17  		Extension: []string{"yaml", "yml"},
    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.DecodeYAMLConfig(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.LoadYAMLConfig(v)
    43  			default:
    44  				return nil, newError("unknow type")
    45  			}
    46  		},
    47  	}))
    48  }