Fix: format flag not working (#410)

This commit is contained in:
秋のかえで 2021-03-21 17:13:51 +08:00 committed by GitHub
parent 4e63c22197
commit b0e7ad9663
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 5 deletions

View File

@ -84,10 +84,19 @@ func LoadConfig(formatName string, input interface{}) (*Config, error) {
formats := make([]string, len(v)) formats := make([]string, len(v))
hasProtobuf := false hasProtobuf := false
for i, file := range v { for i, file := range v {
f := getFormat(file) var f string
if formatName == "auto" {
f = getFormat(file)
if f == "" {
return nil, newError("Unable to get format of", formatName).AtWarning()
}
}
if f == "" { if f == "" {
f = formatName f = formatName
} }
if f == "protobuf" { if f == "protobuf" {
hasProtobuf = true hasProtobuf = true
} }

View File

@ -11,6 +11,7 @@ import (
"regexp" "regexp"
"runtime" "runtime"
"runtime/debug" "runtime/debug"
"strings"
"syscall" "syscall"
"github.com/xtls/xray-core/common/cmdarg" "github.com/xtls/xray-core/common/cmdarg"
@ -31,7 +32,7 @@ Xray. Multiple assign is accepted.
The -confdir=dir flag sets a dir with multiple json config The -confdir=dir flag sets a dir with multiple json config
The -format=json flag sets the format of config files. The -format=json flag sets the format of config files.
Default "json". Default "auto".
The -test flag tells Xray to test config files only, The -test flag tells Xray to test config files only,
without launching the server without launching the server
@ -46,7 +47,7 @@ var (
configFiles cmdarg.Arg // "Config file for Xray.", the option is customed type, parse in main configFiles cmdarg.Arg // "Config file for Xray.", the option is customed type, parse in main
configDir string configDir string
test = cmdRun.Flag.Bool("test", false, "Test config file only, without launching Xray server.") test = cmdRun.Flag.Bool("test", false, "Test config file only, without launching Xray server.")
format = cmdRun.Flag.String("format", "json", "Format of input file.") format = cmdRun.Flag.String("format", "auto", "Format of input file.")
/* We have to do this here because Golang's Test will also need to parse flag, before /* We have to do this here because Golang's Test will also need to parse flag, before
* main func in this file is run. * main func in this file is run.
@ -111,13 +112,26 @@ func dirExists(file string) bool {
return err == nil && info.IsDir() return err == nil && info.IsDir()
} }
func getRegepxByFormat() string {
switch strings.ToLower(*format) {
case "json":
return `^.+\.json$`
case "toml":
return `^.+\.toml$`
case "yaml", "yml":
return `^.+\.(yaml|yml)$`
default:
return `^.+\.(json|toml|yaml|yml)$`
}
}
func readConfDir(dirPath string) { func readConfDir(dirPath string) {
confs, err := ioutil.ReadDir(dirPath) confs, err := ioutil.ReadDir(dirPath)
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
for _, f := range confs { for _, f := range confs {
matched, err := regexp.MatchString(`^.+\.(json|toml|yaml|yml)$`, f.Name()) matched, err := regexp.MatchString(getRegepxByFormat(), f.Name())
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
@ -160,7 +174,7 @@ func getConfigFilePath() cmdarg.Arg {
func getConfigFormat() string { func getConfigFormat() string {
f := core.GetFormatByExtension(*format) f := core.GetFormatByExtension(*format)
if f == "" { if f == "" {
f = "json" f = "auto"
} }
return f return f
} }