implement a simple wantlist command to allow the user to view their wantlist
Jeromy committed
Feb 2, 2015 at 02:48 UTC
559a24156677e0f4899b71c6f04f2c42657b5a58
5 files changed
+50
core/commands/root.go
+1
@@ -98,6 +98,7 @@ var rootSubcommands = map[string]*cmds.Command{
98
"swarm": SwarmCmd,
99
"update": UpdateCmd,
100
"version": VersionCmd,
101
+ "wantlist": WantlistCmd,
102
}
103
104
func init() {
core/commands/wantlist.go
new
+34
@@ -0,0 +1,34 @@
1
+package commands
2
+
3
+import cmds "github.com/jbenet/go-ipfs/commands"
4
+
5
+var WantlistCmd = &cmds.Command{
6
+ Helptext: cmds.HelpText{
7
+ Tagline: "A set of commands to work with the bitswap wantlist",
8
+ ShortDescription: ``,
9
+ },
10
+ Subcommands: map[string]*cmds.Command{
11
+ "show": showWantlistCmd,
12
+ },
13
+}
14
+
15
+var showWantlistCmd = &cmds.Command{
16
+ Helptext: cmds.HelpText{
17
+ Tagline: "Show blocks currently on the wantlist",
18
+ ShortDescription: `
19
+Print out all blocks currently on the bitswap wantlist for the local peer`,
20
+ },
21
+ Type: KeyList{},
22
+ Run: func(req cmds.Request, res cmds.Response) {
23
+ nd, err := req.Context().GetNode()
24
+ if err != nil {
25
+ res.SetError(err, cmds.ErrNormal)
26
+ return
27
+ }
28
+
29
+ res.SetOutput(&KeyList{nd.Exchange.GetWantlist()})
30
+ },
31
+ Marshalers: cmds.MarshalerMap{
32
+ cmds.Text: KeyListTextMarshaler,
33
+ },
34
+}
exchange/bitswap/bitswap.go
+8
@@ -402,3 +402,11 @@ func (bs *bitswap) send(ctx context.Context, p peer.ID, m bsmsg.BitSwapMessage)
402
func (bs *bitswap) Close() error {
403
return bs.process.Close()
404
}
405
+
406
+func (bs *bitswap) GetWantlist() []u.Key {
407
+ var out []u.Key
408
+ for _, e := range bs.wantlist.Entries() {
409
+ out = append(out, e.Key)
410
+ }
411
+ return out
412
+}
exchange/interface.go
+2
@@ -21,5 +21,7 @@ type Interface interface {
21
// available on the network?
22
HasBlock(context.Context, *blocks.Block) error
23
24
+ GetWantlist() []u.Key
25
+
26
io.Closer
27
}
exchange/offline/offline.go
+5
@@ -66,3 +66,8 @@ func (e *offlineExchange) GetBlocks(ctx context.Context, ks []u.Key) (<-chan *bl
66
}()
67
return out, nil
68
}
69
+
70
+// implement Exchange
71
+func (e *offlineExchange) GetWantlist() []u.Key {
72
+ return nil
73
+}