From f92d2d7a9a9eb110019e1631d00b520bcd64a548 Mon Sep 17 00:00:00 2001 From: wz Date: Mon, 16 Apr 2018 15:35:02 +0800 Subject: [PATCH] modify data dir --- cmd/bytomd/main.go | 3 ++- config/config.go | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/cmd/bytomd/main.go b/cmd/bytomd/main.go index 618659b8..6792c872 100644 --- a/cmd/bytomd/main.go +++ b/cmd/bytomd/main.go @@ -10,6 +10,7 @@ import ( "github.com/tendermint/tmlibs/cli" "github.com/bytom/cmd/bytomd/commands" + "github.com/bytom/config" ) // ContextHook is a hook for logrus. @@ -51,6 +52,6 @@ func init() { } func main() { - cmd := cli.PrepareBaseCmd(commands.RootCmd, "TM", os.ExpandEnv("./.bytomd")) + cmd := cli.PrepareBaseCmd(commands.RootCmd, "TM", os.ExpandEnv(config.DefaultDataDir())) cmd.Execute() } diff --git a/config/config.go b/config/config.go index f73f7ea9..9f11feb2 100644 --- a/config/config.go +++ b/config/config.go @@ -1,7 +1,10 @@ package config import ( + "os" + "os/user" "path/filepath" + "runtime" "time" ) @@ -181,3 +184,32 @@ func rootify(path, root string) string { } return filepath.Join(root, path) } + +// DefaultDataDir is the default data directory to use for the databases and other +// persistence requirements. +func DefaultDataDir() string { + // Try to place the data folder in the user's home dir + home := homeDir() + dataDir := "./.Bytom" + if home != "" { + switch runtime.GOOS { + case "darwin": + dataDir = filepath.Join(home, "Library", "Bytom") + case "windows": + dataDir = filepath.Join(home, "AppData", "Roaming", "Bytom") + default: + dataDir = filepath.Join(home, ".Bytom") + } + } + return dataDir +} + +func homeDir() string { + if home := os.Getenv("HOME"); home != "" { + return home + } + if usr, err := user.Current(); err == nil { + return usr.HomeDir + } + return "" +} -- 2.11.0