Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/clef, cmd/geth: use SplitAndTrim from cmd/utils #21579

Merged
merged 1 commit into from
Sep 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 3 additions & 23 deletions cmd/clef/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"math/big"
"os"
"os/signal"
"os/user"
"path/filepath"
"runtime"
"sort"
Expand Down Expand Up @@ -666,8 +665,8 @@ func signer(c *cli.Context) error {
Version: "1.0"},
}
if c.GlobalBool(utils.HTTPEnabledFlag.Name) {
vhosts := splitAndTrim(c.GlobalString(utils.HTTPVirtualHostsFlag.Name))
cors := splitAndTrim(c.GlobalString(utils.HTTPCORSDomainFlag.Name))
vhosts := utils.SplitAndTrim(c.GlobalString(utils.HTTPVirtualHostsFlag.Name))
cors := utils.SplitAndTrim(c.GlobalString(utils.HTTPCORSDomainFlag.Name))

srv := rpc.NewServer()
err := node.RegisterApisFromWhitelist(rpcAPI, []string{"account"}, srv, false)
Expand Down Expand Up @@ -736,21 +735,11 @@ func signer(c *cli.Context) error {
return nil
}

// splitAndTrim splits input separated by a comma
// and trims excessive white space from the substrings.
func splitAndTrim(input string) []string {
result := strings.Split(input, ",")
for i, r := range result {
result[i] = strings.TrimSpace(r)
}
return result
}

// DefaultConfigDir is the default config directory to use for the vaults and other
// persistence requirements.
func DefaultConfigDir() string {
// Try to place the data folder in the user's home dir
home := homeDir()
home := utils.HomeDir()
if home != "" {
if runtime.GOOS == "darwin" {
return filepath.Join(home, "Library", "Signer")
Expand All @@ -769,15 +758,6 @@ func DefaultConfigDir() string {
return ""
}

func homeDir() string {
if home := os.Getenv("HOME"); home != "" {
return home
}
if usr, err := user.Current(); err == nil {
return usr.HomeDir
}
return ""
}
func readMasterKey(ctx *cli.Context, ui core.UIClientAPI) ([]byte, error) {
var (
file string
Expand Down
15 changes: 2 additions & 13 deletions cmd/geth/retesteth.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"math/big"
"os"
"os/signal"
"strings"
"time"

"github.com/ethereum/go-ethereum/cmd/utils"
Expand Down Expand Up @@ -840,16 +839,6 @@ func (api *RetestethAPI) ClientVersion(ctx context.Context) (string, error) {
return "Geth-" + params.VersionWithCommit(gitCommit, gitDate), nil
}

// splitAndTrim splits input separated by a comma
// and trims excessive white space from the substrings.
func splitAndTrim(input string) []string {
result := strings.Split(input, ",")
for i, r := range result {
result[i] = strings.TrimSpace(r)
}
return result
}

func retesteth(ctx *cli.Context) error {
log.Info("Welcome to retesteth!")
// register signer API with server
Expand Down Expand Up @@ -887,8 +876,8 @@ func retesteth(ctx *cli.Context) error {
Version: "1.0",
},
}
vhosts := splitAndTrim(ctx.GlobalString(utils.HTTPVirtualHostsFlag.Name))
cors := splitAndTrim(ctx.GlobalString(utils.HTTPCORSDomainFlag.Name))
vhosts := utils.SplitAndTrim(ctx.GlobalString(utils.HTTPVirtualHostsFlag.Name))
cors := utils.SplitAndTrim(ctx.GlobalString(utils.HTTPCORSDomainFlag.Name))

// register apis and create handler stack
srv := rpc.NewServer()
Expand Down
4 changes: 2 additions & 2 deletions cmd/utils/customflags.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +192,14 @@ func GlobalBig(ctx *cli.Context, name string) *big.Int {
// 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 home := homeDir(); home != "" {
if home := HomeDir(); home != "" {
p = home + p[1:]
}
}
return path.Clean(os.ExpandEnv(p))
}

func homeDir() string {
func HomeDir() string {
if home := os.Getenv("HOME"); home != "" {
return home
}
Expand Down
43 changes: 21 additions & 22 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ var (
DocRootFlag = DirectoryFlag{
Name: "docroot",
Usage: "Document Root for HTTPClient file scheme",
Value: DirectoryString(homeDir()),
Value: DirectoryString(HomeDir()),
}
ExitWhenSyncedFlag = cli.BoolFlag{
Name: "exitwhensynced",
Expand Down Expand Up @@ -789,9 +789,9 @@ func setBootstrapNodes(ctx *cli.Context, cfg *p2p.Config) {
switch {
case ctx.GlobalIsSet(BootnodesFlag.Name) || ctx.GlobalIsSet(LegacyBootnodesV4Flag.Name):
if ctx.GlobalIsSet(LegacyBootnodesV4Flag.Name) {
urls = splitAndTrim(ctx.GlobalString(LegacyBootnodesV4Flag.Name))
urls = SplitAndTrim(ctx.GlobalString(LegacyBootnodesV4Flag.Name))
} else {
urls = splitAndTrim(ctx.GlobalString(BootnodesFlag.Name))
urls = SplitAndTrim(ctx.GlobalString(BootnodesFlag.Name))
}
case ctx.GlobalBool(LegacyTestnetFlag.Name) || ctx.GlobalBool(RopstenFlag.Name):
urls = params.RopstenBootnodes
Expand Down Expand Up @@ -825,9 +825,9 @@ func setBootstrapNodesV5(ctx *cli.Context, cfg *p2p.Config) {
switch {
case ctx.GlobalIsSet(BootnodesFlag.Name) || ctx.GlobalIsSet(LegacyBootnodesV5Flag.Name):
if ctx.GlobalIsSet(LegacyBootnodesV5Flag.Name) {
urls = splitAndTrim(ctx.GlobalString(LegacyBootnodesV5Flag.Name))
urls = SplitAndTrim(ctx.GlobalString(LegacyBootnodesV5Flag.Name))
} else {
urls = splitAndTrim(ctx.GlobalString(BootnodesFlag.Name))
urls = SplitAndTrim(ctx.GlobalString(BootnodesFlag.Name))
}
case ctx.GlobalBool(RopstenFlag.Name):
urls = params.RopstenBootnodes
Expand Down Expand Up @@ -873,13 +873,12 @@ func setNAT(ctx *cli.Context, cfg *p2p.Config) {
}
}

// splitAndTrim splits input separated by a comma
// SplitAndTrim splits input separated by a comma
// and trims excessive white space from the substrings.
func splitAndTrim(input string) (ret []string) {
func SplitAndTrim(input string) (ret []string) {
l := strings.Split(input, ",")
for _, r := range l {
r = strings.TrimSpace(r)
if len(r) > 0 {
if r = strings.TrimSpace(r); r != "" {
ret = append(ret, r)
}
}
Expand Down Expand Up @@ -913,27 +912,27 @@ func setHTTP(ctx *cli.Context, cfg *node.Config) {
}

if ctx.GlobalIsSet(LegacyRPCCORSDomainFlag.Name) {
cfg.HTTPCors = splitAndTrim(ctx.GlobalString(LegacyRPCCORSDomainFlag.Name))
cfg.HTTPCors = SplitAndTrim(ctx.GlobalString(LegacyRPCCORSDomainFlag.Name))
log.Warn("The flag --rpccorsdomain is deprecated and will be removed in the future, please use --http.corsdomain")
}
if ctx.GlobalIsSet(HTTPCORSDomainFlag.Name) {
cfg.HTTPCors = splitAndTrim(ctx.GlobalString(HTTPCORSDomainFlag.Name))
cfg.HTTPCors = SplitAndTrim(ctx.GlobalString(HTTPCORSDomainFlag.Name))
}

if ctx.GlobalIsSet(LegacyRPCApiFlag.Name) {
cfg.HTTPModules = splitAndTrim(ctx.GlobalString(LegacyRPCApiFlag.Name))
cfg.HTTPModules = SplitAndTrim(ctx.GlobalString(LegacyRPCApiFlag.Name))
log.Warn("The flag --rpcapi is deprecated and will be removed in the future, please use --http.api")
}
if ctx.GlobalIsSet(HTTPApiFlag.Name) {
cfg.HTTPModules = splitAndTrim(ctx.GlobalString(HTTPApiFlag.Name))
cfg.HTTPModules = SplitAndTrim(ctx.GlobalString(HTTPApiFlag.Name))
}

if ctx.GlobalIsSet(LegacyRPCVirtualHostsFlag.Name) {
cfg.HTTPVirtualHosts = splitAndTrim(ctx.GlobalString(LegacyRPCVirtualHostsFlag.Name))
cfg.HTTPVirtualHosts = SplitAndTrim(ctx.GlobalString(LegacyRPCVirtualHostsFlag.Name))
log.Warn("The flag --rpcvhosts is deprecated and will be removed in the future, please use --http.vhosts")
}
if ctx.GlobalIsSet(HTTPVirtualHostsFlag.Name) {
cfg.HTTPVirtualHosts = splitAndTrim(ctx.GlobalString(HTTPVirtualHostsFlag.Name))
cfg.HTTPVirtualHosts = SplitAndTrim(ctx.GlobalString(HTTPVirtualHostsFlag.Name))
}
}

Expand All @@ -948,10 +947,10 @@ func setGraphQL(ctx *cli.Context, cfg *node.Config) {
}
cfg.GraphQLPort = ctx.GlobalInt(GraphQLPortFlag.Name)
if ctx.GlobalIsSet(GraphQLCORSDomainFlag.Name) {
cfg.GraphQLCors = splitAndTrim(ctx.GlobalString(GraphQLCORSDomainFlag.Name))
cfg.GraphQLCors = SplitAndTrim(ctx.GlobalString(GraphQLCORSDomainFlag.Name))
}
if ctx.GlobalIsSet(GraphQLVirtualHostsFlag.Name) {
cfg.GraphQLVirtualHosts = splitAndTrim(ctx.GlobalString(GraphQLVirtualHostsFlag.Name))
cfg.GraphQLVirtualHosts = SplitAndTrim(ctx.GlobalString(GraphQLVirtualHostsFlag.Name))
}
}

Expand All @@ -977,19 +976,19 @@ func setWS(ctx *cli.Context, cfg *node.Config) {
}

if ctx.GlobalIsSet(LegacyWSAllowedOriginsFlag.Name) {
cfg.WSOrigins = splitAndTrim(ctx.GlobalString(LegacyWSAllowedOriginsFlag.Name))
cfg.WSOrigins = SplitAndTrim(ctx.GlobalString(LegacyWSAllowedOriginsFlag.Name))
log.Warn("The flag --wsorigins is deprecated and will be removed in the future, please use --ws.origins")
}
if ctx.GlobalIsSet(WSAllowedOriginsFlag.Name) {
cfg.WSOrigins = splitAndTrim(ctx.GlobalString(WSAllowedOriginsFlag.Name))
cfg.WSOrigins = SplitAndTrim(ctx.GlobalString(WSAllowedOriginsFlag.Name))
}

if ctx.GlobalIsSet(LegacyWSApiFlag.Name) {
cfg.WSModules = splitAndTrim(ctx.GlobalString(LegacyWSApiFlag.Name))
cfg.WSModules = SplitAndTrim(ctx.GlobalString(LegacyWSApiFlag.Name))
log.Warn("The flag --wsapi is deprecated and will be removed in the future, please use --ws.api")
}
if ctx.GlobalIsSet(WSApiFlag.Name) {
cfg.WSModules = splitAndTrim(ctx.GlobalString(WSApiFlag.Name))
cfg.WSModules = SplitAndTrim(ctx.GlobalString(WSApiFlag.Name))
}
}

Expand Down Expand Up @@ -1576,7 +1575,7 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *eth.Config) {
if urls == "" {
cfg.DiscoveryURLs = []string{}
} else {
cfg.DiscoveryURLs = splitAndTrim(urls)
cfg.DiscoveryURLs = SplitAndTrim(urls)
}
}

Expand Down