path.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package common
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "os/user"
  7. "path"
  8. "path/filepath"
  9. "runtime"
  10. "strings"
  11. "github.com/kardianos/osext"
  12. )
  13. // MakeName creates a node name that follows the ethereum convention
  14. // for such names. It adds the operation system name and Go runtime version
  15. // the name.
  16. func MakeName(name, version string) string {
  17. return fmt.Sprintf("%s/v%s/%s/%s", name, version, runtime.GOOS, runtime.Version())
  18. }
  19. func ExpandHomePath(p string) (path string) {
  20. path = p
  21. // Check in case of paths like "/something/~/something/"
  22. if len(path) > 1 && path[:2] == "~/" {
  23. usr, _ := user.Current()
  24. dir := usr.HomeDir
  25. path = strings.Replace(p, "~", dir, 1)
  26. }
  27. return
  28. }
  29. func FileExist(filePath string) bool {
  30. _, err := os.Stat(filePath)
  31. if err != nil && os.IsNotExist(err) {
  32. return false
  33. }
  34. return true
  35. }
  36. func ReadAllFile(filePath string) (string, error) {
  37. file, err := os.Open(filePath)
  38. if err != nil {
  39. return "", err
  40. }
  41. data, err := ioutil.ReadAll(file)
  42. if err != nil {
  43. return "", err
  44. }
  45. return string(data), nil
  46. }
  47. func WriteFile(filePath string, content []byte) error {
  48. fh, err := os.OpenFile(filePath, os.O_TRUNC|os.O_RDWR|os.O_CREATE, os.ModePerm)
  49. if err != nil {
  50. return err
  51. }
  52. defer fh.Close()
  53. _, err = fh.Write(content)
  54. if err != nil {
  55. return err
  56. }
  57. return nil
  58. }
  59. func AbsolutePath(Datadir string, filename string) string {
  60. if path.IsAbs(filename) {
  61. return filename
  62. }
  63. return path.Join(Datadir, filename)
  64. }
  65. func DefaultAssetPath() string {
  66. var assetPath string
  67. pwd, _ := os.Getwd()
  68. srcdir := path.Join(os.Getenv("GOPATH"), "src", "github.com", "ethereum", "go-ethereum", "cmd", "mist")
  69. // If the current working directory is the go-ethereum dir
  70. // assume a debug build and use the source directory as
  71. // asset directory.
  72. if pwd == srcdir {
  73. assetPath = path.Join(pwd, "assets")
  74. } else {
  75. switch runtime.GOOS {
  76. case "darwin":
  77. // Get Binary Directory
  78. exedir, _ := osext.ExecutableFolder()
  79. assetPath = filepath.Join(exedir, "../Resources")
  80. case "linux":
  81. assetPath = "/usr/share/mist"
  82. case "windows":
  83. assetPath = "./assets"
  84. default:
  85. assetPath = "."
  86. }
  87. }
  88. // Check if the assetPath exists. If not, try the source directory
  89. // This happens when binary is run from outside cmd/mist directory
  90. if _, err := os.Stat(assetPath); os.IsNotExist(err) {
  91. assetPath = path.Join(srcdir, "assets")
  92. }
  93. return assetPath
  94. }
  95. func DefaultDataDir() string {
  96. usr, _ := user.Current()
  97. if runtime.GOOS == "darwin" {
  98. return path.Join(usr.HomeDir, "Library/Ethereum")
  99. } else if runtime.GOOS == "windows" {
  100. return path.Join(usr.HomeDir, "AppData/Roaming/Ethereum")
  101. } else {
  102. return path.Join(usr.HomeDir, ".ethereum")
  103. }
  104. }
  105. func IsWindows() bool {
  106. return runtime.GOOS == "windows"
  107. }
  108. func WindonizePath(path string) string {
  109. if string(path[0]) == "/" && IsWindows() {
  110. path = path[1:]
  111. }
  112. return path
  113. }