add.go 2.39 KB
Newer Older
1 2 3
package commands

import (
4
	"errors"
5 6 7 8
	"fmt"
	"io"

	cmds "github.com/jbenet/go-ipfs/commands"
9
	core "github.com/jbenet/go-ipfs/core"
Brian Tiger Chow's avatar
Brian Tiger Chow committed
10
	internal "github.com/jbenet/go-ipfs/core/commands2/internal"
11
	importer "github.com/jbenet/go-ipfs/importer"
12 13 14 15 16 17
	dag "github.com/jbenet/go-ipfs/merkledag"
)

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

18 19 20 21
type AddOutput struct {
	Added []Object
}

22 23 24 25 26 27 28 29 30 31 32 33
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
Brian Tiger Chow's avatar
fmt  
Brian Tiger Chow committed
34 35 36
		// opt, found := req.Option("r")
		// if r, _ := opt.(bool); found && r {
		// }
37

Brian Tiger Chow's avatar
Brian Tiger Chow committed
38 39 40 41
		readers, err := internal.ToReaders(req.Arguments())
		if err != nil {
			res.SetError(err, cmds.ErrNormal)
			return
42
		}
43

44 45 46 47 48 49 50 51 52 53
		dagnodes, err := add(n, readers)
		if err != nil {
			res.SetError(errors.New("cast error"), cmds.ErrNormal)
			return
		}

		added := make([]Object, len(req.Arguments()))
		for _, dagnode := range dagnodes {

			k, err := dagnode.Key()
54 55 56
			if err != nil {
				res.SetError(err, cmds.ErrNormal)
				return
57
			}
58
			added = append(added, Object{Hash: k.String(), Links: nil})
59 60
		}

61
		res.SetOutput(&AddOutput{added})
62
	},
63 64
	Marshallers: map[cmds.EncodingType]cmds.Marshaller{
		cmds.Text: func(res cmds.Response) ([]byte, error) {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
65 66 67 68 69 70 71
			val, ok := res.Output().(*AddOutput)
			if !ok {
				return nil, errors.New("cast err")
			}
			added := val.Added
			if len(added) == 1 {
				s := fmt.Sprintf("Added object: %s\n", added[0].Hash)
72 73
				return []byte(s), nil
			}
74

Brian Tiger Chow's avatar
Brian Tiger Chow committed
75 76
			s := fmt.Sprintf("Added %v objects:\n", len(added))
			for _, obj := range added {
77 78 79 80
				s += fmt.Sprintf("- %s\n", obj.Hash)
			}
			return []byte(s), nil
		},
81
	},
82
	Type: &AddOutput{},
83 84
}

85
func add(n *core.IpfsNode, readers []io.Reader) ([]*dag.Node, error) {
86

87
	dagnodes := make([]*dag.Node, 0)
88

89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
	for _, reader := range readers {
		node, err := importer.NewDagFromReader(reader)
		if err != nil {
			return nil, err
		}

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

		err = n.Pinning.Pin(node, true) // ensure we keep it
		if err != nil {
			return nil, err
		}

		dagnodes = append(dagnodes, node)
	}
	return dagnodes, nil
108
}