Skip to content

Commit

Permalink
chore(p2p/flags): Handle empty p2p.network flag. (celestiaorg#3004)
Browse files Browse the repository at this point in the history
Fixes celestiaorg#3003. 
<!--
Thank you for submitting a pull request!

Please make sure you have reviewed our contributors guide before
submitting your
first PR.

Please ensure you've addressed or included references to any related
issues.

Tips:
- Use keywords like "closes" or "fixes" followed by an issue number to
automatically close related issues when the PR is merged (e.g., "closes
#123" or "fixes #123").
- Describe the changes made in the PR.
- Ensure the PR has one of the required tags (kind:fix, kind:misc,
kind:break!, kind:refactor, kind:feat, kind:deps, kind:docs, kind:ci,
kind:chore, kind:testing)

-->

---------

Co-authored-by: ramin <raminkeene@gmail.com>
  • Loading branch information
2 people authored and renaynay committed Jan 15, 2024
1 parent 94b316c commit a6eeb4d
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 17 deletions.
36 changes: 19 additions & 17 deletions nodebuilder/p2p/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ Peers must bidirectionally point to each other. (Format: multiformats.io/multiad
)
flags.String(
networkFlag,
"",
"The name of the network to connect to, e.g. "+
listProvidedNetworks()+
". Must be passed on both init and start to take effect.",
DefaultNetwork.String(),
fmt.Sprintf("The name of the network to connect to, e.g. %s. Must be passed on "+
"both init and start to take effect. Assumes mainnet (%s) unless otherwise specified.",
listProvidedNetworks(),
DefaultNetwork.String()),
)

return flags
Expand Down Expand Up @@ -67,22 +68,23 @@ func ParseFlags(
// ParseNetwork tries to parse the network from the flags and environment,
// and returns either the parsed network or the build's default network
func ParseNetwork(cmd *cobra.Command) (Network, error) {
parsed := cmd.Flag(networkFlag).Value.String()
// no network set through the flags, so check if there is an override in the env
if parsed == "" {
envNetwork, err := parseNetworkFromEnv()
// no network found in env, so use the default network
if envNetwork == "" {
return DefaultNetwork, err
}
if envNetwork, err := parseNetworkFromEnv(); envNetwork != "" {
return envNetwork, err
}
// check if user provided the actual network value
// or an alias
if net, err := Network(parsed).Validate(); err == nil {
return net, nil
parsed := cmd.Flag(networkFlag).Value.String()
switch parsed {
case "":
return "", fmt.Errorf("no network provided, allowed values: %s", listProvidedNetworks())

case DefaultNetwork.String():
return DefaultNetwork, nil

default:
if net, err := Network(parsed).Validate(); err == nil {
return net, nil
}
return "", fmt.Errorf("invalid network specified: %s, allowed values: %s", parsed, listProvidedNetworks())
}
return "", fmt.Errorf("invalid network specified: %s", parsed)
}

// parseNetworkFromEnv tries to parse the network from the environment.
Expand Down
56 changes: 56 additions & 0 deletions nodebuilder/p2p/flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,59 @@ func createCmdWithNetworkFlag() *cobra.Command {
cmd.Flags().AddFlagSet(flags)
return cmd
}

// Set empty network flag and ensure error returned
func TestParseNetwork_emptyFlag(t *testing.T) {
cmd := createCmdWithNetworkFlag()

err := cmd.Flags().Set(networkFlag, "")
require.NoError(t, err)

_, err = ParseNetwork(cmd)
assert.Error(t, err)

}

// Set empty network flag and ensure error returned
func TestParseNetwork_emptyEnvEmptyFlag(t *testing.T) {

t.Setenv(EnvCustomNetwork, "")

cmd := createCmdWithNetworkFlag()
err := cmd.Flags().Set(networkFlag, "")
require.NoError(t, err)

_, err = ParseNetwork(cmd)
require.Error(t, err)

}

// Env overrides empty flag to take precedence
func TestParseNetwork_envOverridesEmptyFlag(t *testing.T) {

t.Setenv(EnvCustomNetwork, "custom-network")

cmd := createCmdWithNetworkFlag()
err := cmd.Flags().Set(networkFlag, "")
require.NoError(t, err)

network, err := ParseNetwork(cmd)
require.NoError(t, err)
assert.Equal(t, Network("custom-network"), network)

}

// Explicitly set flag but env should still override
func TestParseNetwork_envOverridesFlag(t *testing.T) {

t.Setenv(EnvCustomNetwork, "custom-network")

cmd := createCmdWithNetworkFlag()
err := cmd.Flags().Set(networkFlag, string(Mocha))
require.NoError(t, err)

network, err := ParseNetwork(cmd)
require.NoError(t, err)
assert.Equal(t, Network("custom-network"), network)

}

0 comments on commit a6eeb4d

Please sign in to comment.