publish.go 1.42 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
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]
33
			ref = args[1].(string)
34 35 36 37
			res.SetError(errors.New("keychains not yet implemented"), cmds.ErrNormal)
			return
		case 1:
			// name = n.Identity.ID.String()
38
			ref = args[0].(string)
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69

		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{},
}