dag.go 5.79 KB
Newer Older
1 2 3
package dagcmd

import (
Łukasz Magiera's avatar
Łukasz Magiera committed
4
	"bytes"
5 6
	"fmt"
	"io"
7
	"math"
8 9 10
	"strings"

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

15
	cid "gx/ipfs/QmTprEaAA2A9bst5XH7exuyi5KzNMK3SEDNN8rBDnKWcUS/go-cid"
16
	mh "gx/ipfs/QmU9a9NV9RdPNwZQDYd5uKsm6N6LJLSvLbywDDYFbaaC6P/go-multihash"
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
	},
	Subcommands: map[string]*cmds.Command{
Łukasz Magiera's avatar
Łukasz Magiera committed
30 31 32
		"put":     DagPutCmd,
		"get":     DagGetCmd,
		"resolve": DagResolveCmd,
33 34 35
	},
}

Łukasz Magiera's avatar
Łukasz Magiera committed
36
// OutputObject is the output type of 'dag put' command
37 38 39 40
type OutputObject struct {
	Cid *cid.Cid
}

Łukasz Magiera's avatar
Łukasz Magiera committed
41
// ResolveOutput is the output type of 'dag resolve' command
Łukasz Magiera's avatar
Łukasz Magiera committed
42 43 44 45 46
type ResolveOutput struct {
	Cid     *cid.Cid
	RemPath string
}

47 48 49
var DagPutCmd = &cmds.Command{
	Helptext: cmds.HelpText{
		Tagline: "Add a dag node to ipfs.",
Jeromy's avatar
Jeromy committed
50 51 52 53
		ShortDescription: `
'ipfs dag put' accepts input from a file or stdin and parses it
into an object of the specified format.
`,
54 55 56 57 58
	},
	Arguments: []cmds.Argument{
		cmds.FileArg("object data", true, false, "The object to put").EnableStdin(),
	},
	Options: []cmds.Option{
59 60
		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"),
61
		cmds.BoolOption("pin", "Pin this object when adding.").Default(false),
62
		cmds.StringOption("hash", "Hash function to use").Default(""),
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
	},
	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()
79
		hash, _, err := req.Option("hash").String()
80 81 82 83 84
		dopin, _, err := req.Option("pin").Bool()
		if err != nil {
			res.SetError(err, cmds.ErrNormal)
			return
		}
85

86 87 88 89 90 91 92 93 94 95 96 97 98
		// mhType tells inputParser which hash should be used. MaxUint64 means 'use
		// default hash' (sha256 for cbor, sha1 for git..)
		mhType := uint64(math.MaxUint64)

		if hash != "" {
			var ok bool
			mhType, ok = mh.Names[hash]
			if !ok {
				res.SetError(fmt.Errorf("%s in not a valid multihash name", hash), cmds.ErrNormal)
				return
			}
		}

99 100 101 102
		if dopin {
			defer n.Blockstore.PinLock().Unlock()
		}

103
		nds, err := coredag.ParseInputs(ienc, format, fi, mhType, -1)
104 105 106 107
		if err != nil {
			res.SetError(err, cmds.ErrNormal)
			return
		}
108 109 110 111
		if len(nds) == 0 {
			res.SetError(fmt.Errorf("no node returned from ParseInputs"), cmds.ErrNormal)
			return
		}
112

113 114
		b := n.DAG.Batch()
		for _, nd := range nds {
115
			_, err := b.Add(nd)
Jeromy's avatar
Jeromy committed
116 117 118 119
			if err != nil {
				res.SetError(err, cmds.ErrNormal)
				return
			}
120
		}
121

122 123
		if err := b.Commit(); err != nil {
			res.SetError(err, cmds.ErrNormal)
124
			return
125
		}
126

127
		root := nds[0].Cid()
128
		if dopin {
129
			n.Pinning.PinWithMode(root, pin.Recursive)
130 131 132 133 134 135 136 137

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

138
		res.SetOutput(&OutputObject{Cid: root})
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
	},
	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
156 157 158
		ShortDescription: `
'ipfs dag get' fetches a dag node from ipfs and prints it out in the specifed format.
`,
159 160
	},
	Arguments: []cmds.Argument{
161
		cmds.StringArg("ref", true, false, "The object to get").EnableStdin(),
162 163 164 165 166 167 168 169
	},
	Run: func(req cmds.Request, res cmds.Response) {
		n, err := req.InvocContext().GetNode()
		if err != nil {
			res.SetError(err, cmds.ErrNormal)
			return
		}

170
		p, err := path.ParsePath(req.Arguments()[0])
171 172 173 174 175
		if err != nil {
			res.SetError(err, cmds.ErrNormal)
			return
		}

176
		obj, rem, err := n.Resolver.ResolveToLastNode(req.Context(), p)
177 178 179 180 181
		if err != nil {
			res.SetError(err, cmds.ErrNormal)
			return
		}

182 183 184 185 186 187 188 189 190 191 192
		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)
193 194
	},
}
Łukasz Magiera's avatar
Łukasz Magiera committed
195

Łukasz Magiera's avatar
Łukasz Magiera committed
196
// DagResolveCmd returns address of highest block within a path and a path remainder
Łukasz Magiera's avatar
Łukasz Magiera committed
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
var DagResolveCmd = &cmds.Command{
	Helptext: cmds.HelpText{
		Tagline: "Resolve ipld block",
		ShortDescription: `
'ipfs dag resolve' fetches a dag node from ipfs, prints it's address and remaining path.
`,
	},
	Arguments: []cmds.Argument{
		cmds.StringArg("ref", true, false, "The path to resolve").EnableStdin(),
	},
	Run: func(req cmds.Request, res cmds.Response) {
		n, err := req.InvocContext().GetNode()
		if err != nil {
			res.SetError(err, cmds.ErrNormal)
			return
		}

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

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

		res.SetOutput(&ResolveOutput{
			Cid:     obj.Cid(),
			RemPath: path.Join(rem),
		})
	},
	Marshalers: cmds.MarshalerMap{
		cmds.Text: func(res cmds.Response) (io.Reader, error) {
			output := res.Output().(*ResolveOutput)
			buf := new(bytes.Buffer)
			p := output.Cid.String()
			if output.RemPath != "" {
				p = path.Join([]string{p, output.RemPath})
			}

			buf.WriteString(p)

			return buf, nil
		},
	},
	Type: ResolveOutput{},
}