-
Notifications
You must be signed in to change notification settings - Fork 246
feat: Add structured output for net-info #3029
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
Open
alpe
wants to merge
2
commits into
main
Choose a base branch
from
alex/remote_netinfo
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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,19 +1,29 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| package cmd | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "context" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "encoding/json" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "fmt" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "io" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "net/http" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "strings" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "text/tabwriter" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "connectrpc.com/connect" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pb "github.com/evstack/ev-node/types/pb/evnode/v1" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "github.com/spf13/cobra" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "google.golang.org/protobuf/types/known/emptypb" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| rpc "github.com/evstack/ev-node/types/pb/evnode/v1/v1connect" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| flagOutput = "output" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func init() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| NetInfoCmd.Flags().StringP(flagOutput, "o", "text", "Output format (text|json)") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // NetInfoCmd returns information about the running node via RPC | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var NetInfoCmd = &cobra.Command{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Use: "net-info", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -24,9 +34,8 @@ var NetInfoCmd = &cobra.Command{ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return fmt.Errorf("error parsing config: %w", err) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Get RPC address from config | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| rpcAddress := nodeConfig.RPC.Address | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if rpcAddress == "" { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return fmt.Errorf("RPC address not found in node configuration") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -36,7 +45,10 @@ var NetInfoCmd = &cobra.Command{ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Transport: http.DefaultTransport, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| baseURL := fmt.Sprintf("http://%s", rpcAddress) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| baseURL := rpcAddress | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if !strings.HasPrefix(rpcAddress, "http://") && !strings.HasPrefix(rpcAddress, "https://") { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| baseURL = "http://" + baseURL | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Create P2P client | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| p2pClient := rpc.NewP2PServiceClient( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -46,16 +58,33 @@ var NetInfoCmd = &cobra.Command{ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Call GetNetInfo RPC | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| resp, err := p2pClient.GetNetInfo( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| context.Background(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cmd.Context(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| connect.NewRequest(&emptypb.Empty{}), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return fmt.Errorf("error calling GetNetInfo RPC: %w", err) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return fmt.Errorf("GetNetInfo RPC: %w", err) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| netInfo := resp.Msg.NetInfo | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| nodeID := netInfo.Id | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| peerResp, err := p2pClient.GetPeerInfo( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cmd.Context(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| connect.NewRequest(&emptypb.Empty{}), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return fmt.Errorf("GetPeerInfo RPC: %w", err) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| outputFormat, err := cmd.Flags().GetString(flagOutput) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return err | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if outputFormat == "json" { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return formatJson(cmd.OutOrStdout(), netInfo, peerResp) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| nodeID := netInfo.Id | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| out := cmd.OutOrStdout() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| w := tabwriter.NewWriter(out, 2, 0, 2, ' ', 0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -73,14 +102,6 @@ var NetInfoCmd = &cobra.Command{ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fmt.Fprintf(w, "%s\n", strings.Repeat("-", 50)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Also get peer information | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| peerResp, err := p2pClient.GetPeerInfo( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| context.Background(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| connect.NewRequest(&emptypb.Empty{}), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return fmt.Errorf("error calling GetPeerInfo RPC: %w", err) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Print connected peers in a table-like format | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| peerCount := len(peerResp.Msg.Peers) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -109,3 +130,32 @@ var NetInfoCmd = &cobra.Command{ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return nil | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func formatJson(w io.Writer, netInfo *pb.NetInfo, peerResp *connect.Response[pb.GetPeerInfoResponse]) error { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type peerJSON struct { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ID string `json:"id"` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Address string `json:"address"` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type netInfoJSON struct { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| NodeID string `json:"node_id"` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ListenAddresses []string `json:"listen_addresses"` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Peers []peerJSON `json:"peers"` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| peers := make([]peerJSON, 0, len(peerResp.Msg.Peers)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for _, peer := range peerResp.Msg.Peers { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| peers = append(peers, peerJSON{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ID: peer.Id, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Address: peer.Address, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+134
to
+151
Contributor
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. To align with the suggested change at the call site, the function signature should be updated to accept
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| out := netInfoJSON{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| NodeID: netInfo.Id, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ListenAddresses: netInfo.ListenAddresses, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Peers: peers, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| enc := json.NewEncoder(w) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| enc.SetIndent("", " ") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return enc.Encode(out) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Oops, something went wrong.
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.
The current implementation falls back to text output for any unsupported output format. It would be more user-friendly to validate the
outputflag and return an error for unsupported values. Aswitchstatement can make this logic cleaner and more robust.Additionally, to improve the
formatJsonfunction's signature and reduce its coupling toconnecttypes, you can passpeerResp.Msg.Peersdirectly instead of the wholepeerRespobject.