path.go 2.6 KB
Newer Older
obscuren's avatar
obscuren committed
1
package common
obscuren's avatar
obscuren committed
2 3

import (
Taylor Gerring's avatar
Taylor Gerring committed
4
	"fmt"
obscuren's avatar
obscuren committed
5
	"os"
obscuren's avatar
obscuren committed
6
	"os/user"
7 8
	"path/filepath"
	"runtime"
obscuren's avatar
obscuren committed
9
	"strings"
10 11

	"github.com/kardianos/osext"
obscuren's avatar
obscuren committed
12 13
)

Taylor Gerring's avatar
Taylor Gerring committed
14 15 16 17 18 19 20
// MakeName creates a node name that follows the ethereum convention
// for such names. It adds the operation system name and Go runtime version
// the name.
func MakeName(name, version string) string {
	return fmt.Sprintf("%s/v%s/%s/%s", name, version, runtime.GOOS, runtime.Version())
}

obscuren's avatar
obscuren committed
21 22
func ExpandHomePath(p string) (path string) {
	path = p
23
	sep := fmt.Sprintf("%s", os.PathSeparator)
obscuren's avatar
obscuren committed
24 25

	// Check in case of paths like "/something/~/something/"
26
	if len(p) > 1 && p[:1+len(sep)] == "~"+sep {
obscuren's avatar
obscuren committed
27 28 29 30 31 32 33 34
		usr, _ := user.Current()
		dir := usr.HomeDir

		path = strings.Replace(p, "~", dir, 1)
	}

	return
}
obscuren's avatar
obscuren committed
35 36 37 38 39 40 41 42 43 44

func FileExist(filePath string) bool {
	_, err := os.Stat(filePath)
	if err != nil && os.IsNotExist(err) {
		return false
	}

	return true
}

obscuren's avatar
obscuren committed
45
func AbsolutePath(Datadir string, filename string) string {
46
	if filepath.IsAbs(filename) {
obscuren's avatar
obscuren committed
47 48
		return filename
	}
49
	return filepath.Join(Datadir, filename)
obscuren's avatar
obscuren committed
50
}
51 52 53 54

func DefaultAssetPath() string {
	var assetPath string
	pwd, _ := os.Getwd()
55
	srcdir := filepath.Join(os.Getenv("GOPATH"), "src", "github.com", "ethereum", "go-ethereum", "cmd", "mist")
56 57 58 59 60

	// If the current working directory is the go-ethereum dir
	// assume a debug build and use the source directory as
	// asset directory.
	if pwd == srcdir {
61
		assetPath = filepath.Join(pwd, "assets")
62 63 64 65 66
	} else {
		switch runtime.GOOS {
		case "darwin":
			// Get Binary Directory
			exedir, _ := osext.ExecutableFolder()
67
			assetPath = filepath.Join(exedir, "..", "Resources")
68
		case "linux":
69
			assetPath = filepath.Join("usr", "share", "mist")
70
		case "windows":
71
			assetPath = filepath.Join(".", "assets")
72 73 74 75 76 77 78 79
		default:
			assetPath = "."
		}
	}

	// Check if the assetPath exists. If not, try the source directory
	// This happens when binary is run from outside cmd/mist directory
	if _, err := os.Stat(assetPath); os.IsNotExist(err) {
80
		assetPath = filepath.Join(srcdir, "assets")
81 82 83 84 85 86 87 88
	}

	return assetPath
}

func DefaultDataDir() string {
	usr, _ := user.Current()
	if runtime.GOOS == "darwin" {
89
		return filepath.Join(usr.HomeDir, "Library", "Ethereum")
90
	} else if runtime.GOOS == "windows" {
91
		return filepath.Join(usr.HomeDir, "AppData", "Roaming", "Ethereum")
92
	} else {
93
		return filepath.Join(usr.HomeDir, ".ethereum")
94 95 96
	}
}

97
func DefaultIpcPath() string {
98 99 100
	if runtime.GOOS == "windows" {
		return `\\.\pipe\geth.ipc`
	}
101 102 103
	return filepath.Join(DefaultDataDir(), "geth.ipc")
}

104 105 106 107 108 109 110 111 112 113
func IsWindows() bool {
	return runtime.GOOS == "windows"
}

func WindonizePath(path string) string {
	if string(path[0]) == "/" && IsWindows() {
		path = path[1:]
	}
	return path
}