add.go 1.82 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
package commands

import (
	"fmt"
	"io"

	cmds "github.com/jbenet/go-ipfs/commands"
	"github.com/jbenet/go-ipfs/core"
	"github.com/jbenet/go-ipfs/importer"
	dag "github.com/jbenet/go-ipfs/merkledag"
)

// Error indicating the max depth has been exceded.
var ErrDepthLimitExceeded = fmt.Errorf("depth limit exceeded")

16 17 18 19
type AddOutput struct {
	Added []Object
}

20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
var addCmd = &cmds.Command{
	Options: []cmds.Option{
		cmds.Option{[]string{"recursive", "r"}, cmds.Bool},
	},
	Arguments: []cmds.Argument{
		cmds.Argument{"file", cmds.ArgFile, false, true},
	},
	Help: "TODO",
	Run: func(res cmds.Response, req cmds.Request) {
		n := req.Context().Node

		// if recursive, set depth to reflect so
		//opt, found := req.Option("r")
		//if r, _ := opt.(bool); found && r {
		//}

36 37
		added := make([]Object, len(req.Arguments()))

38
		// add every path in args
39
		for i, arg := range req.Arguments() {
40 41 42 43
			// Add the file
			node, err := add(n, arg.(io.Reader))
			if err != nil {
				res.SetError(err, cmds.ErrNormal)
44 45 46 47 48 49 50
				return
			}

			k, err := node.Key()
			if err != nil {
				res.SetError(err, cmds.ErrNormal)
				return
51
			}
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

			added[i] = Object{k.String(), nil}
		}

		res.SetValue(&AddOutput{added})
	},
	Format: func(res cmds.Response) (string, error) {
		v := res.Value().(*AddOutput).Added
		if len(v) == 1 {
			return fmt.Sprintf("Added object: %s\n", v[0].Hash), nil
		}

		s := fmt.Sprintf("Added %v objects:\n", len(v))
		for _, obj := range v {
			s += fmt.Sprintf("- %s\n", obj.Hash)
67
		}
68
		return s, nil
69
	},
70
	Type: &AddOutput{},
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
}

func add(n *core.IpfsNode, in io.Reader) (*dag.Node, error) {
	node, err := importer.NewDagFromReader(in)
	if err != nil {
		return nil, err
	}

	// add the file to the graph + local storage
	err = n.DAG.AddRecursive(node)
	if err != nil {
		return nil, err
	}

	// ensure we keep it
	return node, n.Pinning.Pin(node, true)
}