From b42a5b118f1aa7ac1235547c8594146978941401 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Fri, 16 Sep 2016 11:53:50 +0200 Subject: common, node: move datadir defaults into package node --- cmd/gethrpctest/main.go | 9 ++++---- cmd/utils/customflags.go | 14 +++++++++-- cmd/utils/flags.go | 16 ++++++------- common/defaults.go | 48 -------------------------------------- common/path.go | 27 ---------------------- node/api.go | 4 ++-- node/config.go | 6 ++--- node/defaults.go | 60 ++++++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 89 insertions(+), 95 deletions(-) delete mode 100644 common/defaults.go create mode 100644 node/defaults.go diff --git a/cmd/gethrpctest/main.go b/cmd/gethrpctest/main.go index 2c35ec943..d0d6e1618 100644 --- a/cmd/gethrpctest/main.go +++ b/cmd/gethrpctest/main.go @@ -23,7 +23,6 @@ import ( "os" "os/signal" - "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/eth" @@ -89,11 +88,11 @@ func MakeSystemNode(privkey string, test *tests.BlockTest) (*node.Node, error) { stack, err := node.New(&node.Config{ UseLightweightKDF: true, IPCPath: node.DefaultIPCEndpoint(""), - HTTPHost: common.DefaultHTTPHost, - HTTPPort: common.DefaultHTTPPort, + HTTPHost: node.DefaultHTTPHost, + HTTPPort: node.DefaultHTTPPort, HTTPModules: []string{"admin", "db", "eth", "debug", "miner", "net", "shh", "txpool", "personal", "web3"}, - WSHost: common.DefaultWSHost, - WSPort: common.DefaultWSPort, + WSHost: node.DefaultWSHost, + WSPort: node.DefaultWSPort, WSModules: []string{"admin", "db", "eth", "debug", "miner", "net", "shh", "txpool", "personal", "web3"}, NoDiscovery: true, }) diff --git a/cmd/utils/customflags.go b/cmd/utils/customflags.go index 5cbccfe98..11c92d451 100644 --- a/cmd/utils/customflags.go +++ b/cmd/utils/customflags.go @@ -137,9 +137,19 @@ func (self *DirectoryFlag) Set(value string) { // Note, it has limitations, e.g. ~someuser/tmp will not be expanded func expandPath(p string) string { if strings.HasPrefix(p, "~/") || strings.HasPrefix(p, "~\\") { - if user, err := user.Current(); err == nil { - p = user.HomeDir + p[1:] + if home := homeDir(); home != "" { + p = home + p[1:] } } return path.Clean(os.ExpandEnv(p)) } + +func homeDir() string { + if home := os.Getenv("HOME"); home != "" { + return home + } + if usr, err := user.Current(); err == nil { + return usr.HomeDir + } + return "" +} diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index 47a8ebd41..0be499c5b 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -105,7 +105,7 @@ var ( DataDirFlag = DirectoryFlag{ Name: "datadir", Usage: "Data directory for the databases and keystore", - Value: DirectoryString{common.DefaultDataDir()}, + Value: DirectoryString{node.DefaultDataDir()}, } KeyStoreDirFlag = DirectoryFlag{ Name: "keystore", @@ -139,7 +139,7 @@ var ( DocRootFlag = DirectoryFlag{ Name: "docroot", Usage: "Document Root for HTTPClient file scheme", - Value: DirectoryString{common.HomeDir()}, + Value: DirectoryString{homeDir()}, } CacheFlag = cli.IntFlag{ Name: "cache", @@ -245,12 +245,12 @@ var ( RPCListenAddrFlag = cli.StringFlag{ Name: "rpcaddr", Usage: "HTTP-RPC server listening interface", - Value: common.DefaultHTTPHost, + Value: node.DefaultHTTPHost, } RPCPortFlag = cli.IntFlag{ Name: "rpcport", Usage: "HTTP-RPC server listening port", - Value: common.DefaultHTTPPort, + Value: node.DefaultHTTPPort, } RPCCORSDomainFlag = cli.StringFlag{ Name: "rpccorsdomain", @@ -268,13 +268,13 @@ var ( } IPCApiFlag = cli.StringFlag{ Name: "ipcapi", - Usage: "API's offered over the IPC-RPC interface", + Usage: "APIs offered over the IPC-RPC interface", Value: rpc.DefaultIPCApis, } IPCPathFlag = DirectoryFlag{ Name: "ipcpath", Usage: "Filename for IPC socket/pipe within the datadir (explicit paths escape it)", - Value: DirectoryString{common.DefaultIPCSocket}, + Value: DirectoryString{"geth.ipc"}, } WSEnabledFlag = cli.BoolFlag{ Name: "ws", @@ -283,12 +283,12 @@ var ( WSListenAddrFlag = cli.StringFlag{ Name: "wsaddr", Usage: "WS-RPC server listening interface", - Value: common.DefaultWSHost, + Value: node.DefaultWSHost, } WSPortFlag = cli.IntFlag{ Name: "wsport", Usage: "WS-RPC server listening port", - Value: common.DefaultWSPort, + Value: node.DefaultWSPort, } WSApiFlag = cli.StringFlag{ Name: "wsapi", diff --git a/common/defaults.go b/common/defaults.go deleted file mode 100644 index 8a136fa80..000000000 --- a/common/defaults.go +++ /dev/null @@ -1,48 +0,0 @@ -// Copyright 2016 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package common - -import ( - "path/filepath" - "runtime" -) - -const ( - DefaultIPCSocket = "geth.ipc" // Default (relative) name of the IPC RPC socket - DefaultHTTPHost = "localhost" // Default host interface for the HTTP RPC server - DefaultHTTPPort = 8545 // Default TCP port for the HTTP RPC server - DefaultWSHost = "localhost" // Default host interface for the websocket RPC server - DefaultWSPort = 8546 // Default TCP port for the websocket RPC server -) - -// DefaultDataDir is the default data directory to use for the databases and other -// persistence requirements. -func DefaultDataDir() string { - // Try to place the data folder in the user's home dir - home := HomeDir() - if home != "" { - if runtime.GOOS == "darwin" { - return filepath.Join(home, "Library", "Ethereum") - } else if runtime.GOOS == "windows" { - return filepath.Join(home, "AppData", "Roaming", "Ethereum") - } else { - return filepath.Join(home, ".ethereum") - } - } - // As we cannot guess a stable location, return empty and handle later - return "" -} diff --git a/common/path.go b/common/path.go index cbcd13c4f..bd8da86e7 100644 --- a/common/path.go +++ b/common/path.go @@ -19,10 +19,8 @@ package common import ( "fmt" "os" - "os/user" "path/filepath" "runtime" - "strings" ) // MakeName creates a node name that follows the ethereum convention @@ -32,21 +30,6 @@ func MakeName(name, version string) string { return fmt.Sprintf("%s/v%s/%s/%s", name, version, runtime.GOOS, runtime.Version()) } -func ExpandHomePath(p string) (path string) { - path = p - sep := string(os.PathSeparator) - - // Check in case of paths like "/something/~/something/" - if len(p) > 1 && p[:1+len(sep)] == "~"+sep { - usr, _ := user.Current() - dir := usr.HomeDir - - path = strings.Replace(p, "~", dir, 1) - } - - return -} - func FileExist(filePath string) bool { _, err := os.Stat(filePath) if err != nil && os.IsNotExist(err) { @@ -62,13 +45,3 @@ func AbsolutePath(Datadir string, filename string) string { } return filepath.Join(Datadir, filename) } - -func HomeDir() string { - if home := os.Getenv("HOME"); home != "" { - return home - } - if usr, err := user.Current(); err == nil { - return usr.HomeDir - } - return "" -} diff --git a/node/api.go b/node/api.go index 2942705d9..631e92c8e 100644 --- a/node/api.go +++ b/node/api.go @@ -84,7 +84,7 @@ func (api *PrivateAdminAPI) StartRPC(host *string, port *rpc.HexNumber, cors *st } if host == nil { - h := common.DefaultHTTPHost + h := DefaultHTTPHost if api.node.config.HTTPHost != "" { h = api.node.config.HTTPHost } @@ -133,7 +133,7 @@ func (api *PrivateAdminAPI) StartWS(host *string, port *rpc.HexNumber, allowedOr } if host == nil { - h := common.DefaultWSHost + h := DefaultWSHost if api.node.config.WSHost != "" { h = api.node.config.WSHost } diff --git a/node/config.go b/node/config.go index 4a18432c7..15884a12e 100644 --- a/node/config.go +++ b/node/config.go @@ -201,7 +201,7 @@ func DefaultIPCEndpoint(clientIdentifier string) string { panic("empty executable name") } } - config := &Config{DataDir: common.DefaultDataDir(), IPCPath: clientIdentifier + ".ipc"} + config := &Config{DataDir: DefaultDataDir(), IPCPath: clientIdentifier + ".ipc"} return config.IPCEndpoint() } @@ -216,7 +216,7 @@ func (c *Config) HTTPEndpoint() string { // DefaultHTTPEndpoint returns the HTTP endpoint used by default. func DefaultHTTPEndpoint() string { - config := &Config{HTTPHost: common.DefaultHTTPHost, HTTPPort: common.DefaultHTTPPort} + config := &Config{HTTPHost: DefaultHTTPHost, HTTPPort: DefaultHTTPPort} return config.HTTPEndpoint() } @@ -231,7 +231,7 @@ func (c *Config) WSEndpoint() string { // DefaultWSEndpoint returns the websocket endpoint used by default. func DefaultWSEndpoint() string { - config := &Config{WSHost: common.DefaultWSHost, WSPort: common.DefaultWSPort} + config := &Config{WSHost: DefaultWSHost, WSPort: DefaultWSPort} return config.WSEndpoint() } diff --git a/node/defaults.go b/node/defaults.go new file mode 100644 index 000000000..bfe257c8e --- /dev/null +++ b/node/defaults.go @@ -0,0 +1,60 @@ +// Copyright 2016 The go-ethereum Authors +// This file is part of the go-ethereum library. +// +// The go-ethereum library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The go-ethereum library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see . + +package node + +import ( + "os" + "os/user" + "path/filepath" + "runtime" +) + +const ( + DefaultIPCSocket = "geth.ipc" // Default (relative) name of the IPC RPC socket + DefaultHTTPHost = "localhost" // Default host interface for the HTTP RPC server + DefaultHTTPPort = 8545 // Default TCP port for the HTTP RPC server + DefaultWSHost = "localhost" // Default host interface for the websocket RPC server + DefaultWSPort = 8546 // Default TCP port for the websocket RPC server +) + +// DefaultDataDir is the default data directory to use for the databases and other +// persistence requirements. +func DefaultDataDir() string { + // Try to place the data folder in the user's home dir + home := homeDir() + if home != "" { + if runtime.GOOS == "darwin" { + return filepath.Join(home, "Library", "Ethereum") + } else if runtime.GOOS == "windows" { + return filepath.Join(home, "AppData", "Roaming", "Ethereum") + } else { + return filepath.Join(home, ".ethereum") + } + } + // As we cannot guess a stable location, return empty and handle later + return "" +} + +func homeDir() string { + if home := os.Getenv("HOME"); home != "" { + return home + } + if usr, err := user.Current(); err == nil { + return usr.HomeDir + } + return "" +} -- cgit v1.2.3