path.go 2.5 KB

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