Skip to content

Commit

Permalink
feat(client): add support to the ask command for multiple request types
Browse files Browse the repository at this point in the history
  • Loading branch information
aschmahmann committed Aug 30, 2022
1 parent 907984e commit 14f45a8
Show file tree
Hide file tree
Showing 2 changed files with 214 additions and 9 deletions.
142 changes: 140 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,53 @@ import (
"context"
"fmt"
"log"
"strings"
"time"

pbuf "github.com/gogo/protobuf/proto"

"github.com/ipfs/go-cid"
"github.com/ipfs/go-delegated-routing/client"
"github.com/ipfs/go-ipns"
ipns_pb "github.com/ipfs/go-ipns/pb"
"github.com/ipld/go-ipld-prime/codec/dagjson"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/multiformats/go-multiaddr"

drp "github.com/ipfs/go-delegated-routing/gen/proto"
)

func ask(ctx context.Context, c cid.Cid, endpoint string) error {
func identify(ctx context.Context, endpoint string, prettyOutput bool) error {
ic, err := drp.New_DelegatedRouting_Client(endpoint)
if err != nil {
return err
}

respCh, err := ic.Identify_Async(ctx, &drp.DelegatedRouting_IdentifyArg{})
for r := range respCh {
if r.Err != nil {
log.Println(r.Err)
continue
}

if !prettyOutput {
var buf bytes.Buffer
if err := dagjson.Encode(r.Resp, &buf); err != nil {
return err
}
fmt.Println(buf.String())
} else {
var methods []string
for _, m := range r.Resp.Methods {
methods = append(methods, string(m))
}
fmt.Println(strings.Join(methods, ","))
}
}
return nil
}

func findprovs(ctx context.Context, c cid.Cid, endpoint string, prettyOutput bool) error {
ic, err := drp.New_DelegatedRouting_Client(endpoint)
if err != nil {
return err
Expand All @@ -25,15 +64,114 @@ func ask(ctx context.Context, c cid.Cid, endpoint string) error {
return err
}
for r := range respCh {
var buf bytes.Buffer
if r.Err != nil {
log.Println(r.Err)
continue
}

if !prettyOutput {
var buf bytes.Buffer
if err := dagjson.Encode(r.Resp, &buf); err != nil {
return err
}
fmt.Println(buf.String())
} else {
for _, prov := range r.Resp.Providers {
if prov.ProviderNode.Peer != nil {
ai := &peer.AddrInfo{}
ai.ID = peer.ID(prov.ProviderNode.Peer.ID)
for _, bma := range prov.ProviderNode.Peer.Multiaddresses {
ma, err := multiaddr.NewMultiaddrBytes(bma)
if err != nil {
return err
}
ai.Addrs = append(ai.Addrs, ma)
}
fmt.Println(ai)
}
for _, proto := range prov.ProviderProto {
if proto.Bitswap != nil {
fmt.Println("\t Bitswap")
} else if proto.GraphSyncFILv1 != nil {
fmt.Println("\t GraphSyncFILv1")
var buf bytes.Buffer
if err := dagjson.Encode(proto.GraphSyncFILv1, &buf); err != nil {
return err
}
fmt.Println("\t\t" + buf.String())
} else {
var buf bytes.Buffer
if err := dagjson.Encode(proto, &buf); err != nil {
return err
}
fmt.Println("\t" + buf.String())
}
}
}
}
}
return nil
}

func getIPNS(ctx context.Context, p peer.ID, endpoint string, prettyOutput bool) error {
ic, err := drp.New_DelegatedRouting_Client(endpoint)
if err != nil {
return err
}

if prettyOutput {
c := client.NewClient(ic)
respCh, err := c.GetIPNSAsync(ctx, []byte(p))
if err != nil {
return err
}
for r := range respCh {
if r.Err != nil {
log.Println(r.Err)
continue
}
rec := new(ipns_pb.IpnsEntry)
if err := pbuf.Unmarshal(r.Record, rec); err != nil {
return err
}
seqno := rec.GetSequence()
ttl := time.Duration(rec.GetTtl())
eol, err := ipns.GetEOL(rec)
if err != nil {
return err
}
value := string(rec.GetValue())
fmt.Printf("Sequence: %d, TTL: %v, EOL: %v, Value: %s\n", seqno, ttl, eol, value)
}
return nil
}

respCh, err := ic.GetIPNS_Async(ctx, &drp.GetIPNSRequest{
ID: []byte(p),
})
if err != nil {
return err
}
for r := range respCh {
if r.Err != nil {
log.Println(r.Err)
continue
}
var buf bytes.Buffer
if err := dagjson.Encode(r.Resp, &buf); err != nil {
return err
}
fmt.Println(buf.String())
}
return nil
}

func putIPNS(ctx context.Context, key peer.ID, record []byte, endpoint string) error {
ic, err := drp.New_DelegatedRouting_Client(endpoint)
if err != nil {
return err
}

c := client.NewClient(ic)
return c.PutIPNS(ctx, []byte(key), record)
}
81 changes: 74 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package main

import (
"errors"
"log"
"os"

"github.com/urfave/cli/v2"

"github.com/ipfs/go-cid"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/multiformats/go-multibase"
)

const devIndexerEndpoint = "https://dev.cid.contact/reframe"
Expand Down Expand Up @@ -42,14 +45,78 @@ func main() {
Usage: "Reframe endpoint",
Value: devIndexerEndpoint,
},
&cli.BoolFlag{
Name: "pretty",
Usage: "output data in a prettier format that may convey less information",
Value: false,
},
},
Action: func(ctx *cli.Context) error {
cstr := ctx.Args().Get(0)
c, err := cid.Parse(cstr)
if err != nil {
return err
}
return ask(ctx.Context, c, ctx.String("endpoint"))
Subcommands: []*cli.Command{
{
Name: "identify",
UsageText: "Find which methods are supported by the reframe endpoint",
Action: func(ctx *cli.Context) error {
if ctx.NArg() != 0 {
return errors.New("invalid command, see help")
}
return identify(ctx.Context, ctx.String("endpoint"), ctx.Bool("pretty"))
},
},
{
Name: "findprovs",
Usage: "findprovs <cid>",
UsageText: "Find providers of a given CID",
Action: func(ctx *cli.Context) error {
if ctx.NArg() != 1 {
return errors.New("invalid command, see help")
}
cstr := ctx.Args().Get(0)
c, err := cid.Parse(cstr)
if err != nil {
return err
}
return findprovs(ctx.Context, c, ctx.String("endpoint"), ctx.Bool("pretty"))
},
},
{
Name: "getipns",
Usage: "getipns <ipns-id>",
UsageText: "Get the value of an IPNS ID",
Action: func(ctx *cli.Context) error {
if ctx.NArg() != 1 {
return errors.New("invalid command, see help")
}
pstr := ctx.Args().Get(0)
p, err := peer.Decode(pstr)
if err != nil {
return err
}
return getIPNS(ctx.Context, p, ctx.String("endpoint"), ctx.Bool("pretty"))
},
},
{
Name: "putipns",
Usage: "putipns <ipns-id> <multibase-encoded-record>",
UsageText: "Put an IPNS record",
Flags: []cli.Flag{
},
Action: func(ctx *cli.Context) error {
if ctx.NArg() != 2 {
return errors.New("invalid command, see help")
}
pstr := ctx.Args().Get(0)
p, err := peer.Decode(pstr)
if err != nil {
return err
}
recordStr := ctx.Args().Get(1)
_, recBytes, err := multibase.Decode(recordStr)
if err != nil {
return err
}
return putIPNS(ctx.Context, p, recBytes, ctx.String("endpoint"))
},
},
},
},
},
Expand Down

0 comments on commit 14f45a8

Please sign in to comment.