|
| 1 | +// Package flag provides utilities for managing command flags with environment variable fallback support. |
| 2 | +// It implements a priority system: flag value > environment variable > default value. |
1 | 3 | package flag |
2 | 4 |
|
3 | 5 | import ( |
4 | | - "github.com/0xPolygon/polygon-cli/util" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + |
5 | 9 | "github.com/rs/zerolog/log" |
6 | 10 | "github.com/spf13/cobra" |
7 | | - "github.com/spf13/viper" |
8 | 11 | ) |
9 | 12 |
|
10 | 13 | const ( |
11 | | - // RPCURL is the standard flag name for RPC endpoint URLs. |
| 14 | + // RPCURL is the flag name for RPC URL |
12 | 15 | RPCURL = "rpc-url" |
13 | | - // PrivateKey is the standard flag name for private keys. |
14 | | - PrivateKey = "private-key" |
15 | | - |
16 | | - // DefaultRPCURL is the default RPC endpoint URL. |
| 16 | + // RPCURLEnvVar is the environment variable name for RPC URL |
| 17 | + RPCURLEnvVar = "ETH_RPC_URL" |
| 18 | + // DefaultRPCURL is the default RPC URL when no flag or env var is set |
17 | 19 | DefaultRPCURL = "http://localhost:8545" |
| 20 | + // PrivateKey is the flag name for private key |
| 21 | + PrivateKey = "private-key" |
| 22 | + // PrivateKeyEnvVar is the environment variable name for private key |
| 23 | + PrivateKeyEnvVar = "PRIVATE_KEY" |
18 | 24 | ) |
19 | 25 |
|
20 | | -// GetFlag retrieves a flag value from Viper after binding it. |
21 | | -// It binds the flag to enable environment variable fallback via Viper. |
22 | | -func GetFlag(cmd *cobra.Command, flagName string) string { |
23 | | - if err := viper.BindPFlag(flagName, cmd.Flags().Lookup(flagName)); err != nil { |
24 | | - log.Fatal().Err(err).Str("flag", flagName).Msg("Failed to bind flag to viper") |
25 | | - } |
26 | | - return viper.GetString(flagName) |
| 26 | +// GetRPCURL retrieves the RPC URL from the command flag or environment variable. |
| 27 | +// Returns the flag value if set, otherwise the environment variable value, otherwise the default. |
| 28 | +// Returns empty string and nil error if none are set. |
| 29 | +func GetRPCURL(cmd *cobra.Command) (string, error) { |
| 30 | + return getValue(cmd, RPCURL, RPCURLEnvVar, false) |
27 | 31 | } |
28 | 32 |
|
29 | | -// GetRPCURL retrieves the rpc-url flag value from Viper after binding it and validates |
30 | | -// that it is a valid URL with a supported scheme (http, https, ws, wss). |
31 | | -func GetRPCURL(cmd *cobra.Command) (string, error) { |
32 | | - rpcURL := GetFlag(cmd, RPCURL) |
33 | | - if err := util.ValidateUrl(rpcURL); err != nil { |
34 | | - return "", err |
35 | | - } |
36 | | - return rpcURL, nil |
| 33 | +// GetRequiredRPCURL retrieves the RPC URL from the command flag or environment variable. |
| 34 | +// Returns an error if the value is not set or empty. |
| 35 | +func GetRequiredRPCURL(cmd *cobra.Command) (string, error) { |
| 36 | + return getValue(cmd, RPCURL, RPCURLEnvVar, true) |
37 | 37 | } |
38 | 38 |
|
39 | | -// GetPrivateKey retrieves the private-key flag value from Viper after binding it. |
40 | | -// This is a convenience wrapper around GetFlag for the standard private key flag. |
| 39 | +// GetPrivateKey retrieves the private key from the command flag or environment variable. |
| 40 | +// Returns the flag value if set, otherwise the environment variable value, otherwise the default. |
| 41 | +// Returns empty string and nil error if none are set. |
41 | 42 | func GetPrivateKey(cmd *cobra.Command) (string, error) { |
42 | | - return GetFlag(cmd, PrivateKey), nil |
| 43 | + return getValue(cmd, PrivateKey, PrivateKeyEnvVar, false) |
| 44 | +} |
| 45 | + |
| 46 | +// GetRequiredPrivateKey retrieves the private key from the command flag or environment variable. |
| 47 | +// Returns an error if the value is not set or empty. |
| 48 | +func GetRequiredPrivateKey(cmd *cobra.Command) (string, error) { |
| 49 | + return getValue(cmd, PrivateKey, PrivateKeyEnvVar, true) |
| 50 | +} |
| 51 | + |
| 52 | +// getValue retrieves a flag value with environment variable fallback support. |
| 53 | +// It implements a priority system where flag values take precedence over environment variables, |
| 54 | +// which take precedence over default values. |
| 55 | +// |
| 56 | +// Parameters: |
| 57 | +// - cmd: The cobra command to retrieve the flag from |
| 58 | +// - flagName: The name of the flag to retrieve |
| 59 | +// - envVarName: The environment variable name to check as fallback |
| 60 | +// - required: Whether the value is required (returns error if empty) |
| 61 | +// |
| 62 | +// Returns the resolved value and an error if required validation fails. |
| 63 | +func getValue(cmd *cobra.Command, flagName, envVarName string, required bool) (string, error) { |
| 64 | + flag := cmd.Flag(flagName) |
| 65 | + if flag == nil { |
| 66 | + return "", fmt.Errorf("flag %q not found", flagName) |
| 67 | + } |
| 68 | + |
| 69 | + // Priority: flag > env var > default |
| 70 | + value := flag.DefValue |
| 71 | + |
| 72 | + envVarValue := os.Getenv(envVarName) |
| 73 | + if envVarValue != "" { |
| 74 | + value = envVarValue |
| 75 | + } |
| 76 | + |
| 77 | + if flag.Changed { |
| 78 | + value = flag.Value.String() |
| 79 | + } |
| 80 | + |
| 81 | + if required && value == "" { |
| 82 | + return "", fmt.Errorf("required flag(s) %q not set", flagName) |
| 83 | + } |
| 84 | + |
| 85 | + return value, nil |
43 | 86 | } |
44 | 87 |
|
45 | 88 | // MarkFlagRequired marks a regular flag as required and logs a fatal error if marking fails. |
|
0 commit comments