resolve.go 1.84 KB
Newer Older
1 2 3 4
package commands

import (
	"errors"
5 6
	"io"
	"strings"
7 8 9 10 11

	cmds "github.com/jbenet/go-ipfs/commands"
)

var resolveCmd = &cmds.Command{
12 13 14 15 16 17 18 19 20
	Helptext: cmds.HelpText{
		Tagline: "Gets the value currently published at an IPNS name",
		ShortDescription: `
IPNS is a PKI namespace, where names are the hashes of public keys, and
the private key enables publishing new (signed) values. In resolve, 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
21 22
the private key enables publishing new (signed) values. In resolve, the
default value of <name> is your own identity public key.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37


Examples:

Resolve the value of your identity:

  > ipfs name resolve
  QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy

Resolve te value of another name:

  > ipfs name resolve QmbCMUZw6JFeZ7Wp9jkzbye3Fzp2GGcPgC3nmeUjfVF87n
  QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy

`,
38
	},
39 40

	Arguments: []cmds.Argument{
41
		cmds.StringArg("name", false, false, "The IPNS name to resolve. Defaults to your node's peerID."),
42
	},
43
	Run: func(req cmds.Request) (interface{}, error) {
44

45 46 47 48 49
		n, err := req.Context().GetNode()
		if err != nil {
			return nil, err
		}

50
		var name string
51

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
52
		if n.PeerHost == nil {
53
			return nil, errNotOnline
54 55 56
		}

		if len(req.Arguments()) == 0 {
57
			if n.Identity == "" {
58
				return nil, errors.New("Identity not loaded!")
59
			}
60
			name = n.Identity.Pretty()
61

62
		} else {
63
			name = req.Arguments()[0]
64 65
		}

66
		output, err := n.Namesys.Resolve(name)
67
		if err != nil {
68
			return nil, err
69 70
		}

Matt Bell's avatar
Matt Bell committed
71 72
		// TODO: better errors (in the case of not finding the name, we get "failed to find any peer in table")

73
		return output, nil
74
	},
75
	Marshalers: cmds.MarshalerMap{
76
		cmds.Text: func(res cmds.Response) (io.Reader, error) {
77
			output := res.Output().(string)
78
			return strings.NewReader(output), nil
79
		},
80 81
	},
}