Skip to content

Commit fdb7424

Browse files
authored
cmd/clef, cmd/geth: use SplitAndTrim from cmd/utils (#21579)
1 parent 129cf07 commit fdb7424

File tree

4 files changed

+28
-60
lines changed

4 files changed

+28
-60
lines changed

cmd/clef/main.go

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929
"math/big"
3030
"os"
3131
"os/signal"
32-
"os/user"
3332
"path/filepath"
3433
"runtime"
3534
"sort"
@@ -666,8 +665,8 @@ func signer(c *cli.Context) error {
666665
Version: "1.0"},
667666
}
668667
if c.GlobalBool(utils.HTTPEnabledFlag.Name) {
669-
vhosts := splitAndTrim(c.GlobalString(utils.HTTPVirtualHostsFlag.Name))
670-
cors := splitAndTrim(c.GlobalString(utils.HTTPCORSDomainFlag.Name))
668+
vhosts := utils.SplitAndTrim(c.GlobalString(utils.HTTPVirtualHostsFlag.Name))
669+
cors := utils.SplitAndTrim(c.GlobalString(utils.HTTPCORSDomainFlag.Name))
671670

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

739-
// splitAndTrim splits input separated by a comma
740-
// and trims excessive white space from the substrings.
741-
func splitAndTrim(input string) []string {
742-
result := strings.Split(input, ",")
743-
for i, r := range result {
744-
result[i] = strings.TrimSpace(r)
745-
}
746-
return result
747-
}
748-
749738
// DefaultConfigDir is the default config directory to use for the vaults and other
750739
// persistence requirements.
751740
func DefaultConfigDir() string {
752741
// Try to place the data folder in the user's home dir
753-
home := homeDir()
742+
home := utils.HomeDir()
754743
if home != "" {
755744
if runtime.GOOS == "darwin" {
756745
return filepath.Join(home, "Library", "Signer")
@@ -769,15 +758,6 @@ func DefaultConfigDir() string {
769758
return ""
770759
}
771760

772-
func homeDir() string {
773-
if home := os.Getenv("HOME"); home != "" {
774-
return home
775-
}
776-
if usr, err := user.Current(); err == nil {
777-
return usr.HomeDir
778-
}
779-
return ""
780-
}
781761
func readMasterKey(ctx *cli.Context, ui core.UIClientAPI) ([]byte, error) {
782762
var (
783763
file string

cmd/geth/retesteth.go

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"math/big"
2424
"os"
2525
"os/signal"
26-
"strings"
2726
"time"
2827

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

843-
// splitAndTrim splits input separated by a comma
844-
// and trims excessive white space from the substrings.
845-
func splitAndTrim(input string) []string {
846-
result := strings.Split(input, ",")
847-
for i, r := range result {
848-
result[i] = strings.TrimSpace(r)
849-
}
850-
return result
851-
}
852-
853842
func retesteth(ctx *cli.Context) error {
854843
log.Info("Welcome to retesteth!")
855844
// register signer API with server
@@ -887,8 +876,8 @@ func retesteth(ctx *cli.Context) error {
887876
Version: "1.0",
888877
},
889878
}
890-
vhosts := splitAndTrim(ctx.GlobalString(utils.HTTPVirtualHostsFlag.Name))
891-
cors := splitAndTrim(ctx.GlobalString(utils.HTTPCORSDomainFlag.Name))
879+
vhosts := utils.SplitAndTrim(ctx.GlobalString(utils.HTTPVirtualHostsFlag.Name))
880+
cors := utils.SplitAndTrim(ctx.GlobalString(utils.HTTPCORSDomainFlag.Name))
892881

893882
// register apis and create handler stack
894883
srv := rpc.NewServer()

cmd/utils/customflags.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,14 +192,14 @@ func GlobalBig(ctx *cli.Context, name string) *big.Int {
192192
// Note, it has limitations, e.g. ~someuser/tmp will not be expanded
193193
func expandPath(p string) string {
194194
if strings.HasPrefix(p, "~/") || strings.HasPrefix(p, "~\\") {
195-
if home := homeDir(); home != "" {
195+
if home := HomeDir(); home != "" {
196196
p = home + p[1:]
197197
}
198198
}
199199
return path.Clean(os.ExpandEnv(p))
200200
}
201201

202-
func homeDir() string {
202+
func HomeDir() string {
203203
if home := os.Getenv("HOME"); home != "" {
204204
return home
205205
}

cmd/utils/flags.go

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ var (
162162
DocRootFlag = DirectoryFlag{
163163
Name: "docroot",
164164
Usage: "Document Root for HTTPClient file scheme",
165-
Value: DirectoryString(homeDir()),
165+
Value: DirectoryString(HomeDir()),
166166
}
167167
ExitWhenSyncedFlag = cli.BoolFlag{
168168
Name: "exitwhensynced",
@@ -793,9 +793,9 @@ func setBootstrapNodes(ctx *cli.Context, cfg *p2p.Config) {
793793
switch {
794794
case ctx.GlobalIsSet(BootnodesFlag.Name) || ctx.GlobalIsSet(LegacyBootnodesV4Flag.Name):
795795
if ctx.GlobalIsSet(LegacyBootnodesV4Flag.Name) {
796-
urls = splitAndTrim(ctx.GlobalString(LegacyBootnodesV4Flag.Name))
796+
urls = SplitAndTrim(ctx.GlobalString(LegacyBootnodesV4Flag.Name))
797797
} else {
798-
urls = splitAndTrim(ctx.GlobalString(BootnodesFlag.Name))
798+
urls = SplitAndTrim(ctx.GlobalString(BootnodesFlag.Name))
799799
}
800800
case ctx.GlobalBool(LegacyTestnetFlag.Name) || ctx.GlobalBool(RopstenFlag.Name):
801801
urls = params.RopstenBootnodes
@@ -829,9 +829,9 @@ func setBootstrapNodesV5(ctx *cli.Context, cfg *p2p.Config) {
829829
switch {
830830
case ctx.GlobalIsSet(BootnodesFlag.Name) || ctx.GlobalIsSet(LegacyBootnodesV5Flag.Name):
831831
if ctx.GlobalIsSet(LegacyBootnodesV5Flag.Name) {
832-
urls = splitAndTrim(ctx.GlobalString(LegacyBootnodesV5Flag.Name))
832+
urls = SplitAndTrim(ctx.GlobalString(LegacyBootnodesV5Flag.Name))
833833
} else {
834-
urls = splitAndTrim(ctx.GlobalString(BootnodesFlag.Name))
834+
urls = SplitAndTrim(ctx.GlobalString(BootnodesFlag.Name))
835835
}
836836
case ctx.GlobalBool(RopstenFlag.Name):
837837
urls = params.RopstenBootnodes
@@ -877,13 +877,12 @@ func setNAT(ctx *cli.Context, cfg *p2p.Config) {
877877
}
878878
}
879879

880-
// splitAndTrim splits input separated by a comma
880+
// SplitAndTrim splits input separated by a comma
881881
// and trims excessive white space from the substrings.
882-
func splitAndTrim(input string) (ret []string) {
882+
func SplitAndTrim(input string) (ret []string) {
883883
l := strings.Split(input, ",")
884884
for _, r := range l {
885-
r = strings.TrimSpace(r)
886-
if len(r) > 0 {
885+
if r = strings.TrimSpace(r); r != "" {
887886
ret = append(ret, r)
888887
}
889888
}
@@ -917,38 +916,38 @@ func setHTTP(ctx *cli.Context, cfg *node.Config) {
917916
}
918917

919918
if ctx.GlobalIsSet(LegacyRPCCORSDomainFlag.Name) {
920-
cfg.HTTPCors = splitAndTrim(ctx.GlobalString(LegacyRPCCORSDomainFlag.Name))
919+
cfg.HTTPCors = SplitAndTrim(ctx.GlobalString(LegacyRPCCORSDomainFlag.Name))
921920
log.Warn("The flag --rpccorsdomain is deprecated and will be removed in the future, please use --http.corsdomain")
922921
}
923922
if ctx.GlobalIsSet(HTTPCORSDomainFlag.Name) {
924-
cfg.HTTPCors = splitAndTrim(ctx.GlobalString(HTTPCORSDomainFlag.Name))
923+
cfg.HTTPCors = SplitAndTrim(ctx.GlobalString(HTTPCORSDomainFlag.Name))
925924
}
926925

927926
if ctx.GlobalIsSet(LegacyRPCApiFlag.Name) {
928-
cfg.HTTPModules = splitAndTrim(ctx.GlobalString(LegacyRPCApiFlag.Name))
927+
cfg.HTTPModules = SplitAndTrim(ctx.GlobalString(LegacyRPCApiFlag.Name))
929928
log.Warn("The flag --rpcapi is deprecated and will be removed in the future, please use --http.api")
930929
}
931930
if ctx.GlobalIsSet(HTTPApiFlag.Name) {
932-
cfg.HTTPModules = splitAndTrim(ctx.GlobalString(HTTPApiFlag.Name))
931+
cfg.HTTPModules = SplitAndTrim(ctx.GlobalString(HTTPApiFlag.Name))
933932
}
934933

935934
if ctx.GlobalIsSet(LegacyRPCVirtualHostsFlag.Name) {
936-
cfg.HTTPVirtualHosts = splitAndTrim(ctx.GlobalString(LegacyRPCVirtualHostsFlag.Name))
935+
cfg.HTTPVirtualHosts = SplitAndTrim(ctx.GlobalString(LegacyRPCVirtualHostsFlag.Name))
937936
log.Warn("The flag --rpcvhosts is deprecated and will be removed in the future, please use --http.vhosts")
938937
}
939938
if ctx.GlobalIsSet(HTTPVirtualHostsFlag.Name) {
940-
cfg.HTTPVirtualHosts = splitAndTrim(ctx.GlobalString(HTTPVirtualHostsFlag.Name))
939+
cfg.HTTPVirtualHosts = SplitAndTrim(ctx.GlobalString(HTTPVirtualHostsFlag.Name))
941940
}
942941
}
943942

944943
// setGraphQL creates the GraphQL listener interface string from the set
945944
// command line flags, returning empty if the GraphQL endpoint is disabled.
946945
func setGraphQL(ctx *cli.Context, cfg *node.Config) {
947946
if ctx.GlobalIsSet(GraphQLCORSDomainFlag.Name) {
948-
cfg.GraphQLCors = splitAndTrim(ctx.GlobalString(GraphQLCORSDomainFlag.Name))
947+
cfg.GraphQLCors = SplitAndTrim(ctx.GlobalString(GraphQLCORSDomainFlag.Name))
949948
}
950949
if ctx.GlobalIsSet(GraphQLVirtualHostsFlag.Name) {
951-
cfg.GraphQLVirtualHosts = splitAndTrim(ctx.GlobalString(GraphQLVirtualHostsFlag.Name))
950+
cfg.GraphQLVirtualHosts = SplitAndTrim(ctx.GlobalString(GraphQLVirtualHostsFlag.Name))
952951
}
953952
}
954953

@@ -974,19 +973,19 @@ func setWS(ctx *cli.Context, cfg *node.Config) {
974973
}
975974

976975
if ctx.GlobalIsSet(LegacyWSAllowedOriginsFlag.Name) {
977-
cfg.WSOrigins = splitAndTrim(ctx.GlobalString(LegacyWSAllowedOriginsFlag.Name))
976+
cfg.WSOrigins = SplitAndTrim(ctx.GlobalString(LegacyWSAllowedOriginsFlag.Name))
978977
log.Warn("The flag --wsorigins is deprecated and will be removed in the future, please use --ws.origins")
979978
}
980979
if ctx.GlobalIsSet(WSAllowedOriginsFlag.Name) {
981-
cfg.WSOrigins = splitAndTrim(ctx.GlobalString(WSAllowedOriginsFlag.Name))
980+
cfg.WSOrigins = SplitAndTrim(ctx.GlobalString(WSAllowedOriginsFlag.Name))
982981
}
983982

984983
if ctx.GlobalIsSet(LegacyWSApiFlag.Name) {
985-
cfg.WSModules = splitAndTrim(ctx.GlobalString(LegacyWSApiFlag.Name))
984+
cfg.WSModules = SplitAndTrim(ctx.GlobalString(LegacyWSApiFlag.Name))
986985
log.Warn("The flag --wsapi is deprecated and will be removed in the future, please use --ws.api")
987986
}
988987
if ctx.GlobalIsSet(WSApiFlag.Name) {
989-
cfg.WSModules = splitAndTrim(ctx.GlobalString(WSApiFlag.Name))
988+
cfg.WSModules = SplitAndTrim(ctx.GlobalString(WSApiFlag.Name))
990989
}
991990
}
992991

@@ -1580,7 +1579,7 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *eth.Config) {
15801579
if urls == "" {
15811580
cfg.DiscoveryURLs = []string{}
15821581
} else {
1583-
cfg.DiscoveryURLs = splitAndTrim(urls)
1582+
cfg.DiscoveryURLs = SplitAndTrim(urls)
15841583
}
15851584
}
15861585

0 commit comments

Comments
 (0)