mirror of
https://github.com/v2fly/v2ray-core.git
synced 2025-01-05 00:47:51 -05:00
ff59bd37ce
* scalable commands column * new multi-json loader For both internal & external json loader This commit also: * applies -confdir to other formats, e.g. "yaml" in the future * multiple assign of -confdir is accepted * add flag to load confdir recursively * config loader can have alias name * json loader also accepts .jsonc * add merge command * add help topics for json merge, format loader * format loaders don't panic * apply lint style * add merge test * merge same tag in array, solve v2fly/discussion#97 * apply lint style * merge code optimize * fix merge cmdarg.Arg * update cmd description * improve merge logic * fix zero value overwrite * fix "null" lost after array merge * code optimize * fix merged slices not sorted * code optimize * add package doc * fix a typo
72 lines
1.6 KiB
Go
72 lines
1.6 KiB
Go
package all
|
|
|
|
import (
|
|
"bytes"
|
|
"os"
|
|
|
|
"google.golang.org/protobuf/proto"
|
|
"v2ray.com/core/commands/base"
|
|
"v2ray.com/core/infra/conf/merge"
|
|
"v2ray.com/core/infra/conf/serial"
|
|
)
|
|
|
|
var cmdConvert = &base.Command{
|
|
UsageLine: "{{.Exec}} convert [-r] [c1.json] [<url>.json] [dir1] ...",
|
|
Short: "Convert multiple json config to protobuf",
|
|
Long: `
|
|
Convert JSON config to protobuf.
|
|
|
|
If multiple JSON files or folders specified, it merges them first, then convert.
|
|
|
|
Arguments:
|
|
|
|
-r
|
|
Load confdir recursively.
|
|
|
|
Examples:
|
|
|
|
{{.Exec}} {{.LongName}} config.json
|
|
{{.Exec}} {{.LongName}} c1.json c2.json
|
|
{{.Exec}} {{.LongName}} c1.json https://url.to/c2.json
|
|
{{.Exec}} {{.LongName}} "path/to/json_dir"
|
|
`,
|
|
}
|
|
|
|
func init() {
|
|
cmdConvert.Run = executeConvert // break init loop
|
|
}
|
|
|
|
var convertReadDirRecursively = cmdConvert.Flag.Bool("r", false, "")
|
|
|
|
func executeConvert(cmd *base.Command, args []string) {
|
|
unnamed := cmd.Flag.Args()
|
|
files := resolveFolderToFiles(unnamed, *convertReadDirRecursively)
|
|
if len(files) == 0 {
|
|
base.Fatalf("empty config list")
|
|
}
|
|
|
|
data, err := merge.FilesToJSON(files)
|
|
if err != nil {
|
|
base.Fatalf("failed to load json: %s", err)
|
|
}
|
|
r := bytes.NewReader(data)
|
|
cf, err := serial.DecodeJSONConfig(r)
|
|
if err != nil {
|
|
base.Fatalf("failed to decode json: %s", err)
|
|
}
|
|
|
|
pbConfig, err := cf.Build()
|
|
if err != nil {
|
|
base.Fatalf(err.Error())
|
|
}
|
|
|
|
bytesConfig, err := proto.Marshal(pbConfig)
|
|
if err != nil {
|
|
base.Fatalf("failed to marshal proto config: %s", err)
|
|
}
|
|
|
|
if _, err := os.Stdout.Write(bytesConfig); err != nil {
|
|
base.Fatalf("failed to write proto config: %s", err)
|
|
}
|
|
}
|