publish.go 3.61 KB
Newer Older
1 2 3 4 5
package commands

import (
	"errors"
	"fmt"
6 7
	"io"
	"strings"
8

9 10
	context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"

11
	key "github.com/ipfs/go-ipfs/blocks/key"
12 13 14
	cmds "github.com/ipfs/go-ipfs/commands"
	core "github.com/ipfs/go-ipfs/core"
	crypto "github.com/ipfs/go-ipfs/p2p/crypto"
Etienne Laurin's avatar
Etienne Laurin committed
15
	path "github.com/ipfs/go-ipfs/path"
16 17 18 19
)

var errNotOnline = errors.New("This command must be run in online mode. Try running 'ipfs daemon' first.")

20
var PublishCmd = &cmds.Command{
21 22 23 24 25 26 27 28 29
	Helptext: cmds.HelpText{
		Tagline: "Publish an object to IPNS",
		ShortDescription: `
IPNS is a PKI namespace, where names are the hashes of public keys, and
the private key enables publishing new (signed) values. In publish, the
default value of <name> is your own identity public key.
`,
		LongDescription: `
IPNS is a PKI namespace, where names are the hashes of public keys, and
30 31
the private key enables publishing new (signed) values. In publish, the
default value of <name> is your own identity public key.
32 33 34

Examples:

Etienne Laurin's avatar
Etienne Laurin committed
35
Publish an <ipfs-path> to your identity name:
36

Etienne Laurin's avatar
Etienne Laurin committed
37
  > ipfs name publish /ipfs/QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy
38
  Published to QmbCMUZw6JFeZ7Wp9jkzbye3Fzp2GGcPgC3nmeUjfVF87n: /ipfs/QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy
39

Etienne Laurin's avatar
Etienne Laurin committed
40
Publish an <ipfs-path> to another public key (not implemented):
41

42 43 44
  > ipfs name publish /ipfs/QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy QmbCMUZw6JFeZ7Wp9jkzbye3Fzp2GGcPgC3nmeUjfVF87n
  Published to QmbCMUZw6JFeZ7Wp9jkzbye3Fzp2GGcPgC3nmeUjfVF87n: /ipfs/QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy

45
`,
46
	},
47

48
	Arguments: []cmds.Argument{
49
		cmds.StringArg("name", false, false, "The IPNS name to publish to. Defaults to your node's peerID"),
50
		cmds.StringArg("ipfs-path", true, false, "IPFS path of the obejct to be published at <name>").EnableStdin(),
51
	},
52
	Run: func(req cmds.Request, res cmds.Response) {
53
		log.Debug("Begin Publish")
54 55
		n, err := req.Context().GetNode()
		if err != nil {
56 57
			res.SetError(err, cmds.ErrNormal)
			return
58
		}
59

60 61 62 63 64 65
		if !n.OnlineMode() {
			err := n.SetupOfflineRouting()
			if err != nil {
				res.SetError(err, cmds.ErrNormal)
				return
			}
66 67
		}

68 69
		args := req.Arguments()

70
		if n.Identity == "" {
71 72
			res.SetError(errors.New("Identity not loaded!"), cmds.ErrNormal)
			return
73 74
		}

75
		var name string
Etienne Laurin's avatar
Etienne Laurin committed
76
		var pstr string
77 78 79

		switch len(args) {
		case 2:
80
			name = args[0]
Etienne Laurin's avatar
Etienne Laurin committed
81
			pstr = args[1]
82 83 84 85
			if name != n.Identity.Pretty() {
				res.SetError(errors.New("keychains not yet implemented"), cmds.ErrNormal)
				return
			}
86
		case 1:
87
			// name = n.Identity.Pretty()
Etienne Laurin's avatar
Etienne Laurin committed
88 89 90
			pstr = args[0]
		}

91
		p, err := path.ParsePath(pstr)
Etienne Laurin's avatar
Etienne Laurin committed
92
		if err != nil {
93
			res.SetError(fmt.Errorf("failed to validate path: %v", err), cmds.ErrNormal)
Etienne Laurin's avatar
Etienne Laurin committed
94
			return
95 96 97
		}

		// TODO n.Keychain.Get(name).PrivKey
98 99
		// TODO(cryptix): is req.Context().Context a child of n.Context()?
		output, err := publish(req.Context().Context, n, n.PrivateKey, p)
100 101 102 103 104
		if err != nil {
			res.SetError(err, cmds.ErrNormal)
			return
		}
		res.SetOutput(output)
105
	},
106
	Marshalers: cmds.MarshalerMap{
107
		cmds.Text: func(res cmds.Response) (io.Reader, error) {
108
			v := res.Output().(*IpnsEntry)
109
			s := fmt.Sprintf("Published to %s: %s\n", v.Name, v.Value)
110
			return strings.NewReader(s), nil
111 112
		},
	},
113
	Type: IpnsEntry{},
114 115
}

116
func publish(ctx context.Context, n *core.IpfsNode, k crypto.PrivKey, ref path.Path) (*IpnsEntry, error) {
Jeromy's avatar
Jeromy committed
117
	// First, verify the path exists
118
	_, err := core.Resolve(ctx, n, ref)
Jeromy's avatar
Jeromy committed
119 120 121 122
	if err != nil {
		return nil, err
	}

123
	err = n.Namesys.Publish(ctx, k, ref)
124 125 126 127 128 129 130 131 132 133
	if err != nil {
		return nil, err
	}

	hash, err := k.GetPublic().Hash()
	if err != nil {
		return nil, err
	}

	return &IpnsEntry{
134
		Name:  key.Key(hash).String(),
135
		Value: ref.String(),
136 137
	}, nil
}