github.com/bytom/bytom@v1.1.2-0.20221014091027-bbcba3df6075/cmd/bytomd/commands/root.go (about)

     1  package commands
     2  
     3  import (
     4  	"os/user"
     5  	"strings"
     6  
     7  	"github.com/spf13/cobra"
     8  	"github.com/spf13/viper"
     9  	cmn "github.com/tendermint/tmlibs/common"
    10  
    11  	cfg "github.com/bytom/bytom/config"
    12  )
    13  
    14  var (
    15  	config = cfg.DefaultConfig()
    16  )
    17  
    18  var RootCmd = &cobra.Command{
    19  	Use:   "bytomd",
    20  	Short: "Multiple asset management.",
    21  	PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
    22  		err := viper.Unmarshal(config)
    23  		if err != nil {
    24  			return err
    25  		}
    26  		pathParts := strings.SplitN(config.RootDir, "/", 2)
    27  		if len(pathParts) == 2 && (pathParts[0] == "~" || pathParts[0] == "$HOME") {
    28  			usr, err := user.Current()
    29  			if err != nil {
    30  				cmn.Exit("Error: " + err.Error())
    31  			}
    32  			pathParts[0] = usr.HomeDir
    33  			config.RootDir = strings.Join(pathParts, "/")
    34  		}
    35  		config.SetRoot(config.RootDir)
    36  		return nil
    37  	},
    38  }