|
|
@ -19,14 +19,19 @@ type ConfigFormat struct {
|
|
|
|
Loader ConfigLoader
|
|
|
|
Loader ConfigLoader
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type ConfigSource struct {
|
|
|
|
|
|
|
|
Name string
|
|
|
|
|
|
|
|
Format string
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ConfigLoader is a utility to load Xray config from external source.
|
|
|
|
// ConfigLoader is a utility to load Xray config from external source.
|
|
|
|
type ConfigLoader func(input interface{}) (*Config, error)
|
|
|
|
type ConfigLoader func(input interface{}) (*Config, error)
|
|
|
|
|
|
|
|
|
|
|
|
// ConfigBuilder is a builder to build core.Config from filenames and formats
|
|
|
|
// ConfigBuilder is a builder to build core.Config from filenames and formats
|
|
|
|
type ConfigBuilder func(files []string, formats []string) (*Config, error)
|
|
|
|
type ConfigBuilder func(files []*ConfigSource) (*Config, error)
|
|
|
|
|
|
|
|
|
|
|
|
// ConfigsMerger merge multiple json configs into on config
|
|
|
|
// ConfigsMerger merge multiple json configs into on config
|
|
|
|
type ConfigsMerger func(files []string, formats []string) (string, error)
|
|
|
|
type ConfigsMerger func(files []*ConfigSource) (string, error)
|
|
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
var (
|
|
|
|
configLoaderByName = make(map[string]*ConfigFormat)
|
|
|
|
configLoaderByName = make(map[string]*ConfigFormat)
|
|
|
@ -55,20 +60,21 @@ func RegisterConfigLoader(format *ConfigFormat) error {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func GetMergedConfig(args cmdarg.Arg) (string, error) {
|
|
|
|
func GetMergedConfig(args cmdarg.Arg) (string, error) {
|
|
|
|
files := make([]string, 0)
|
|
|
|
var files []*ConfigSource
|
|
|
|
formats := make([]string, 0)
|
|
|
|
|
|
|
|
supported := []string{"json", "yaml", "toml"}
|
|
|
|
supported := []string{"json", "yaml", "toml"}
|
|
|
|
for _, file := range args {
|
|
|
|
for _, file := range args {
|
|
|
|
format := getFormat(file)
|
|
|
|
format := getFormat(file)
|
|
|
|
for _, s := range supported {
|
|
|
|
for _, s := range supported {
|
|
|
|
if s == format {
|
|
|
|
if s == format {
|
|
|
|
files = append(files, file)
|
|
|
|
files = append(files, &ConfigSource{
|
|
|
|
formats = append(formats, format)
|
|
|
|
Name: file,
|
|
|
|
|
|
|
|
Format: format,
|
|
|
|
|
|
|
|
})
|
|
|
|
break
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ConfigMergedFormFiles(files, formats)
|
|
|
|
return ConfigMergedFormFiles(files)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func GetFormatByExtension(ext string) string {
|
|
|
|
func GetFormatByExtension(ext string) string {
|
|
|
@ -101,7 +107,7 @@ func getFormat(filename string) string {
|
|
|
|
func LoadConfig(formatName string, input interface{}) (*Config, error) {
|
|
|
|
func LoadConfig(formatName string, input interface{}) (*Config, error) {
|
|
|
|
switch v := input.(type) {
|
|
|
|
switch v := input.(type) {
|
|
|
|
case cmdarg.Arg:
|
|
|
|
case cmdarg.Arg:
|
|
|
|
formats := make([]string, len(v))
|
|
|
|
files := make([]*ConfigSource, len(v))
|
|
|
|
hasProtobuf := false
|
|
|
|
hasProtobuf := false
|
|
|
|
for i, file := range v {
|
|
|
|
for i, file := range v {
|
|
|
|
var f string
|
|
|
|
var f string
|
|
|
@ -123,7 +129,10 @@ func LoadConfig(formatName string, input interface{}) (*Config, error) {
|
|
|
|
if f == "protobuf" {
|
|
|
|
if f == "protobuf" {
|
|
|
|
hasProtobuf = true
|
|
|
|
hasProtobuf = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
formats[i] = f
|
|
|
|
files[i] = &ConfigSource{
|
|
|
|
|
|
|
|
Name: file,
|
|
|
|
|
|
|
|
Format: f,
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// only one protobuf config file is allowed
|
|
|
|
// only one protobuf config file is allowed
|
|
|
@ -136,8 +145,7 @@ func LoadConfig(formatName string, input interface{}) (*Config, error) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// to avoid import cycle
|
|
|
|
// to avoid import cycle
|
|
|
|
return ConfigBuilderForFiles(v, formats)
|
|
|
|
return ConfigBuilderForFiles(files)
|
|
|
|
|
|
|
|
|
|
|
|
case io.Reader:
|
|
|
|
case io.Reader:
|
|
|
|
if f, found := configLoaderByName[formatName]; found {
|
|
|
|
if f, found := configLoaderByName[formatName]; found {
|
|
|
|
return f.Loader(v)
|
|
|
|
return f.Loader(v)
|
|
|
|