OSDN Git Service

Fix $HOME env (#1250)
authorHAOYUatHZ <37070449+HAOYUatHZ@users.noreply.github.com>
Mon, 13 Aug 2018 10:31:39 +0000 (18:31 +0800)
committerPaladz <yzhu101@uottawa.ca>
Mon, 13 Aug 2018 10:31:39 +0000 (18:31 +0800)
* Fix $HOME env

* Use strings.Join() to concat

cmd/bytomd/commands/root.go

index 9398867..a678d3f 100644 (file)
@@ -1,8 +1,12 @@
 package commands
 
 import (
+       "os/user"
+       "strings"
+
        "github.com/spf13/cobra"
        "github.com/spf13/viper"
+       cmn "github.com/tendermint/tmlibs/common"
 
        cfg "github.com/bytom/config"
 )
@@ -19,6 +23,15 @@ var RootCmd = &cobra.Command{
                if err != nil {
                        return err
                }
+               pathParts := strings.SplitN(config.RootDir, "/", 2)
+               if len(pathParts) == 2 && (pathParts[0] == "~" || pathParts[0] == "$HOME") {
+                       usr, err := user.Current()
+                       if err != nil {
+                               cmn.Exit("Error: " + err.Error())
+                       }
+                       pathParts[0] = usr.HomeDir
+                       config.RootDir = strings.Join(pathParts, "/")
+               }
                config.SetRoot(config.RootDir)
                return nil
        },