Skip to content

Commit 705154c

Browse files
authored
Merge branch 'main' into julien/cache
2 parents ccf46e8 + 0988691 commit 705154c

File tree

1 file changed

+65
-15
lines changed

1 file changed

+65
-15
lines changed

pkg/cmd/p2p.go

Lines changed: 65 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,29 @@
11
package cmd
22

33
import (
4-
"context"
4+
"encoding/json"
55
"fmt"
6+
"io"
67
"net/http"
78
"strings"
89
"text/tabwriter"
910

1011
"connectrpc.com/connect"
12+
pb "github.com/evstack/ev-node/types/pb/evnode/v1"
1113
"github.com/spf13/cobra"
1214
"google.golang.org/protobuf/types/known/emptypb"
1315

1416
rpc "github.com/evstack/ev-node/types/pb/evnode/v1/v1connect"
1517
)
1618

19+
const (
20+
flagOutput = "output"
21+
)
22+
23+
func init() {
24+
NetInfoCmd.Flags().StringP(flagOutput, "o", "text", "Output format (text|json)")
25+
}
26+
1727
// NetInfoCmd returns information about the running node via RPC
1828
var NetInfoCmd = &cobra.Command{
1929
Use: "net-info",
@@ -24,9 +34,8 @@ var NetInfoCmd = &cobra.Command{
2434
if err != nil {
2535
return fmt.Errorf("error parsing config: %w", err)
2636
}
27-
28-
// Get RPC address from config
2937
rpcAddress := nodeConfig.RPC.Address
38+
3039
if rpcAddress == "" {
3140
return fmt.Errorf("RPC address not found in node configuration")
3241
}
@@ -36,7 +45,10 @@ var NetInfoCmd = &cobra.Command{
3645
Transport: http.DefaultTransport,
3746
}
3847

39-
baseURL := fmt.Sprintf("http://%s", rpcAddress)
48+
baseURL := rpcAddress
49+
if !strings.HasPrefix(rpcAddress, "http://") && !strings.HasPrefix(rpcAddress, "https://") {
50+
baseURL = "http://" + baseURL
51+
}
4052

4153
// Create P2P client
4254
p2pClient := rpc.NewP2PServiceClient(
@@ -46,16 +58,33 @@ var NetInfoCmd = &cobra.Command{
4658

4759
// Call GetNetInfo RPC
4860
resp, err := p2pClient.GetNetInfo(
49-
context.Background(),
61+
cmd.Context(),
5062
connect.NewRequest(&emptypb.Empty{}),
5163
)
5264
if err != nil {
53-
return fmt.Errorf("error calling GetNetInfo RPC: %w", err)
65+
return fmt.Errorf("GetNetInfo RPC: %w", err)
5466
}
5567

5668
netInfo := resp.Msg.NetInfo
57-
nodeID := netInfo.Id
5869

70+
peerResp, err := p2pClient.GetPeerInfo(
71+
cmd.Context(),
72+
connect.NewRequest(&emptypb.Empty{}),
73+
)
74+
if err != nil {
75+
return fmt.Errorf("GetPeerInfo RPC: %w", err)
76+
}
77+
78+
outputFormat, err := cmd.Flags().GetString(flagOutput)
79+
if err != nil {
80+
return err
81+
}
82+
83+
if outputFormat == "json" {
84+
return formatJson(cmd.OutOrStdout(), netInfo, peerResp)
85+
}
86+
87+
nodeID := netInfo.Id
5988
out := cmd.OutOrStdout()
6089
w := tabwriter.NewWriter(out, 2, 0, 2, ' ', 0)
6190

@@ -73,14 +102,6 @@ var NetInfoCmd = &cobra.Command{
73102
}
74103

75104
fmt.Fprintf(w, "%s\n", strings.Repeat("-", 50))
76-
// Also get peer information
77-
peerResp, err := p2pClient.GetPeerInfo(
78-
context.Background(),
79-
connect.NewRequest(&emptypb.Empty{}),
80-
)
81-
if err != nil {
82-
return fmt.Errorf("error calling GetPeerInfo RPC: %w", err)
83-
}
84105

85106
// Print connected peers in a table-like format
86107
peerCount := len(peerResp.Msg.Peers)
@@ -109,3 +130,32 @@ var NetInfoCmd = &cobra.Command{
109130
return nil
110131
},
111132
}
133+
134+
func formatJson(w io.Writer, netInfo *pb.NetInfo, peerResp *connect.Response[pb.GetPeerInfoResponse]) error {
135+
type peerJSON struct {
136+
ID string `json:"id"`
137+
Address string `json:"address"`
138+
}
139+
type netInfoJSON struct {
140+
NodeID string `json:"node_id"`
141+
ListenAddresses []string `json:"listen_addresses"`
142+
Peers []peerJSON `json:"peers"`
143+
}
144+
145+
peers := make([]peerJSON, 0, len(peerResp.Msg.Peers))
146+
for _, peer := range peerResp.Msg.Peers {
147+
peers = append(peers, peerJSON{
148+
ID: peer.Id,
149+
Address: peer.Address,
150+
})
151+
}
152+
153+
out := netInfoJSON{
154+
NodeID: netInfo.Id,
155+
ListenAddresses: netInfo.ListenAddresses,
156+
Peers: peers,
157+
}
158+
enc := json.NewEncoder(w)
159+
enc.SetIndent("", " ")
160+
return enc.Encode(out)
161+
}

0 commit comments

Comments
 (0)