mirror of
https://github.com/v2fly/v2ray-core.git
synced 2025-01-10 11:27:26 -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
91 lines
1.9 KiB
Go
91 lines
1.9 KiB
Go
package commands
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
|
|
"v2ray.com/core"
|
|
"v2ray.com/core/commands/base"
|
|
)
|
|
|
|
// CmdTest tests config files
|
|
var CmdTest = &base.Command{
|
|
CustomFlags: true,
|
|
UsageLine: "{{.Exec}} test [-format=json] [-c config.json] [-d dir]",
|
|
Short: "Test config files",
|
|
Long: `
|
|
Test config files, without launching V2Ray server.
|
|
|
|
Arguments:
|
|
|
|
-c, -config
|
|
Config file for V2Ray. Multiple assign is accepted.
|
|
|
|
-d, -confdir
|
|
A dir with config files. Multiple assign is accepted.
|
|
|
|
-r
|
|
Load confdir recursively.
|
|
|
|
-format
|
|
Format of input files. (default "json")
|
|
|
|
Examples:
|
|
|
|
{{.Exec}} {{.LongName}} -c config.json
|
|
{{.Exec}} {{.LongName}} -d path/to/dir
|
|
|
|
Use "{{.Exec}} help format-loader" for more information about format.
|
|
`,
|
|
Run: executeTest,
|
|
}
|
|
|
|
func executeTest(cmd *base.Command, args []string) {
|
|
setConfigFlags(cmd)
|
|
cmd.Flag.Parse(args)
|
|
|
|
extension, err := getLoaderExtension()
|
|
if err != nil {
|
|
base.Fatalf(err.Error())
|
|
}
|
|
|
|
if len(configDirs) > 0 {
|
|
dirReader := readConfDir
|
|
if *configDirRecursively {
|
|
dirReader = readConfDirRecursively
|
|
}
|
|
for _, d := range configDirs {
|
|
log.Println("Using confdir from arg:", d)
|
|
configFiles = append(configFiles, dirReader(d, extension)...)
|
|
}
|
|
}
|
|
if len(configFiles) == 0 {
|
|
if len(configDirs) == 0 {
|
|
cmd.Flag.Usage()
|
|
base.SetExitStatus(1)
|
|
base.Exit()
|
|
}
|
|
base.Fatalf("no config file found with extension: %s", extension)
|
|
}
|
|
printVersion()
|
|
_, err = startV2RayTesting()
|
|
if err != nil {
|
|
base.Fatalf("Test failed: %s", err)
|
|
}
|
|
fmt.Println("Configuration OK.")
|
|
}
|
|
|
|
func startV2RayTesting() (core.Server, error) {
|
|
config, err := core.LoadConfig(*configFormat, configFiles[0], configFiles)
|
|
if err != nil {
|
|
return nil, newError("failed to read config files: [", configFiles.String(), "]").Base(err)
|
|
}
|
|
|
|
server, err := core.New(config)
|
|
if err != nil {
|
|
return nil, newError("failed to create server").Base(err)
|
|
}
|
|
|
|
return server, nil
|
|
}
|