-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
feat: add --grpc client option #14051
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d8ba54e
feat: add --grpc client option
aaronc 3c1db4d
updates
aaronc 2263135
default to TLS credentials
aaronc 42f5ca2
add min version because of gosec lint
aaronc 41d9385
Merge branch 'main' into aaronc/grpc-client-flag
tac0turtle 98c8002
Merge branch 'main' into aaronc/grpc-client-flag
tac0turtle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,17 @@ | ||
package client | ||
|
||
import ( | ||
"crypto/tls" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/pflag" | ||
"github.com/tendermint/tendermint/libs/cli" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/credentials" | ||
"google.golang.org/grpc/credentials/insecure" | ||
|
||
"github.com/cosmos/cosmos-sdk/client/flags" | ||
"github.com/cosmos/cosmos-sdk/crypto/keyring" | ||
|
@@ -147,6 +151,28 @@ func ReadPersistentCommandFlags(clientCtx Context, flagSet *pflag.FlagSet) (Cont | |
} | ||
} | ||
|
||
if clientCtx.GRPCClient == nil || flagSet.Changed(flags.FlagGRPC) { | ||
grpcURI, _ := flagSet.GetString(flags.FlagGRPC) | ||
if grpcURI != "" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's also do an early return here if grpcURI == "" { return clientCtx } |
||
var dialOpts []grpc.DialOption | ||
|
||
useInsecure, _ := flagSet.GetBool(flags.FlagGRPCInsecure) | ||
aaronc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if useInsecure { | ||
dialOpts = append(dialOpts, grpc.WithTransportCredentials(insecure.NewCredentials())) | ||
} else { | ||
dialOpts = append(dialOpts, grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{ | ||
MinVersion: tls.VersionTLS12, | ||
}))) | ||
} | ||
|
||
grpcClient, err := grpc.Dial(grpcURI, dialOpts...) | ||
if err != nil { | ||
return Context{}, err | ||
} | ||
clientCtx = clientCtx.WithGRPCClient(grpcClient) | ||
} | ||
} | ||
|
||
return clientCtx, nil | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we please invert this condition and return early if .GRPCClient != nil or if !flagSet.Changed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But we don't actually want an early return. This isn't an error condition. It follows the same pattern as the rest of the function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Early returns ease readability and reduce nesting. This code incurs 3 levels of nesting that could be trivially cut out just by early returns.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But look at the rest of the function. This is not an early return condition. I know this is the last clause in the function, but adding an early return here would just make maintenance of this function harder because it doesn't fit the general pattern in the rest of the function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left to your discretion :-)