path.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package common
  2. import (
  3. "fmt"
  4. "os"
  5. "os/user"
  6. "path/filepath"
  7. "runtime"
  8. "strings"
  9. "github.com/kardianos/osext"
  10. )
  11. // MakeName creates a node name that follows the ethereum convention
  12. // for such names. It adds the operation system name and Go runtime version
  13. // the name.
  14. func MakeName(name, version string) string {
  15. return fmt.Sprintf("%s/v%s/%s/%s", name, version, runtime.GOOS, runtime.Version())
  16. }
  17. func ExpandHomePath(p string) (path string) {
  18. path = p
  19. sep := fmt.Sprintf("%s", os.PathSeparator)
  20. // Check in case of paths like "/something/~/something/"
  21. if len(p) > 1 && p[:1+len(sep)] == "~"+sep {
  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 filepath.IsAbs(filename) {
  37. return filename
  38. }
  39. return filepath.Join(Datadir, filename)
  40. }
  41. func DefaultAssetPath() string {
  42. var assetPath string
  43. pwd, _ := os.Getwd()
  44. srcdir := filepath.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 = filepath.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 = filepath.Join("usr", "share", "mist")
  58. case "windows":
  59. assetPath = filepath.Join(".", "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 = filepath.Join(srcdir, "assets")
  68. }
  69. return assetPath
  70. }
  71. func DefaultDataDir() string {
  72. usr, _ := user.Current()
  73. if runtime.GOOS == "darwin" {
  74. return filepath.Join(usr.HomeDir, "Library", "Ethereum")
  75. } else if runtime.GOOS == "windows" {
  76. return filepath.Join(usr.HomeDir, "AppData", "Roaming", "Ethereum")
  77. } else {
  78. return filepath.Join(usr.HomeDir, ".ethereum")
  79. }
  80. }
  81. func DefaultIpcPath() string {
  82. if runtime.GOOS == "windows" {
  83. return `\\.\pipe\geth.ipc`
  84. }
  85. return filepath.Join(DefaultDataDir(), "geth.ipc")
  86. }
  87. func IsWindows() bool {
  88. return runtime.GOOS == "windows"
  89. }
  90. func WindonizePath(path string) string {
  91. if string(path[0]) == "/" && IsWindows() {
  92. path = path[1:]
  93. }
  94. return path
  95. }