OSDN Git Service

modify data dir
authorwz <mars@bytom.io>
Mon, 16 Apr 2018 07:35:02 +0000 (15:35 +0800)
committerwz <mars@bytom.io>
Mon, 16 Apr 2018 11:31:07 +0000 (19:31 +0800)
cmd/bytomd/main.go
config/config.go

index 618659b..6792c87 100644 (file)
@@ -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()
 }
index f73f7ea..9f11feb 100644 (file)
@@ -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 ""
+}