@cryptotaxi247 / kubo / commits / c70ca4dd5

core/commands2: Added 'publish' command

Matt Bell committed Oct 31, 2014 at 16:08 UTC c70ca4dd51f10c059d03ac0708c6610324263d52
2 files changed +70
core/commands2/publish.go new
+69
@@ -0,0 +1,69 @@
1 +package commands
2 +
3 +import (
4 + "errors"
5 + "fmt"
6 +
7 + cmds "github.com/jbenet/go-ipfs/commands"
8 + nsys "github.com/jbenet/go-ipfs/namesys"
9 + u "github.com/jbenet/go-ipfs/util"
10 +)
11 +
12 +type PublishOutput struct {
13 + Name, Value string
14 +}
15 +
16 +var publishCmd = &cmds.Command{
17 + Help: "TODO",
18 + Run: func(res cmds.Response, req cmds.Request) {
19 + n := req.Context().Node
20 + args := req.Arguments()
21 +
22 + if n.Identity == nil {
23 + res.SetError(errors.New("Identity not loaded!"), cmds.ErrNormal)
24 + return
25 + }
26 +
27 + // name := ""
28 + ref := ""
29 +
30 + switch len(args) {
31 + case 2:
32 + // name = args[0]
33 + ref = args[1]
34 + res.SetError(errors.New("keychains not yet implemented"), cmds.ErrNormal)
35 + return
36 + case 1:
37 + // name = n.Identity.ID.String()
38 + ref = args[0]
39 +
40 + default:
41 + res.SetError(fmt.Errorf("Publish expects 1 or 2 args; got %d.", len(args)), cmds.ErrClient)
42 + }
43 + // later, n.Keychain.Get(name).PrivKey
44 + k := n.Identity.PrivKey()
45 +
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 + }
52 +
53 + hash, err := k.GetPublic().Hash()
54 + if err != nil {
55 + res.SetError(err, cmds.ErrNormal)
56 + return
57 + }
58 +
59 + res.SetValue(&PublishOutput{
60 + Name: u.Key(hash).String(),
61 + Value: ref,
62 + })
63 + },
64 + Format: func(res cmds.Response) (string, error) {
65 + v := res.Value().(*PublishOutput)
66 + return fmt.Sprintf("Published name %s to %s\n", v.Name, v.Value), nil
67 + },
68 + Type: &PublishOutput{},
69 +}
core/commands2/root.go
+1
@@ -56,6 +56,7 @@ var rootSubcommands = map[string]*cmds.Command{
56 "cat": catCmd,
57 "ls": lsCmd,
58 "commands": commandsCmd,
59 + "publish": publishCmd,
60
61 // test subcommands
62 // TODO: remove these when we don't need them anymore