dag.go 4.64 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
	path "github.com/ipfs/go-ipfs/path"
11
	pin "github.com/ipfs/go-ipfs/pin"
12

13 14 15
	ipldcbor "gx/ipfs/QmNrbCt8j9DT5W9Pmjy2SdudT9k8GpaDr4sRuFix3BXhgR/go-ipld-cbor"
	cid "gx/ipfs/QmYhQaCYEcaPPjxJX7YcPcVKkQfRy6sJ7B3XmGFk82XYdQ/go-cid"
	node "gx/ipfs/Qmb3Hm9QDFmfYuET4pu7Kyg8JV78jFa1nvZx5vnCZsK4ck/go-ipld-format"
16 17 18 19
)

var DagCmd = &cmds.Command{
	Helptext: cmds.HelpText{
Jeromy's avatar
Jeromy committed
20 21 22 23 24 25 26
		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.
		`,
27 28 29 30 31 32 33 34 35 36 37 38 39 40
	},
	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
41 42 43 44
		ShortDescription: `
'ipfs dag put' accepts input from a file or stdin and parses it
into an object of the specified format.
`,
45 46 47 48 49
	},
	Arguments: []cmds.Argument{
		cmds.FileArg("object data", true, false, "The object to put").EnableStdin(),
	},
	Options: []cmds.Option{
50 51
		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"),
52
		cmds.BoolOption("pin", "Pin this object when adding.").Default(false),
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
	},
	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()
69 70 71 72 73
		dopin, _, err := req.Option("pin").Bool()
		if err != nil {
			res.SetError(err, cmds.ErrNormal)
			return
		}
74

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

		var c *cid.Cid
80 81
		switch ienc {
		case "json":
82
			nd, err := convertJsonToType(fi, format)
83 84 85 86 87
			if err != nil {
				res.SetError(err, cmds.ErrNormal)
				return
			}

88
			c, err = n.DAG.Add(nd)
89 90 91 92
			if err != nil {
				res.SetError(err, cmds.ErrNormal)
				return
			}
Jeromy's avatar
Jeromy committed
93 94 95 96 97 98 99
		case "raw":
			nd, err := convertRawToType(fi, format)
			if err != nil {
				res.SetError(err, cmds.ErrNormal)
				return
			}

100
			c, err = n.DAG.Add(nd)
Jeromy's avatar
Jeromy committed
101 102 103 104
			if err != nil {
				res.SetError(err, cmds.ErrNormal)
				return
			}
105 106 107
		default:
			res.SetError(fmt.Errorf("unrecognized input encoding: %s", ienc), cmds.ErrNormal)
			return
108
		}
109 110 111 112 113 114 115 116 117 118 119 120

		if dopin {
			n.Pinning.PinWithMode(c, pin.Recursive)

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

		res.SetOutput(&OutputObject{Cid: c})
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
	},
	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
138 139 140
		ShortDescription: `
'ipfs dag get' fetches a dag node from ipfs and prints it out in the specifed format.
`,
141 142
	},
	Arguments: []cmds.Argument{
143
		cmds.StringArg("ref", true, false, "The object to get").EnableStdin(),
144 145 146 147 148 149 150 151
	},
	Run: func(req cmds.Request, res cmds.Response) {
		n, err := req.InvocContext().GetNode()
		if err != nil {
			res.SetError(err, cmds.ErrNormal)
			return
		}

152
		p, err := path.ParsePath(req.Arguments()[0])
153 154 155 156 157
		if err != nil {
			res.SetError(err, cmds.ErrNormal)
			return
		}

158
		obj, rem, err := n.Resolver.ResolveToLastNode(req.Context(), p)
159 160 161 162 163
		if err != nil {
			res.SetError(err, cmds.ErrNormal)
			return
		}

164 165 166 167 168 169 170 171 172 173 174
		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)
175 176 177
	},
}

178
func convertJsonToType(r io.Reader, format string) (node.Node, error) {
179 180
	switch format {
	case "cbor", "dag-cbor":
Jeromy's avatar
Jeromy committed
181
		return ipldcbor.FromJson(r)
182 183 184 185 186 187
	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
188 189 190 191 192 193 194 195 196 197 198 199 200 201

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)
	}
}