Commit c70ca4dd authored by Matt Bell's avatar Matt Bell Committed by Juan Batiz-Benet

core/commands2: Added 'publish' command

parent d8afd9a9
package commands
import (
"errors"
"fmt"
cmds "github.com/jbenet/go-ipfs/commands"
nsys "github.com/jbenet/go-ipfs/namesys"
u "github.com/jbenet/go-ipfs/util"
)
type PublishOutput struct {
Name, Value string
}
var publishCmd = &cmds.Command{
Help: "TODO",
Run: func(res cmds.Response, req cmds.Request) {
n := req.Context().Node
args := req.Arguments()
if n.Identity == nil {
res.SetError(errors.New("Identity not loaded!"), cmds.ErrNormal)
return
}
// name := ""
ref := ""
switch len(args) {
case 2:
// name = args[0]
ref = args[1]
res.SetError(errors.New("keychains not yet implemented"), cmds.ErrNormal)
return
case 1:
// name = n.Identity.ID.String()
ref = args[0]
default:
res.SetError(fmt.Errorf("Publish expects 1 or 2 args; got %d.", len(args)), cmds.ErrClient)
}
// later, n.Keychain.Get(name).PrivKey
k := n.Identity.PrivKey()
pub := nsys.NewRoutingPublisher(n.Routing)
err := pub.Publish(k, ref)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
hash, err := k.GetPublic().Hash()
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
res.SetValue(&PublishOutput{
Name: u.Key(hash).String(),
Value: ref,
})
},
Format: func(res cmds.Response) (string, error) {
v := res.Value().(*PublishOutput)
return fmt.Sprintf("Published name %s to %s\n", v.Name, v.Value), nil
},
Type: &PublishOutput{},
}
......@@ -56,6 +56,7 @@ var rootSubcommands = map[string]*cmds.Command{
"cat": catCmd,
"ls": lsCmd,
"commands": commandsCmd,
"publish": publishCmd,
// test subcommands
// TODO: remove these when we don't need them anymore
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment