From 8ddf889152a8aff85164abe074d47f634af523db Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Tue, 25 Feb 2025 12:52:22 +0100 Subject: [PATCH] cmd/utils: move CheckExclusive to package internal/flags --- cmd/abigen/main.go | 2 +- cmd/utils/flags.go | 53 +++++---------------------------------- internal/flags/helpers.go | 42 +++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 48 deletions(-) diff --git a/cmd/abigen/main.go b/cmd/abigen/main.go index 8ee3130b5b..10343190c3 100644 --- a/cmd/abigen/main.go +++ b/cmd/abigen/main.go @@ -93,7 +93,7 @@ func init() { } func abigen(c *cli.Context) error { - utils.CheckExclusive(c, abiFlag, jsonFlag) // Only one source can be selected. + flags.CheckExclusive(c, abiFlag, jsonFlag) // Only one source can be selected. if c.String(pkgFlag.Name) == "" { utils.Fatalf("No destination package specified (--pkg)") diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index 243b5d22f0..728ec2d667 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -1197,7 +1197,7 @@ func setWS(ctx *cli.Context, cfg *node.Config) { // setIPC creates an IPC path configuration from the set command line flags, // returning an empty string if IPC was explicitly disabled, or the set path. func setIPC(ctx *cli.Context, cfg *node.Config) { - CheckExclusive(ctx, IPCDisabledFlag, IPCPathFlag) + flags.CheckExclusive(ctx, IPCDisabledFlag, IPCPathFlag) switch { case ctx.Bool(IPCDisabledFlag.Name): cfg.IPCPath = "" @@ -1295,8 +1295,8 @@ func SetP2PConfig(ctx *cli.Context, cfg *p2p.Config) { cfg.NoDiscovery = true } - CheckExclusive(ctx, DiscoveryV4Flag, NoDiscoverFlag) - CheckExclusive(ctx, DiscoveryV5Flag, NoDiscoverFlag) + flags.CheckExclusive(ctx, DiscoveryV4Flag, NoDiscoverFlag) + flags.CheckExclusive(ctx, DiscoveryV5Flag, NoDiscoverFlag) cfg.DiscoveryV4 = ctx.Bool(DiscoveryV4Flag.Name) cfg.DiscoveryV5 = ctx.Bool(DiscoveryV5Flag.Name) @@ -1528,52 +1528,11 @@ func setRequiredBlocks(ctx *cli.Context, cfg *ethconfig.Config) { } } -// CheckExclusive verifies that only a single instance of the provided flags was -// set by the user. Each flag might optionally be followed by a string type to -// specialize it further. -func CheckExclusive(ctx *cli.Context, args ...interface{}) { - set := make([]string, 0, 1) - for i := 0; i < len(args); i++ { - // Make sure the next argument is a flag and skip if not set - flag, ok := args[i].(cli.Flag) - if !ok { - panic(fmt.Sprintf("invalid argument, not cli.Flag type: %T", args[i])) - } - // Check if next arg extends current and expand its name if so - name := flag.Names()[0] - - if i+1 < len(args) { - switch option := args[i+1].(type) { - case string: - // Extended flag check, make sure value set doesn't conflict with passed in option - if ctx.String(flag.Names()[0]) == option { - name += "=" + option - set = append(set, "--"+name) - } - // shift arguments and continue - i++ - continue - - case cli.Flag: - default: - panic(fmt.Sprintf("invalid argument, not cli.Flag or string extension: %T", args[i+1])) - } - } - // Mark the flag if it's set - if ctx.IsSet(flag.Names()[0]) { - set = append(set, "--"+name) - } - } - if len(set) > 1 { - Fatalf("Flags %v can't be used at the same time", strings.Join(set, ", ")) - } -} - // SetEthConfig applies eth-related command line flags to the config. func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) { // Avoid conflicting network flags - CheckExclusive(ctx, MainnetFlag, DeveloperFlag, SepoliaFlag, HoleskyFlag) - CheckExclusive(ctx, DeveloperFlag, ExternalSignerFlag) // Can't use both ephemeral unlocked and external signer + flags.CheckExclusive(ctx, MainnetFlag, DeveloperFlag, SepoliaFlag, HoleskyFlag) + flags.CheckExclusive(ctx, DeveloperFlag, ExternalSignerFlag) // Can't use both ephemeral unlocked and external signer // Set configurations from CLI flags setEtherbase(ctx, cfg) @@ -1841,7 +1800,7 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) { func MakeBeaconLightConfig(ctx *cli.Context) bparams.ClientConfig { var config bparams.ClientConfig customConfig := ctx.IsSet(BeaconConfigFlag.Name) - CheckExclusive(ctx, MainnetFlag, SepoliaFlag, HoleskyFlag, BeaconConfigFlag) + flags.CheckExclusive(ctx, MainnetFlag, SepoliaFlag, HoleskyFlag, BeaconConfigFlag) switch { case ctx.Bool(MainnetFlag.Name): config.ChainConfig = *bparams.MainnetLightConfig diff --git a/internal/flags/helpers.go b/internal/flags/helpers.go index 170b67b310..fc84ae85da 100644 --- a/internal/flags/helpers.go +++ b/internal/flags/helpers.go @@ -295,3 +295,45 @@ func CheckEnvVars(ctx *cli.Context, flags []cli.Flag, prefix string) { } } } + +// CheckExclusive verifies that only a single instance of the provided flags was +// set by the user. Each flag might optionally be followed by a string type to +// specialize it further. +func CheckExclusive(ctx *cli.Context, args ...any) { + set := make([]string, 0, 1) + for i := 0; i < len(args); i++ { + // Make sure the next argument is a flag and skip if not set + flag, ok := args[i].(cli.Flag) + if !ok { + panic(fmt.Sprintf("invalid argument, not cli.Flag type: %T", args[i])) + } + // Check if next arg extends current and expand its name if so + name := flag.Names()[0] + + if i+1 < len(args) { + switch option := args[i+1].(type) { + case string: + // Extended flag check, make sure value set doesn't conflict with passed in option + if ctx.String(flag.Names()[0]) == option { + name += "=" + option + set = append(set, "--"+name) + } + // shift arguments and continue + i++ + continue + + case cli.Flag: + default: + panic(fmt.Sprintf("invalid argument, not cli.Flag or string extension: %T", args[i+1])) + } + } + // Mark the flag if it's set + if ctx.IsSet(flag.Names()[0]) { + set = append(set, "--"+name) + } + } + if len(set) > 1 { + fmt.Fprintf(os.Stderr, "Flags %v can't be used at the same time", strings.Join(set, ", ")) + os.Exit(1) + } +}