storj/cmd/kad/main.go
Dylan Lott c77ca2b161
[WIP] MVP of Kad inspection tools (#596)
* WIP starting to wire up the kademlia CLI tool

* WIP wiring up kad cli tools

* WIP starting to wire up the kademlia CLI tool

* WIP wiring up kad cli tools

* Got everything wired up

* WIP starting to wire up the kademlia CLI tool

* WIP wiring up kad cli tools

* merge in upstream

* WIP wiring up kad cli tools

* Got everything wired up

* WIP trying to get CLI to connect

* Inspector connects to overlay now

* Some refactoring

* Linter fixes

* Linter fixes

* Switch to pkg/process instead of using rootCmd.Execute
2018-11-09 11:40:15 -07:00

93 lines
1.8 KiB
Go

package main
import (
"context"
"fmt"
"github.com/spf13/cobra"
"storj.io/storj/pkg/node"
"storj.io/storj/pkg/overlay"
"storj.io/storj/pkg/process"
"storj.io/storj/pkg/provider"
)
var (
rootCmd = &cobra.Command{
Use: "inspector",
Short: "CLI for interacting with Storj Kademlia network",
}
getNodeCmd = &cobra.Command{
Use: "get",
Short: "get node with `id`",
RunE: GetNode,
}
listNodeCmd = &cobra.Command{
Use: "list-nodes",
Short: "get all nodes in cache",
RunE: ListNodes,
}
)
// Inspector gives access to kademlia and overlay cache
type Inspector struct {
overlay overlay.Client
identity *provider.FullIdentity
}
// NewInspector returns an Inspector client
func NewInspector(address string) (*Inspector, error) {
id, err := node.NewFullIdentity(context.Background(), 12, 4)
if err != nil {
return &Inspector{}, nil
}
overlay, err := overlay.NewOverlayClient(id, address)
if err != nil {
return &Inspector{}, nil
}
return &Inspector{
overlay: overlay,
identity: id,
}, nil
}
// GetNode returns a node with the requested ID or nothing at all
func GetNode(cmd *cobra.Command, args []string) (err error) {
i, err := NewInspector("127.0.0.1:7778")
if err != nil {
fmt.Printf("error dialing inspector: %+v\n", err)
return err
}
n := node.IDFromString("testnode")
found, err := i.overlay.Lookup(context.Background(), n)
if err != nil {
return err
}
fmt.Printf("### FOUND: %+v\n", found)
return nil
}
// ListNodes returns the nodes in the cache
func ListNodes(cmd *cobra.Command, args []string) (err error) {
i, err := NewInspector("127.0.0.1:7778")
if err != nil {
fmt.Printf("error dialing inspector: %+v\n", err)
return err
}
fmt.Printf("Inspector: %+v\n", i)
return nil
}
func init() {
rootCmd.AddCommand(getNodeCmd)
rootCmd.AddCommand(listNodeCmd)
}
func main() {
process.Exec(rootCmd)
}