dag.go 4.59 KB
Newer Older
1 2 3 4 5
package dagcmd

import (
	"fmt"
	"io"
Jeromy's avatar
Jeromy committed
6
	"io/ioutil"
7 8 9
	"strings"

	cmds "github.com/ipfs/go-ipfs/commands"
10
	coredag "github.com/ipfs/go-ipfs/core/coredag"
11
	path "github.com/ipfs/go-ipfs/path"
12
	pin "github.com/ipfs/go-ipfs/pin"
13

14 15 16
	cid "gx/ipfs/QmTprEaAA2A9bst5XH7exuyi5KzNMK3SEDNN8rBDnKWcUS/go-cid"
	node "gx/ipfs/QmYNyRZJBUYPNrLszFmrBrPJbsBh2vMsefz5gnDpB5M1P6/go-ipld-format"
	ipldcbor "gx/ipfs/QmemYymP73eVdTUUMZEiSpiHeZQKNJdT5dP2iuHssZh1sR/go-ipld-cbor"
17 18 19 20
)

var DagCmd = &cmds.Command{
	Helptext: cmds.HelpText{
Jeromy's avatar
Jeromy committed
21 22 23 24 25 26 27
		Tagline: "Interact with ipld dag objects.",
		ShortDescription: `
'ipfs dag' is used for creating and manipulating dag objects.

This subcommand is currently an experimental feature, but it is intended
to deprecate and replace the existing 'ipfs object' command moving forward.
		`,
28 29 30 31 32 33 34 35 36 37 38 39 40 41
	},
	Subcommands: map[string]*cmds.Command{
		"put": DagPutCmd,
		"get": DagGetCmd,
	},
}

type OutputObject struct {
	Cid *cid.Cid
}

var DagPutCmd = &cmds.Command{
	Helptext: cmds.HelpText{
		Tagline: "Add a dag node to ipfs.",
Jeromy's avatar
Jeromy committed
42 43 44 45
		ShortDescription: `
'ipfs dag put' accepts input from a file or stdin and parses it
into an object of the specified format.
`,
46 47 48 49 50
	},
	Arguments: []cmds.Argument{
		cmds.FileArg("object data", true, false, "The object to put").EnableStdin(),
	},
	Options: []cmds.Option{
51 52
		cmds.StringOption("format", "f", "Format that the object will be added as.").Default("cbor"),
		cmds.StringOption("input-enc", "Format that the input object will be.").Default("json"),
53
		cmds.BoolOption("pin", "Pin this object when adding.").Default(false),
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
	},
	Run: func(req cmds.Request, res cmds.Response) {
		n, err := req.InvocContext().GetNode()
		if err != nil {
			res.SetError(err, cmds.ErrNormal)
			return
		}

		fi, err := req.Files().NextFile()
		if err != nil {
			res.SetError(err, cmds.ErrNormal)
			return
		}

		ienc, _, _ := req.Option("input-enc").String()
		format, _, _ := req.Option("format").String()
70 71 72 73 74
		dopin, _, err := req.Option("pin").Bool()
		if err != nil {
			res.SetError(err, cmds.ErrNormal)
			return
		}
75

76 77 78 79
		if dopin {
			defer n.Blockstore.PinLock().Unlock()
		}

80 81 82 83 84
		nds, err := coredag.ParseInputs(ienc, format, fi)
		if err != nil {
			res.SetError(err, cmds.ErrNormal)
			return
		}
85 86 87 88
		if len(nds) == 0 {
			res.SetError(fmt.Errorf("no node returned from ParseInputs"), cmds.ErrNormal)
			return
		}
89

90 91
		b := n.DAG.Batch()
		for _, nd := range nds {
92
			_, err := b.Add(nd)
Jeromy's avatar
Jeromy committed
93 94 95 96
			if err != nil {
				res.SetError(err, cmds.ErrNormal)
				return
			}
97
		}
98

99 100
		if err := b.Commit(); err != nil {
			res.SetError(err, cmds.ErrNormal)
101
			return
102
		}
103

104
		root := nds[0].Cid()
105
		if dopin {
106
			n.Pinning.PinWithMode(root, pin.Recursive)
107 108 109 110 111 112 113 114

			err := n.Pinning.Flush()
			if err != nil {
				res.SetError(err, cmds.ErrNormal)
				return
			}
		}

115
		res.SetOutput(&OutputObject{Cid: root})
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
	},
	Type: OutputObject{},
	Marshalers: cmds.MarshalerMap{
		cmds.Text: func(res cmds.Response) (io.Reader, error) {
			oobj, ok := res.Output().(*OutputObject)
			if !ok {
				return nil, fmt.Errorf("expected a different object in marshaler")
			}

			return strings.NewReader(oobj.Cid.String()), nil
		},
	},
}

var DagGetCmd = &cmds.Command{
	Helptext: cmds.HelpText{
		Tagline: "Get a dag node from ipfs.",
Jeromy's avatar
Jeromy committed
133 134 135
		ShortDescription: `
'ipfs dag get' fetches a dag node from ipfs and prints it out in the specifed format.
`,
136 137
	},
	Arguments: []cmds.Argument{
138
		cmds.StringArg("ref", true, false, "The object to get").EnableStdin(),
139 140 141 142 143 144 145 146
	},
	Run: func(req cmds.Request, res cmds.Response) {
		n, err := req.InvocContext().GetNode()
		if err != nil {
			res.SetError(err, cmds.ErrNormal)
			return
		}

147
		p, err := path.ParsePath(req.Arguments()[0])
148 149 150 151 152
		if err != nil {
			res.SetError(err, cmds.ErrNormal)
			return
		}

153
		obj, rem, err := n.Resolver.ResolveToLastNode(req.Context(), p)
154 155 156 157 158
		if err != nil {
			res.SetError(err, cmds.ErrNormal)
			return
		}

159 160 161 162 163 164 165 166 167 168 169
		var out interface{} = obj
		if len(rem) > 0 {
			final, _, err := obj.Resolve(rem)
			if err != nil {
				res.SetError(err, cmds.ErrNormal)
				return
			}
			out = final
		}

		res.SetOutput(out)
170 171 172
	},
}

173
func convertJsonToType(r io.Reader, format string) (node.Node, error) {
174 175
	switch format {
	case "cbor", "dag-cbor":
Jeromy's avatar
Jeromy committed
176
		return ipldcbor.FromJson(r)
177 178 179 180 181 182
	case "dag-pb", "protobuf":
		return nil, fmt.Errorf("protobuf handling in 'dag' command not yet implemented")
	default:
		return nil, fmt.Errorf("unknown target format: %s", format)
	}
}
Jeromy's avatar
Jeromy committed
183 184 185 186 187 188 189 190 191 192 193 194 195 196

func convertRawToType(r io.Reader, format string) (node.Node, error) {
	switch format {
	case "cbor", "dag-cbor":
		data, err := ioutil.ReadAll(r)
		if err != nil {
			return nil, err
		}

		return ipldcbor.Decode(data)
	default:
		return nil, fmt.Errorf("unsupported target format for raw input: %s", format)
	}
}