From 0062ad914e2f6e75de90cb3c77f5378a413ae38b Mon Sep 17 00:00:00 2001 From: zu1k Date: Wed, 5 Oct 2022 22:16:55 +0800 Subject: [PATCH] change default dataDir and configDir https://github.com/adrg/xdg Signed-off-by: zu1k --- README.md | 8 ++++--- README_en.md | 8 ++++--- go.mod | 1 + go.sum | 3 +++ internal/constant/path.go | 44 +++++++++++++-------------------------- 5 files changed, 28 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index 0951c9a..f3b996f 100644 --- a/README.md +++ b/README.md @@ -295,11 +295,13 @@ export NALI_DB_IP4=ipip 1.1.1.1 [Australia] ``` -### 更换工作目录 +### 工作目录 -如果未指定数据库存放目录,数据库默认将存放在 `~/.nali` +设置环境变量 `NALI_HOME` 来指定工作目录,配置文件和数据库存放在工作目录下。也可在配置文件中使用绝对路径指定其他数据库路径。 -设置环境变量 `NALI_HOME` 来指定工作目录,数据库存放在工作目录下。也可在配置文件中使用绝对路径指定其他数据库路径。 +设置环境变量 `NALI_CONFIG_HOME` 来指定配置文件目录,`NALI_DB_HOME` 来执行数据库文件目录 + +如果未指定相关环境变量,将使用 XDG 规范,配置文件目录在 `$XDG_CONFIG_HOME/nali`,数据库文件目录在 `$XDG_DATA_HOME/nali` ``` set NALI_HOME=D:\nali diff --git a/README_en.md b/README_en.md index c3c44f4..6f86571 100644 --- a/README_en.md +++ b/README_en.md @@ -288,11 +288,13 @@ The values that can be set for this parameter can be found in the list of suppor 1.1.1.1 [Australia] ``` -### Change database directory +### Change directory -If the database directory is not specified, the database will be placed in `~/.nali` +Set the environment variable `NALI_HOME` to specify the working directory where the configuration file and database are stored. You can also use absolute paths in the configuration file to specify other database paths. -Set environment variables `NALI_HOME` to specify the working directory +Set the environment variable `NALI_CONFIG_HOME` to specify the configuration file directory and `NALI_DB_HOME` to specify the database file directory. + +If no environment variable is specified, the XDG specification will be used, with the configuration file directory in `$XDG_CONFIG_HOME/nali` and the database file directory in `$XDG_DATA_HOME/nali`. ``` set NALI_HOME=D:\nalidb diff --git a/go.mod b/go.mod index 34aa5f2..03af161 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/zu1k/nali go 1.18 require ( + github.com/adrg/xdg v0.4.0 github.com/fatih/color v1.13.0 github.com/ip2location/ip2location-go/v9 v9.4.1 github.com/ipipdotnet/ipdb-go v1.3.3 diff --git a/go.sum b/go.sum index 50ef510..cbb3d57 100644 --- a/go.sum +++ b/go.sum @@ -38,6 +38,8 @@ cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3f dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= +github.com/adrg/xdg v0.4.0 h1:RzRqFcjH4nE5C6oTAxhBtoE2IRyjBSa62SCbyPidvls= +github.com/adrg/xdg v0.4.0/go.mod h1:N6ag73EX4wyxeaoeHctc1mas01KZgsj5tYiAIwqJE/E= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI= @@ -353,6 +355,7 @@ golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220804214406-8e32c043e418 h1:9vYwv7OjYaky/tlAeD7C4oC9EsPTlaFl1H2jS++V+ME= golang.org/x/sys v0.0.0-20220804214406-8e32c043e418/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/internal/constant/path.go b/internal/constant/path.go index b60d085..bc8eca6 100644 --- a/internal/constant/path.go +++ b/internal/constant/path.go @@ -4,6 +4,8 @@ import ( "log" "os" "path/filepath" + + "github.com/adrg/xdg" ) var ( @@ -16,50 +18,32 @@ func init() { ConfigDirPath = naliHome DataDirPath = naliHome } else { - if dir, got := getFirstValidEnv("NALI_CONFIG_HOME", "XDG_CONFIG_HOME"); got { - ConfigDirPath = dir - } else { - homeDir, err := os.UserHomeDir() - if err != nil { - log.Fatal("get user's home dir failed!") - } - ConfigDirPath = filepath.Join(homeDir, ".nali") + ConfigDirPath = os.Getenv("NALI_CONFIG_HOME") + if len(ConfigDirPath) == 0 { + ConfigDirPath = filepath.Join(xdg.ConfigHome, "nali") } - if dir, got := getFirstValidEnv("NALI_DB_HOME", "XDG_DATA_HOME"); got { - DataDirPath = dir - } else { - homeDir, err := os.UserHomeDir() - if err != nil { - log.Fatal("get user's home dir failed!") - } - DataDirPath = filepath.Join(homeDir, ".nali") + DataDirPath = os.Getenv("NALI_DB_HOME") + if len(DataDirPath) == 0 { + DataDirPath = filepath.Join(xdg.DataHome, "nali") } } prepareDir(ConfigDirPath) prepareDir(DataDirPath) - os.Chdir(DataDirPath) -} - -func getFirstValidEnv(keys ...string) (string, bool) { - for _, key := range keys { - if value := os.Getenv(key); len(value) > 0 { - return value, true - } - } - return "", false + _ = os.Chdir(DataDirPath) } func prepareDir(dir string) { stat, err := os.Stat(dir) - if os.IsNotExist(err) { + if err != nil && os.IsNotExist(err) { if err := os.MkdirAll(dir, 0755); err != nil { log.Fatal("can not create config dir:", dir) } - } - if !stat.IsDir() { - log.Fatal("path already exists, but not a dir:", dir) + } else { + if !stat.IsDir() { + log.Fatal("path already exists, but not a dir:", dir) + } } }