publish.go 979 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
package commands

import (
	"errors"
	"fmt"
	"io"

	"github.com/jbenet/go-ipfs/core"
	u "github.com/jbenet/go-ipfs/util"

	nsys "github.com/jbenet/go-ipfs/namesys"
)

func Publish(n *core.IpfsNode, args []string, opts map[string]interface{}, out io.Writer) error {
	log.Debug("Begin Publish")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
16

17 18 19 20
	if n.Identity == nil {
		return errors.New("Identity not loaded!")
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
	// name := ""
	ref := ""

	switch len(args) {
	case 2:
		// name = args[0]
		ref = args[1]
		return errors.New("keychains not yet implemented")
	case 1:
		// name = n.Identity.ID.String()
		ref = args[0]

	default:
		return fmt.Errorf("Publish expects 1 or 2 args; got %d.", len(args))
	}

	// later, n.Keychain.Get(name).PrivKey
38
	k := n.Identity.PrivKey()
39

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
40
	pub := nsys.NewRoutingPublisher(n.Routing)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
41
	err := pub.Publish(k, ref)
42 43 44 45 46 47 48 49
	if err != nil {
		return err
	}

	hash, err := k.GetPublic().Hash()
	if err != nil {
		return err
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
50
	fmt.Fprintf(out, "published name %s to %s\n", u.Key(hash), ref)
51 52 53

	return nil
}