core/commands2: Added 'resolve' command
Matt Bell committed
Nov 4, 2014 at 23:46 UTC
bbca4452982b785933ed924b78b82c5cbb2b8a9d
2 files changed
+75
core/commands2/resolve.go
new
+74
@@ -0,0 +1,74 @@
1
+package commands
2
+
3
+import (
4
+ "errors"
5
+
6
+ cmds "github.com/jbenet/go-ipfs/commands"
7
+ "github.com/jbenet/go-ipfs/core"
8
+)
9
+
10
+type ResolveOutput struct {
11
+ Entries []IpnsEntry
12
+}
13
+
14
+var resolveCmd = &cmds.Command{
15
+ Arguments: []cmds.Argument{
16
+ cmds.Argument{"name", cmds.ArgString, false, true},
17
+ },
18
+ Run: func(res cmds.Response, req cmds.Request) {
19
+ name := ""
20
+ args := req.Arguments()
21
+ n := req.Context().Node
22
+ var output []IpnsEntry
23
+
24
+ if len(args) == 0 {
25
+ if n.Identity == nil {
26
+ res.SetError(errors.New("Identity not loaded!"), cmds.ErrNormal)
27
+ return
28
+ }
29
+
30
+ name = n.Identity.ID().String()
31
+ entry, err := resolve(name, n)
32
+ if err != nil {
33
+ res.SetError(err, cmds.ErrNormal)
34
+ return
35
+ }
36
+
37
+ output = []IpnsEntry{entry}
38
+
39
+ } else {
40
+ output = make([]IpnsEntry, len(args))
41
+
42
+ for i, arg := range args {
43
+ var ok bool
44
+ name, ok = arg.(string)
45
+ if !ok {
46
+ res.SetError(errors.New("cast error"), cmds.ErrNormal)
47
+ return
48
+ }
49
+
50
+ entry, err := resolve(name, n)
51
+ if err != nil {
52
+ res.SetError(err, cmds.ErrNormal)
53
+ return
54
+ }
55
+ output[i] = entry
56
+ }
57
+ }
58
+
59
+ res.SetOutput(&ResolveOutput{output})
60
+ },
61
+ Type: &ResolveOutput{},
62
+}
63
+
64
+func resolve(name string, n *core.IpfsNode) (IpnsEntry, error) {
65
+ resolved, err := n.Namesys.Resolve(name)
66
+ if err != nil {
67
+ return IpnsEntry{}, err
68
+ }
69
+
70
+ return IpnsEntry{
71
+ Name: name,
72
+ Value: resolved,
73
+ }, nil
74
+}
core/commands2/root.go
+1
@@ -65,6 +65,7 @@ var rootSubcommands = map[string]*cmds.Command{
65
"diag": diagCmd,
66
"pin": pinCmd,
67
"unpin": unpinCmd,
68
+ "resolve": resolveCmd,
69
70
// test subcommands
71
// TODO: remove these when we don't need them anymore