path.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright 2014 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package common
  17. import (
  18. "fmt"
  19. "os"
  20. "os/user"
  21. "path/filepath"
  22. "runtime"
  23. "strings"
  24. )
  25. // MakeName creates a node name that follows the ethereum convention
  26. // for such names. It adds the operation system name and Go runtime version
  27. // the name.
  28. func MakeName(name, version string) string {
  29. return fmt.Sprintf("%s/v%s/%s/%s", name, version, runtime.GOOS, runtime.Version())
  30. }
  31. func ExpandHomePath(p string) (path string) {
  32. path = p
  33. sep := fmt.Sprintf("%s", os.PathSeparator)
  34. // Check in case of paths like "/something/~/something/"
  35. if len(p) > 1 && p[:1+len(sep)] == "~"+sep {
  36. usr, _ := user.Current()
  37. dir := usr.HomeDir
  38. path = strings.Replace(p, "~", dir, 1)
  39. }
  40. return
  41. }
  42. func FileExist(filePath string) bool {
  43. _, err := os.Stat(filePath)
  44. if err != nil && os.IsNotExist(err) {
  45. return false
  46. }
  47. return true
  48. }
  49. func AbsolutePath(Datadir string, filename string) string {
  50. if filepath.IsAbs(filename) {
  51. return filename
  52. }
  53. return filepath.Join(Datadir, filename)
  54. }
  55. func HomeDir() string {
  56. if home := os.Getenv("HOME"); home != "" {
  57. return home
  58. }
  59. if usr, err := user.Current(); err == nil {
  60. return usr.HomeDir
  61. }
  62. return ""
  63. }