github.com/neatlab/neatio@v1.7.3-0.20220425043230-d903e92fcc75/chain/consensus/neatcon/utils.go (about)

     1  package neatcon
     2  
     3  import (
     4  	"os"
     5  	"os/user"
     6  	"path/filepath"
     7  	"runtime"
     8  
     9  	"gopkg.in/urfave/cli.v1"
    10  
    11  	tmcfg "github.com/neatlab/neatio/chain/consensus/neatcon/config/neatcon"
    12  	cfg "github.com/neatlib/config-go"
    13  )
    14  
    15  func GetNeatConConfig(chainId string, ctx *cli.Context) cfg.Config {
    16  	datadir := ctx.GlobalString(DataDirFlag.Name)
    17  	config := tmcfg.GetConfig(datadir, chainId)
    18  
    19  	return config
    20  }
    21  
    22  func HomeDir() string {
    23  	if home := os.Getenv("HOME"); home != "" {
    24  		return home
    25  	}
    26  	if usr, err := user.Current(); err == nil {
    27  		return usr.HomeDir
    28  	}
    29  	return ""
    30  }
    31  
    32  func DefaultDataDir() string {
    33  	// Try to place the data folder in the user's home dir
    34  	home := HomeDir()
    35  	if home != "" {
    36  		if runtime.GOOS == "windows" {
    37  			return filepath.Join(home, "AppData", "Roaming", "neatio")
    38  		} else {
    39  			return filepath.Join(home, ".neatio")
    40  		}
    41  	}
    42  	// As we cannot guess a stable location, return empty and handle later
    43  	return ""
    44  }
    45  
    46  func ConcatCopyPreAllocate(slices [][]byte) []byte {
    47  	var totalLen int
    48  	for _, s := range slices {
    49  		totalLen += len(s)
    50  	}
    51  	tmp := make([]byte, totalLen)
    52  	var i int
    53  	for _, s := range slices {
    54  		i += copy(tmp[i:], s)
    55  	}
    56  	return tmp
    57  }