From 77d7ef97148088a1fe07d72a768f94ee67ff1100 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=8D=E5=B8=85=E4=BD=A0=E6=8A=A5=E8=AD=A6?= Date: Sat, 30 Oct 2021 14:53:16 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=B7=A5=E4=BD=9C=E7=9B=AE?= =?UTF-8?q?=E5=BD=95=E9=85=8D=E7=BD=AE=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 2 +- pkg/conf/conf.go | 1 + pkg/util/path.go | 18 +++++++++++++++--- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index d71e587..f609018 100644 --- a/main.go +++ b/main.go @@ -16,7 +16,7 @@ var ( ) func init() { - flag.StringVar(&confPath, "c", util.RelativePath("conf.ini"), "配置文件路径") + flag.StringVar(&confPath, "c", util.RelativeExecutablePath("conf.ini"), "配置文件路径") flag.BoolVar(&isEject, "eject", false, "导出内置静态资源") flag.StringVar(&scriptName, "database-script", "", "运行内置数据库助手脚本") flag.Parse() diff --git a/pkg/conf/conf.go b/pkg/conf/conf.go index ceeb597..6ade75f 100644 --- a/pkg/conf/conf.go +++ b/pkg/conf/conf.go @@ -26,6 +26,7 @@ type system struct { Debug bool SessionSecret string HashIDSalt string + DataPath string } type ssl struct { diff --git a/pkg/util/path.go b/pkg/util/path.go index 2451603..60ba0a3 100644 --- a/pkg/util/path.go +++ b/pkg/util/path.go @@ -48,11 +48,23 @@ func FormSlash(old string) string { return path.Clean(strings.ReplaceAll(old, "\\", "/")) } -// RelativePath 获取相对工作目录的路径 +// RelativeExecutablePath 获取相对可执行文件的路径 +func RelativeExecutablePath(name string) string { + if filepath.IsAbs(name) { + return name + } + e, _ := os.Executable() + return filepath.Join(filepath.Dir(e), name) +} + +//RelativePath 获取相对路径 func RelativePath(name string) string { if filepath.IsAbs(name) { return name } - e, _ := os.Getwd() - return filepath.Join(e, name) + if conf.SystemConfig.DataPath != "" { + return filepath.Join(conf.SystemConfig.DataPath, name) + } else { + return RelativeExecutablePath(name) + } }