mirror of
https://github.com/ipfs/kubo.git
synced 2025-07-01 19:24:14 +08:00
extract publish func
This commit is contained in:

committed by
Juan Batiz-Benet

parent
96fd88e916
commit
e86e7d875e
@ -5,12 +5,15 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
cmds "github.com/jbenet/go-ipfs/commands"
|
cmds "github.com/jbenet/go-ipfs/commands"
|
||||||
|
core "github.com/jbenet/go-ipfs/core"
|
||||||
|
crypto "github.com/jbenet/go-ipfs/crypto"
|
||||||
nsys "github.com/jbenet/go-ipfs/namesys"
|
nsys "github.com/jbenet/go-ipfs/namesys"
|
||||||
u "github.com/jbenet/go-ipfs/util"
|
u "github.com/jbenet/go-ipfs/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
type PublishOutput struct {
|
type PublishOutput struct {
|
||||||
Name, Value string
|
Name string
|
||||||
|
Value string
|
||||||
}
|
}
|
||||||
|
|
||||||
var publishCmd = &cmds.Command{
|
var publishCmd = &cmds.Command{
|
||||||
@ -40,26 +43,16 @@ var publishCmd = &cmds.Command{
|
|||||||
default:
|
default:
|
||||||
res.SetError(fmt.Errorf("Publish expects 1 or 2 args; got %d.", len(args)), cmds.ErrClient)
|
res.SetError(fmt.Errorf("Publish expects 1 or 2 args; got %d.", len(args)), cmds.ErrClient)
|
||||||
}
|
}
|
||||||
// later, n.Keychain.Get(name).PrivKey
|
|
||||||
|
// TODO n.Keychain.Get(name).PrivKey
|
||||||
k := n.Identity.PrivKey()
|
k := n.Identity.PrivKey()
|
||||||
|
publishOutput, err := publish(n, k, ref)
|
||||||
|
|
||||||
pub := nsys.NewRoutingPublisher(n.Routing)
|
|
||||||
err := pub.Publish(k, ref)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
res.SetError(err, cmds.ErrNormal)
|
res.SetError(err, cmds.ErrNormal)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
res.SetOutput(publishOutput)
|
||||||
hash, err := k.GetPublic().Hash()
|
|
||||||
if err != nil {
|
|
||||||
res.SetError(err, cmds.ErrNormal)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
res.SetOutput(&PublishOutput{
|
|
||||||
Name: u.Key(hash).String(),
|
|
||||||
Value: ref,
|
|
||||||
})
|
|
||||||
},
|
},
|
||||||
Marshallers: map[cmds.EncodingType]cmds.Marshaller{
|
Marshallers: map[cmds.EncodingType]cmds.Marshaller{
|
||||||
cmds.Text: func(res cmds.Response) ([]byte, error) {
|
cmds.Text: func(res cmds.Response) ([]byte, error) {
|
||||||
@ -70,3 +63,21 @@ var publishCmd = &cmds.Command{
|
|||||||
},
|
},
|
||||||
Type: &PublishOutput{},
|
Type: &PublishOutput{},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func publish(n *core.IpfsNode, k crypto.PrivKey, ref string) (*PublishOutput, error) {
|
||||||
|
pub := nsys.NewRoutingPublisher(n.Routing)
|
||||||
|
err := pub.Publish(k, ref)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
hash, err := k.GetPublic().Hash()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &PublishOutput{
|
||||||
|
Name: u.Key(hash).String(),
|
||||||
|
Value: ref,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user