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

add a flag loader to allow flags to be set also via env vars #524

Merged
merged 3 commits into from
Feb 28, 2025
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
6 changes: 5 additions & 1 deletion cmd/cdk/cdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"
"time"

"github.com/0xPolygon/polygon-cli/cmd/flag_loader"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/go-errors/errors"
Expand Down Expand Up @@ -86,7 +87,10 @@ var CDKCmd = &cobra.Command{
Use: "cdk",
Short: "Utilities for interacting with CDK networks",
Long: "Basic utility commands for interacting with the cdk contracts",
Args: cobra.NoArgs,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
cdkInputArgs.rpcURL = flag_loader.GetRpcUrlFlagValue(cmd)
},
Args: cobra.NoArgs,
}

type inputArgs struct {
Expand Down
5 changes: 5 additions & 0 deletions cmd/dumpblocks/dumpblocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

_ "embed"

"github.com/0xPolygon/polygon-cli/cmd/flag_loader"
"github.com/0xPolygon/polygon-cli/proto/gen/pb"
"github.com/0xPolygon/polygon-cli/rpctypes"
"github.com/0xPolygon/polygon-cli/util"
Expand Down Expand Up @@ -54,6 +55,10 @@ var DumpblocksCmd = &cobra.Command{
Use: "dumpblocks start end",
Short: "Export a range of blocks from a JSON-RPC endpoint.",
Long: usage,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
rpcUrlFlagValue := flag_loader.GetRpcUrlFlagValue(cmd)
inputDumpblocks.RpcUrl = *rpcUrlFlagValue
},
PreRunE: func(cmd *cobra.Command, args []string) error {
return checkFlags()
},
Expand Down
5 changes: 5 additions & 0 deletions cmd/ecrecover/ecrecover.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"math/big"
"os"

"github.com/0xPolygon/polygon-cli/cmd/flag_loader"
"github.com/0xPolygon/polygon-cli/util"
ethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
Expand All @@ -31,6 +32,10 @@ var EcRecoverCmd = &cobra.Command{
Short: "Recovers and returns the public key of the signature",
Long: usage,
Args: cobra.NoArgs,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
rpcUrlFlagValue := flag_loader.GetRpcUrlFlagValue(cmd)
rpcUrl = *rpcUrlFlagValue
},
PreRunE: func(cmd *cobra.Command, args []string) error {
return checkFlags()
},
Expand Down
26 changes: 14 additions & 12 deletions cmd/fixnoncegap/fixnoncegap.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"
"time"

"github.com/0xPolygon/polygon-cli/cmd/flag_loader"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
Expand All @@ -20,10 +21,19 @@ import (
)

var FixNonceGapCmd = &cobra.Command{
Use: "fix-nonce-gap",
Short: "Send txs to fix the nonce gap for a specific account",
Long: fixNonceGapUsage,
Args: cobra.NoArgs,
Use: "fix-nonce-gap",
Short: "Send txs to fix the nonce gap for a specific account",
Long: fixNonceGapUsage,
Args: cobra.NoArgs,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
var err error
inputFixNonceGapArgs.rpcURL = flag_loader.GetRpcUrlFlagValue(cmd)
inputFixNonceGapArgs.privateKey, err = flag_loader.GetRequiredPrivateKeyFlagValue(cmd)
if err != nil {
return err
}
return nil
},
PreRunE: prepareRpcClient,
RunE: fixNonceGap,
SilenceUsage: true,
Expand Down Expand Up @@ -221,7 +231,6 @@ func init() {
inputFixNonceGapArgs.privateKey = FixNonceGapCmd.PersistentFlags().String(ArgPrivateKey, "", "the private key to be used when sending the txs to fix the nonce gap")
inputFixNonceGapArgs.replace = FixNonceGapCmd.PersistentFlags().Bool(ArgReplace, false, "replace the existing txs in the pool")
inputFixNonceGapArgs.maxNonce = FixNonceGapCmd.PersistentFlags().Uint64(ArgMaxNonce, 0, "when set, the max nonce will be this value instead of trying to get it from the pool")
fatalIfError(FixNonceGapCmd.MarkPersistentFlagRequired(ArgPrivateKey))
}

// Wait for the transaction to be mined
Expand Down Expand Up @@ -255,13 +264,6 @@ func WaitMineTransaction(ctx context.Context, client *ethclient.Client, tx *type
}
}

func fatalIfError(err error) {
if err == nil {
return
}
log.Fatal().Err(err).Msg("Unexpected error occurred")
}

func getMaxNonceFromTxPool(addr common.Address) (uint64, error) {
var result PoolContent
err := rpcClient.Client().Call(&result, "txpool_content")
Expand Down
56 changes: 56 additions & 0 deletions cmd/flag_loader/flag_loader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package flag_loader

import (
"fmt"
"os"

"github.com/spf13/cobra"
)

const (
rpcUrlFlagName, rpcUrlEnvVar = "rpc-url", "ETH_RPC_URL"
privateKeyFlagName, privateKeyEnvVar = "private-key", "PRIVATE_KEY"
)

func GetRpcUrlFlagValue(cmd *cobra.Command) *string {
v, _ := getFlagValue(cmd, rpcUrlFlagName, rpcUrlEnvVar, false)
return v
}

func GetRequiredRpcUrlFlagValue(cmd *cobra.Command) (*string, error) {
return getFlagValue(cmd, rpcUrlFlagName, rpcUrlEnvVar, true)
}

func GetPrivateKeyFlagValue(cmd *cobra.Command) *string {
v, _ := getFlagValue(cmd, privateKeyFlagName, privateKeyEnvVar, false)
return v
}

func GetRequiredPrivateKeyFlagValue(cmd *cobra.Command) (*string, error) {
return getFlagValue(cmd, privateKeyFlagName, privateKeyEnvVar, true)
}

func getFlagValue(cmd *cobra.Command, flagName, envVarName string, required bool) (*string, error) {
flag := cmd.Flag(flagName)
var flagValue string
if flag.Changed {
flagValue = flag.Value.String()
}
flagDefaultValue := flag.DefValue

envVarValue := os.Getenv(envVarName)

value := flagDefaultValue
if envVarValue != "" {
value = envVarValue
}
if flag.Changed {
value = flagValue
}

if required && (!flag.Changed && envVarValue == "") {
return nil, fmt.Errorf("required flag(s) \"%s\" not set", flagName)
}

return &value, nil
}
155 changes: 155 additions & 0 deletions cmd/flag_loader/flag_loader_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
package flag_loader

import (
"fmt"
"os"
"strconv"
"testing"

"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
)

func TestValuePriority(t *testing.T) {
type testCase struct {
defaultValue *int
envVarValue *int
flagValue *int
required bool

expectedValue *int
expectedError error
}

testCases := []testCase{
{
defaultValue: ptr(1),
envVarValue: ptr(2),
flagValue: ptr(3),
expectedValue: ptr(3),
required: true,
expectedError: nil,
},
{
defaultValue: ptr(1),
envVarValue: ptr(2),
flagValue: ptr(1),
expectedValue: ptr(1),
required: true,
expectedError: nil,
},
{
defaultValue: ptr(1),
envVarValue: ptr(2),
flagValue: nil,
expectedValue: ptr(2),
required: true,
expectedError: nil,
},
{
defaultValue: ptr(1),
envVarValue: nil,
flagValue: ptr(3),
expectedValue: ptr(3),
required: true,
expectedError: nil,
},
{
defaultValue: nil,
envVarValue: ptr(2),
flagValue: ptr(3),
expectedValue: ptr(3),
required: true,
expectedError: nil,
},
{
defaultValue: nil,
envVarValue: nil,
flagValue: ptr(3),
expectedValue: ptr(3),
required: true,
expectedError: nil,
},
{
defaultValue: ptr(1),
envVarValue: nil,
flagValue: nil,
expectedValue: ptr(1),
required: false,
expectedError: nil,
},
{
defaultValue: nil,
envVarValue: ptr(2),
flagValue: nil,
expectedValue: ptr(2),
required: true,
expectedError: nil,
},
{
defaultValue: nil,
envVarValue: nil,
flagValue: nil,
expectedValue: ptr(0),
required: false,
expectedError: nil,
},
{
defaultValue: nil,
envVarValue: nil,
flagValue: nil,
expectedValue: nil,
required: true,
expectedError: fmt.Errorf("required flag(s) \"flag\" not set"),
},
}

for _, tc := range testCases {
var value *int
cmd := &cobra.Command{
Use: "test",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
valueStr, err := getFlagValue(cmd, "flag", "FLAG", tc.required)
if tc.expectedError != nil {
assert.EqualError(t, err, tc.expectedError.Error())
return
}
assert.NoError(t, err)
valueInt, err := strconv.Atoi(*valueStr)
assert.NoError(t, err)
value = &valueInt
},
Run: func(cmd *cobra.Command, args []string) {
if tc.expectedValue != nil {
assert.Equal(t, *tc.expectedValue, *value)
} else {
assert.Nil(t, value)
}
},
}
if tc.defaultValue != nil {
cmd.Flags().Int("flag", *tc.defaultValue, "flag")
} else {
cmd.Flags().Int("flag", 0, "flag")
}

os.Unsetenv("FLAG")
if tc.envVarValue != nil {
v := strconv.Itoa(*tc.envVarValue)
os.Setenv("FLAG", v)
}

if tc.flagValue != nil {
v := strconv.Itoa(*tc.flagValue)
cmd.SetArgs([]string{"--flag", v})
}

err := cmd.Execute()
assert.Nil(t, err)
}

}

func ptr[T any](v T) *T {
return &v
}
5 changes: 5 additions & 0 deletions cmd/fund/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

_ "embed"

"github.com/0xPolygon/polygon-cli/cmd/flag_loader"
"github.com/0xPolygon/polygon-cli/util"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -44,6 +45,10 @@ var FundCmd = &cobra.Command{
Use: "fund",
Short: "Bulk fund crypto wallets automatically.",
Long: usage,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
params.RpcUrl = flag_loader.GetRpcUrlFlagValue(cmd)
params.PrivateKey = flag_loader.GetPrivateKeyFlagValue(cmd)
},
PreRunE: func(cmd *cobra.Command, args []string) error {
return checkFlags()
},
Expand Down
5 changes: 5 additions & 0 deletions cmd/loadtest/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"sync"
"time"

"github.com/0xPolygon/polygon-cli/cmd/flag_loader"
"github.com/0xPolygon/polygon-cli/rpctypes"
"github.com/0xPolygon/polygon-cli/util"
ethcommon "github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -166,6 +167,10 @@ var LoadtestCmd = &cobra.Command{
Short: "Run a generic load test against an Eth/EVM style JSON-RPC endpoint.",
Long: loadtestUsage,
Args: cobra.NoArgs,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
inputLoadTestParams.RPCUrl = flag_loader.GetRpcUrlFlagValue(cmd)
inputLoadTestParams.PrivateKey = flag_loader.GetPrivateKeyFlagValue(cmd)
},
PreRunE: func(cmd *cobra.Command, args []string) error {
zerolog.DurationFieldUnit = time.Second
zerolog.DurationFieldInteger = true
Expand Down
3 changes: 3 additions & 0 deletions cmd/monitor/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"sync"
"time"

"github.com/0xPolygon/polygon-cli/cmd/flag_loader"
"github.com/0xPolygon/polygon-cli/util"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -57,6 +58,8 @@ var MonitorCmd = &cobra.Command{
Args: cobra.NoArgs,
SilenceUsage: true,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
rpcUrlFlagValue := flag_loader.GetRpcUrlFlagValue(cmd)
rpcUrl = *rpcUrlFlagValue
// By default, hide logs from `polycli monitor`.
verbosityFlag := cmd.Flag("verbosity")
if verbosityFlag != nil && !verbosityFlag.Changed {
Expand Down
4 changes: 4 additions & 0 deletions cmd/nodekey/nodekey.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

_ "embed"

"github.com/0xPolygon/polygon-cli/cmd/flag_loader"
gethcrypto "github.com/ethereum/go-ethereum/crypto"
gethenode "github.com/ethereum/go-ethereum/p2p/enode"
libp2pcrypto "github.com/libp2p/go-libp2p/core/crypto"
Expand Down Expand Up @@ -65,6 +66,9 @@ var NodekeyCmd = &cobra.Command{
Use: "nodekey",
Short: "Generate node keys for different blockchain clients and protocols.",
Long: usage,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
inputNodeKeyPrivateKey = flag_loader.GetRpcUrlFlagValue(cmd)
},
RunE: func(cmd *cobra.Command, args []string) error {
var nko nodeKeyOut
var withSeed bool
Expand Down
Loading
Loading