dag: dag resolve subcommand
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
Łukasz Magiera committed
Aug 13, 2017 at 17:59 UTC
5da8368a4228788911ab6db43debf4a915ec25f5
3 files changed
+83
-3
core/commands/dag/dag.go
+60
-2
@@ -1,6 +1,7 @@
1
package dagcmd
2
3
import (
4
+ "bytes"
5
"fmt"
6
"io"
7
"math"
@@ -26,8 +27,9 @@ to deprecate and replace the existing 'ipfs object' command moving forward.
27
`,
28
},
29
Subcommands: map[string]*cmds.Command{
29
- "put": DagPutCmd,
30
- "get": DagGetCmd,
30
+ "put": DagPutCmd,
31
+ "get": DagGetCmd,
32
+ "resolve": DagResolveCmd,
33
},
34
}
35
@@ -35,6 +37,11 @@ type OutputObject struct {
37
Cid *cid.Cid
38
}
39
40
+type ResolveOutput struct {
41
+ Cid *cid.Cid
42
+ RemPath string
43
+}
44
+
45
var DagPutCmd = &cmds.Command{
46
Helptext: cmds.HelpText{
47
Tagline: "Add a dag node to ipfs.",
@@ -183,3 +190,54 @@ var DagGetCmd = &cmds.Command{
190
res.SetOutput(out)
191
},
192
}
193
+
194
+var DagResolveCmd = &cmds.Command{
195
+ Helptext: cmds.HelpText{
196
+ Tagline: "Resolve ipld block",
197
+ ShortDescription: `
198
+'ipfs dag resolve' fetches a dag node from ipfs, prints it's address and remaining path.
199
+`,
200
+ },
201
+ Arguments: []cmds.Argument{
202
+ cmds.StringArg("ref", true, false, "The path to resolve").EnableStdin(),
203
+ },
204
+ Run: func(req cmds.Request, res cmds.Response) {
205
+ n, err := req.InvocContext().GetNode()
206
+ if err != nil {
207
+ res.SetError(err, cmds.ErrNormal)
208
+ return
209
+ }
210
+
211
+ p, err := path.ParsePath(req.Arguments()[0])
212
+ if err != nil {
213
+ res.SetError(err, cmds.ErrNormal)
214
+ return
215
+ }
216
+
217
+ obj, rem, err := n.Resolver.ResolveToLastNode(req.Context(), p)
218
+ if err != nil {
219
+ res.SetError(err, cmds.ErrNormal)
220
+ return
221
+ }
222
+
223
+ res.SetOutput(&ResolveOutput{
224
+ Cid: obj.Cid(),
225
+ RemPath: path.Join(rem),
226
+ })
227
+ },
228
+ Marshalers: cmds.MarshalerMap{
229
+ cmds.Text: func(res cmds.Response) (io.Reader, error) {
230
+ output := res.Output().(*ResolveOutput)
231
+ buf := new(bytes.Buffer)
232
+ p := output.Cid.String()
233
+ if output.RemPath != "" {
234
+ p = path.Join([]string{p, output.RemPath})
235
+ }
236
+
237
+ buf.WriteString(p)
238
+
239
+ return buf, nil
240
+ },
241
+ },
242
+ Type: ResolveOutput{},
243
+}
core/commands/root.go
+2
-1
@@ -166,7 +166,8 @@ var rootROSubcommands = map[string]*cmds.Command{
166
},
167
"dag": &cmds.Command{
168
Subcommands: map[string]*cmds.Command{
169
- "get": dag.DagGetCmd,
169
+ "get": dag.DagGetCmd,
170
+ "resolve": dag.DagResolveCmd,
171
},
172
},
173
"refs": RefsROCmd,
test/sharness/t0053-dag.sh
+21
@@ -166,6 +166,27 @@ test_dag_cmd() {
166
printf $HASH > dag_put_exp &&
167
test_cmp dag_put_exp dag_put_out
168
'
169
+
170
+ test_expect_success "prepare data for dag resolve" '
171
+ NESTED_HASH=$(echo "{\"data\":123}" | ipfs dag put) &&
172
+ HASH=$(echo "{\"obj\":{\"/\":\"${NESTED_HASH}\"}}" | ipfs dag put)
173
+ '
174
+
175
+ test_expect_success "dag resolve some things" '
176
+ ipfs dag resolve $HASH > resolve_hash &&
177
+ ipfs dag resolve ${HASH}/obj > resolve_obj &&
178
+ ipfs dag resolve ${HASH}/obj/data > resolve_data
179
+ '
180
+
181
+ test_expect_success "dag resolve output looks good" '
182
+ printf $HASH > resolve_hash_exp &&
183
+ printf $NESTED_HASH > resolve_obj_exp &&
184
+ printf $NESTED_HASH/data > resolve_data_exp &&
185
+
186
+ test_cmp resolve_hash_exp resolve_hash &&
187
+ test_cmp resolve_obj_exp resolve_obj &&
188
+ test_cmp resolve_data_exp resolve_data
189
+ '
190
}
191
192
# should work offline