@cryptotaxi247 / kubo / commits / b420ba1e4

core/commands2: Added preliminary 'add' command

Matt Bell committed Nov 2, 2014 at 18:49 UTC b420ba1e49f1207597a747087a7a28a354e2cf2d
2 files changed +59
core/commands2/add.go new
+58
@@ -0,0 +1,58 @@
1 +package commands
2 +
3 +import (
4 + "fmt"
5 + "io"
6 +
7 + cmds "github.com/jbenet/go-ipfs/commands"
8 + "github.com/jbenet/go-ipfs/core"
9 + "github.com/jbenet/go-ipfs/importer"
10 + dag "github.com/jbenet/go-ipfs/merkledag"
11 +)
12 +
13 +// Error indicating the max depth has been exceded.
14 +var ErrDepthLimitExceeded = fmt.Errorf("depth limit exceeded")
15 +
16 +var addCmd = &cmds.Command{
17 + Options: []cmds.Option{
18 + cmds.Option{[]string{"recursive", "r"}, cmds.Bool},
19 + },
20 + Arguments: []cmds.Argument{
21 + cmds.Argument{"file", cmds.ArgFile, false, true},
22 + },
23 + Help: "TODO",
24 + Run: func(res cmds.Response, req cmds.Request) {
25 + n := req.Context().Node
26 +
27 + // if recursive, set depth to reflect so
28 + //opt, found := req.Option("r")
29 + //if r, _ := opt.(bool); found && r {
30 + //}
31 +
32 + // add every path in args
33 + for _, arg := range req.Arguments() {
34 + // Add the file
35 + node, err := add(n, arg.(io.Reader))
36 + if err != nil {
37 + res.SetError(err, cmds.ErrNormal)
38 + }
39 + fmt.Println(node.Key())
40 + }
41 + },
42 +}
43 +
44 +func add(n *core.IpfsNode, in io.Reader) (*dag.Node, error) {
45 + node, err := importer.NewDagFromReader(in)
46 + if err != nil {
47 + return nil, err
48 + }
49 +
50 + // add the file to the graph + local storage
51 + err = n.DAG.AddRecursive(node)
52 + if err != nil {
53 + return nil, err
54 + }
55 +
56 + // ensure we keep it
57 + return node, n.Pinning.Pin(node, true)
58 +}
core/commands2/root.go
+1
@@ -57,6 +57,7 @@ var rootSubcommands = map[string]*cmds.Command{
57 "ls": lsCmd,
58 "commands": commandsCmd,
59 "publish": publishCmd,
60 + "add": addCmd,
61
62 // test subcommands
63 // TODO: remove these when we don't need them anymore