add.go 2.72 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
var addCmd = &cmds.Command{
	Options: []cmds.Option{
		cmds.Option{[]string{"recursive", "r"}, cmds.Bool},
	},
	Arguments: []cmds.Argument{
		cmds.Argument{"file", cmds.ArgFile, false, true},
	},
Brian Tiger Chow's avatar
Brian Tiger Chow committed
29 30 31 32 33 34 35 36 37
	// TODO UsageLine: "add",
	// TODO Short:     "Add an object to ipfs.",
	Help: `ipfs add <path>... - Add objects to ipfs.

    Adds contents of <path> to ipfs. Use -r to add directories.
    Note that directories are added recursively, to form the ipfs
    MerkleDAG. A smarter partial add with a staging area (like git)
    remains to be implemented.
`,
38 39 40 41
	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
42 43 44
		// opt, found := req.Option("r")
		// if r, _ := opt.(bool); found && r {
		// }
45

Brian Tiger Chow's avatar
Brian Tiger Chow committed
46 47 48 49
		readers, err := internal.ToReaders(req.Arguments())
		if err != nil {
			res.SetError(err, cmds.ErrNormal)
			return
50
		}
51

52 53 54 55 56 57 58 59 60 61
		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()
62 63 64
			if err != nil {
				res.SetError(err, cmds.ErrNormal)
				return
65
			}
66
			added = append(added, Object{Hash: k.String(), Links: nil})
67 68
		}

69
		res.SetOutput(&AddOutput{added})
70
	},
71 72
	Marshallers: map[cmds.EncodingType]cmds.Marshaller{
		cmds.Text: func(res cmds.Response) ([]byte, error) {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
73 74 75 76 77 78 79
			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)
80 81
				return []byte(s), nil
			}
82

Brian Tiger Chow's avatar
Brian Tiger Chow committed
83 84
			s := fmt.Sprintf("Added %v objects:\n", len(added))
			for _, obj := range added {
85 86 87 88
				s += fmt.Sprintf("- %s\n", obj.Hash)
			}
			return []byte(s), nil
		},
89
	},
90
	Type: &AddOutput{},
91 92
}

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

95
	dagnodes := make([]*dag.Node, 0)
96

97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
	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
116
}