path.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. sep := fmt.Sprintf("%s", os.PathSeparator)
  21. // Check in case of paths like "/something/~/something/"
  22. if len(p) > 1 && p[:1+len(sep)] == "~"+sep {
  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 AbsolutePath(Datadir string, filename string) string {
  37. if path.IsAbs(filename) {
  38. return filename
  39. }
  40. return path.Join(Datadir, filename)
  41. }
  42. func DefaultAssetPath() string {
  43. var assetPath string
  44. pwd, _ := os.Getwd()
  45. srcdir := path.Join(os.Getenv("GOPATH"), "src", "github.com", "ethereum", "go-ethereum", "cmd", "mist")
  46. // If the current working directory is the go-ethereum dir
  47. // assume a debug build and use the source directory as
  48. // asset directory.
  49. if pwd == srcdir {
  50. assetPath = path.Join(pwd, "assets")
  51. } else {
  52. switch runtime.GOOS {
  53. case "darwin":
  54. // Get Binary Directory
  55. exedir, _ := osext.ExecutableFolder()
  56. assetPath = filepath.Join(exedir, "..", "Resources")
  57. case "linux":
  58. assetPath = path.Join("usr", "share", "mist")
  59. case "windows":
  60. assetPath = path.Join(".", "assets")
  61. default:
  62. assetPath = "."
  63. }
  64. }
  65. // Check if the assetPath exists. If not, try the source directory
  66. // This happens when binary is run from outside cmd/mist directory
  67. if _, err := os.Stat(assetPath); os.IsNotExist(err) {
  68. assetPath = path.Join(srcdir, "assets")
  69. }
  70. return assetPath
  71. }
  72. func DefaultDataDir() string {
  73. usr, _ := user.Current()
  74. if runtime.GOOS == "darwin" {
  75. return path.Join(usr.HomeDir, "Library", "Ethereum")
  76. } else if runtime.GOOS == "windows" {
  77. return path.Join(usr.HomeDir, "AppData", "Roaming", "Ethereum")
  78. } else {
  79. return path.Join(usr.HomeDir, ".ethereum")
  80. }
  81. }
  82. func IsWindows() bool {
  83. return runtime.GOOS == "windows"
  84. }
  85. func WindonizePath(path string) string {
  86. if string(path[0]) == "/" && IsWindows() {
  87. path = path[1:]
  88. }
  89. return path
  90. }