@cryptotaxi247 / kubo / commits / e86e7d875

extract publish func

Brian Tiger Chow committed Nov 4, 2014 at 05:58 UTC e86e7d875ed82ae36d165840a279cedf2fce535e
1 file changed +26 -15
core/commands2/publish.go
+26 -15
@@ -5,12 +5,15 @@ import (
5 "fmt"
6
7 cmds "github.com/jbenet/go-ipfs/commands"
8 + core "github.com/jbenet/go-ipfs/core"
9 + crypto "github.com/jbenet/go-ipfs/crypto"
10 nsys "github.com/jbenet/go-ipfs/namesys"
11 u "github.com/jbenet/go-ipfs/util"
12 )
13
14 type PublishOutput struct {
13 - Name, Value string
15 + Name string
16 + Value string
17 }
18
19 var publishCmd = &cmds.Command{
@@ -40,26 +43,16 @@ var publishCmd = &cmds.Command{
43 default:
44 res.SetError(fmt.Errorf("Publish expects 1 or 2 args; got %d.", len(args)), cmds.ErrClient)
45 }
43 - // later, n.Keychain.Get(name).PrivKey
44 - k := n.Identity.PrivKey()
46
46 - pub := nsys.NewRoutingPublisher(n.Routing)
47 - err := pub.Publish(k, ref)
48 - if err != nil {
49 - res.SetError(err, cmds.ErrNormal)
50 - return
51 - }
47 + // TODO n.Keychain.Get(name).PrivKey
48 + k := n.Identity.PrivKey()
49 + publishOutput, err := publish(n, k, ref)
50
53 - hash, err := k.GetPublic().Hash()
51 if err != nil {
52 res.SetError(err, cmds.ErrNormal)
53 return
54 }
58 -
59 - res.SetOutput(&PublishOutput{
60 - Name: u.Key(hash).String(),
61 - Value: ref,
62 - })
55 + res.SetOutput(publishOutput)
56 },
57 Marshallers: map[cmds.EncodingType]cmds.Marshaller{
58 cmds.Text: func(res cmds.Response) ([]byte, error) {
@@ -70,3 +63,21 @@ var publishCmd = &cmds.Command{
63 },
64 Type: &PublishOutput{},
65 }
66 +
67 +func publish(n *core.IpfsNode, k crypto.PrivKey, ref string) (*PublishOutput, error) {
68 + pub := nsys.NewRoutingPublisher(n.Routing)
69 + err := pub.Publish(k, ref)
70 + if err != nil {
71 + return nil, err
72 + }
73 +
74 + hash, err := k.GetPublic().Hash()
75 + if err != nil {
76 + return nil, err
77 + }
78 +
79 + return &PublishOutput{
80 + Name: u.Key(hash).String(),
81 + Value: ref,
82 + }, nil
83 +}