test.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package commands
  2. import (
  3. "fmt"
  4. "log"
  5. core "github.com/v2fly/v2ray-core/v4"
  6. "github.com/v2fly/v2ray-core/v4/main/commands/base"
  7. )
  8. // CmdTest tests config files
  9. var CmdTest = &base.Command{
  10. CustomFlags: true,
  11. UsageLine: "{{.Exec}} test [-format=json] [-c config.json] [-d dir]",
  12. Short: "test config files",
  13. Long: `
  14. Test config files, without launching V2Ray server.
  15. {{.Exec}} will also use the config directory specified by environment
  16. variable "v2ray.location.confdir". If no config found, it tries
  17. to load config from one of below:
  18. 1. The default "config.json" in the current directory
  19. 2. The config file from ENV "v2ray.location.config"
  20. 3. The stdin if all failed above
  21. Arguments:
  22. -c, -config <file>
  23. Config file for V2Ray. Multiple assign is accepted.
  24. -d, -confdir <dir>
  25. A directory with config files. Multiple assign is accepted.
  26. -r
  27. Load confdir recursively.
  28. -format <format>
  29. Format of config input. (default "auto")
  30. Examples:
  31. {{.Exec}} {{.LongName}} -c config.json
  32. {{.Exec}} {{.LongName}} -d path/to/dir
  33. Use "{{.Exec}} help format-loader" for more information about format.
  34. `,
  35. Run: executeTest,
  36. }
  37. func executeTest(cmd *base.Command, args []string) {
  38. setConfigFlags(cmd)
  39. cmd.Flag.Parse(args)
  40. extension, err := core.GetLoaderExtensions(*configFormat)
  41. if err != nil {
  42. base.Fatalf(err.Error())
  43. }
  44. if len(configDirs) > 0 {
  45. dirReader := readConfDir
  46. if *configDirRecursively {
  47. dirReader = readConfDirRecursively
  48. }
  49. for _, d := range configDirs {
  50. log.Println("Using confdir from arg:", d)
  51. configFiles = append(configFiles, dirReader(d, extension)...)
  52. }
  53. }
  54. printVersion()
  55. _, err = startV2Ray()
  56. if err != nil {
  57. base.Fatalf("Test failed: %s", err)
  58. }
  59. fmt.Println("Configuration OK.")
  60. }