path.go 2.3 KB

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