From 9706a446cd51cd455254dfad714dce2437240c62 Mon Sep 17 00:00:00 2001 From: five9th Date: Fri, 28 Feb 2025 19:54:58 +0300 Subject: [PATCH] default statsfile fix --- main/run.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/main/run.go b/main/run.go index 57dae605..6ac213c6 100644 --- a/main/run.go +++ b/main/run.go @@ -55,7 +55,7 @@ var ( test = cmdRun.Flag.Bool("test", false, "Test config file only, without launching Xray server.") format = cmdRun.Flag.String("format", "auto", "Format of input file.") restoreStats = cmdRun.Flag.Bool("restore-stats", false, "Restore stats from previous session.") - statsFile = cmdRun.Flag.String("statsfile", "xray_stats.json", "Path to the JSON file the stats will be restored from.") + statsFile = cmdRun.Flag.String("statsfile", FileFromExeDir("xray_stats.json"), "Path to the JSON file the stats will be restored from.") /* We have to do this here because Golang's Test will also need to parse flag, before * main func in this file is run. @@ -142,6 +142,19 @@ func dirExists(file string) bool { return err == nil && info.IsDir() } +// FileFromExeDir returns the absolute path of a file located in the Executable directory. +func FileFromExeDir(fileName string) string { + exePath, err := os.Executable() + if err != nil { + msg := fmt.Sprintf("error getting executable path: %v", err) + fmt.Println(msg) + + return fileName + } + exeDir := filepath.Dir(exePath) + return filepath.Join(exeDir, fileName) +} + func getRegepxByFormat() string { switch strings.ToLower(*format) { case "json":