@cryptotaxi247 / kubo / commits / a7accecc8

core/commands2: Moved 'resolve' and 'publish' into subcommands of 'name'

Matt Bell committed Nov 5, 2014 at 00:51 UTC a7accecc8ebfaaf0cadc1893ec62c4341354f59b
3 files changed +75 -76
core/commands2/name.go renamed
+74
@@ -16,6 +16,18 @@ type IpnsEntry struct {
16 Value string
17 }
18
19 +type ResolveOutput struct {
20 + Entries []IpnsEntry
21 +}
22 +
23 +var nameCmd = &cmds.Command{
24 + Help: "TODO",
25 + Subcommands: map[string]*cmds.Command{
26 + "publish": publishCmd,
27 + "resolve": resolveCmd,
28 + },
29 +}
30 +
31 var publishCmd = &cmds.Command{
32 Arguments: []cmds.Argument{
33 cmds.Argument{"name", cmds.ArgString, false, false},
@@ -65,6 +77,56 @@ var publishCmd = &cmds.Command{
77 Type: &IpnsEntry{},
78 }
79
80 +var resolveCmd = &cmds.Command{
81 + Arguments: []cmds.Argument{
82 + cmds.Argument{"name", cmds.ArgString, false, true},
83 + },
84 + Run: func(res cmds.Response, req cmds.Request) {
85 + name := ""
86 + args := req.Arguments()
87 + n := req.Context().Node
88 + var output []IpnsEntry
89 +
90 + if len(args) == 0 {
91 + if n.Identity == nil {
92 + res.SetError(errors.New("Identity not loaded!"), cmds.ErrNormal)
93 + return
94 + }
95 +
96 + name = n.Identity.ID().String()
97 + entry, err := resolve(name, n)
98 + if err != nil {
99 + res.SetError(err, cmds.ErrNormal)
100 + return
101 + }
102 +
103 + output = []IpnsEntry{entry}
104 +
105 + } else {
106 + output = make([]IpnsEntry, len(args))
107 +
108 + for i, arg := range args {
109 + var ok bool
110 + name, ok = arg.(string)
111 + if !ok {
112 + res.SetError(errors.New("cast error"), cmds.ErrNormal)
113 + return
114 + }
115 +
116 + entry, err := resolve(name, n)
117 + if err != nil {
118 + res.SetError(err, cmds.ErrNormal)
119 + return
120 + }
121 + output[i] = entry
122 + }
123 + }
124 +
125 + res.SetOutput(&ResolveOutput{output})
126 + },
127 + Type: &ResolveOutput{},
128 +}
129 +
130 func publish(n *core.IpfsNode, k crypto.PrivKey, ref string) (*IpnsEntry, error) {
131 pub := nsys.NewRoutingPublisher(n.Routing)
132 err := pub.Publish(k, ref)
@@ -82,3 +144,15 @@ func publish(n *core.IpfsNode, k crypto.PrivKey, ref string) (*IpnsEntry, error)
144 Value: ref,
145 }, nil
146 }
147 +
148 +func resolve(name string, n *core.IpfsNode) (IpnsEntry, error) {
149 + resolved, err := n.Namesys.Resolve(name)
150 + if err != nil {
151 + return IpnsEntry{}, err
152 + }
153 +
154 + return IpnsEntry{
155 + Name: name,
156 + Value: resolved,
157 + }, nil
158 +}
core/commands2/resolve.go deleted
-74
@@ -1,74 +0,0 @@
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 -2
@@ -59,13 +59,12 @@ var rootSubcommands = map[string]*cmds.Command{
59 "cat": catCmd,
60 "ls": lsCmd,
61 "commands": commandsCmd,
62 - "publish": publishCmd,
62 + "name": nameCmd,
63 "add": addCmd,
64 "log": logCmd,
65 "diag": diagCmd,
66 "pin": pinCmd,
67 "unpin": unpinCmd,
68 - "resolve": resolveCmd,
68
69 // test subcommands
70 // TODO: remove these when we don't need them anymore