Add sub-command "-dump" to "run". (#2854)

* Add MarshalToJson().

* Add cmd arg -dump for printing out merged multiple json configs.

---------

Co-authored-by: nobody <nobody@nowhere.mars>
This commit is contained in:
nobody 2023-12-30 00:16:48 +08:00 committed by GitHub
parent 006cf491e5
commit 44bb83033f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 476 additions and 10 deletions

View file

@ -2,6 +2,7 @@ package core
import (
"io"
"slices"
"strings"
"github.com/xtls/xray-core/common"
@ -24,10 +25,14 @@ type ConfigLoader func(input interface{}) (*Config, error)
// ConfigBuilder is a builder to build core.Config from filenames and formats
type ConfigBuilder func(files []string, formats []string) (*Config, error)
// ConfigMerger merge multiple json configs into on config
type ConfigsMerger func(files []string, formats []string) (string, error)
var (
configLoaderByName = make(map[string]*ConfigFormat)
configLoaderByExt = make(map[string]*ConfigFormat)
ConfigBuilderForFiles ConfigBuilder
ConfigMergedFormFiles ConfigsMerger
)
// RegisterConfigLoader add a new ConfigLoader.
@ -49,6 +54,20 @@ func RegisterConfigLoader(format *ConfigFormat) error {
return nil
}
func GetMergedConfig(args cmdarg.Arg) (string, error) {
files := make([]string, 0)
formats := make([]string, 0)
supported := []string{"json", "yaml", "toml"}
for _, file := range args {
format := getFormat(file)
if slices.Contains(supported, format) {
files = append(files, file)
formats = append(formats, format)
}
}
return ConfigMergedFormFiles(files, formats)
}
func GetFormatByExtension(ext string) string {
switch strings.ToLower(ext) {
case "pb", "protobuf":