Skip to content

Commit

Permalink
Status error nodes (#57)
Browse files Browse the repository at this point in the history
Status nicely displays nodes with errors

Co-authored-by: Luke Rindels <[email protected]>
  • Loading branch information
Aptimex and luker983 authored Aug 26, 2024
1 parent 84a9f79 commit 342aa4e
Showing 1 changed file with 60 additions and 6 deletions.
66 changes: 60 additions & 6 deletions src/cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"net/netip"
"strings"
"log"

"github.com/fatih/color"
"github.com/m1gwings/treedrawer/tree"
Expand Down Expand Up @@ -54,6 +53,7 @@ func (c statusCmdConfig) Run() {
relayConfig peer.Config
e2eeConfig peer.Config
children []*Node
error string
}

var err error
Expand All @@ -75,12 +75,16 @@ func (c statusCmdConfig) Run() {
// Get map of all nodes for building tree.
// Key on public key of relay interfaces.
nodes := make(map[string]Node)
e2ee_peer_list := clientConfigE2EE.GetPeers()
var errorNodes []Node
e2ee_peer_list := client.e2eeConfig.GetPeers()
for _, ep := range e2ee_peer_list {
relayConfig, e2eeConfig, err := api.ServerInfo(netip.AddrPortFrom(ep.GetApiAddr(), uint16(ApiPort)))
if err != nil {
message := "failed to fetch node's configuration as peer"
log.Printf("%s: %v", message, err)
errorNodes = append(errorNodes, Node{
peerConfig: ep,
error: err.Error(),
})

} else {
nodes[relayConfig.GetPublicKey()] = Node{
peerConfig: ep,
Expand Down Expand Up @@ -127,7 +131,7 @@ func (c statusCmdConfig) Run() {
ips := []string{}
var api string
for j, a := range c.peerConfig.GetAllowedIPs() {
if j == len(c.peerConfig.GetAllowedIPs())-1 {
if j == len(c.peerConfig.GetAllowedIPs()) - 1 {
api = a.IP.String()
} else {
ips = append(ips, a.String())
Expand All @@ -149,6 +153,56 @@ func (c statusCmdConfig) Run() {
check("could not build tree", err)
treeTraversal(&client, child)

fmt.Fprintln(color.Output)
fmt.Println()
fmt.Fprintln(color.Output, WhiteBold(t))
fmt.Println()

if len(errorNodes) > 0 {
// Display known peers that we had issues connecting to
fmt.Fprintln(color.Output, WhiteBold("Peers with Errors:"))
fmt.Println()

for _, node := range errorNodes {
ips := []string{}
var api string
for j, a := range node.peerConfig.GetAllowedIPs() {
if j == len(node.peerConfig.GetAllowedIPs()) - 1 {
api = a.IP.String()
} else {
ips = append(ips, a.String())
}
}

t = tree.NewTree(tree.NodeString(fmt.Sprintf(`server
nickname: %v
e2ee: %v...
api: %v
routes: %v
error: %v`, node.peerConfig.GetNickname(), node.peerConfig.GetPublicKey().String()[:8], api, strings.Join(ips, ","), errorWrap(node.error, 80))))
fmt.Fprintln(color.Output, WhiteBold(t))
}
}
}

func errorWrap(text string, lineWidth int) string {
words := strings.Fields(strings.TrimSpace(text))
if len(words) == 0 {
return text
}
wrapped := words[0]
spaceLeft := lineWidth - len(wrapped)
indent := len(" error: ")
for _, word := range words[1:] {
if len(word)+1 > spaceLeft {
wrapped += " \n" + strings.Repeat(" ", indent) + word
spaceLeft = lineWidth - len(word)
} else {
wrapped += " " + word
spaceLeft -= 1 + len(word)
}
}

return wrapped
}

0 comments on commit 342aa4e

Please sign in to comment.