resolve.go 3.09 KB
Newer Older
1 2 3
package commands

import (
4 5
	"io"
	"strings"
6

7
	cmds "github.com/ipfs/go-ipfs/commands"
8
	"github.com/ipfs/go-ipfs/core"
9
	path "github.com/ipfs/go-ipfs/path"
10
	u "github.com/ipfs/go-ipfs/util"
11 12
)

13 14
type ResolvedPath struct {
	Path path.Path
15 16
}

17
var ResolveCmd = &cmds.Command{
18
	Helptext: cmds.HelpText{
19
		Tagline: "Resolve the value of names to IPFS",
20
		ShortDescription: `
21 22 23
There are a number of mutable name protocols that can link among
themselves and into IPNS.  This command accepts any of these
identifiers and resolves them to the referenced item.
24 25
`,
		LongDescription: `
26 27 28 29 30
There are a number of mutable name protocols that can link among
themselves and into IPNS.  For example IPNS references can (currently)
point at IPFS object, and DNS links can point at other DNS links, IPNS
entries, or IPFS objects.  This command accepts any of these
identifiers and resolves them to the referenced item.
31 32 33 34 35

Examples:

Resolve the value of your identity:

36 37 38 39 40 41 42
  > ipfs resolve /ipns/QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy
  /ipfs/Qmcqtw8FfrVSBaRmbWwHxt3AuySBhJLcvmFYi3Lbc4xnwj

Resolve the value of another name:

  > ipfs resolve /ipns/QmbCMUZw6JFeZ7Wp9jkzbye3Fzp2GGcPgC3nmeUjfVF87n
  /ipns/QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy
43

44
Resolve the value of another name recursively:
45

46 47
  > ipfs resolve -r /ipns/QmbCMUZw6JFeZ7Wp9jkzbye3Fzp2GGcPgC3nmeUjfVF87n
  /ipfs/Qmcqtw8FfrVSBaRmbWwHxt3AuySBhJLcvmFYi3Lbc4xnwj
48

49 50 51 52 53
Resolve the value of an IPFS DAG path:

  > ipfs resolve /ipfs/QmeZy1fGbwgVSrqbfh9fKQrAWgeyRnj7h8fsHS1oy3k99x/beep/boop
  /ipfs/QmYRMjyvAiHKN9UTi8Bzt1HUspmSRD8T8DwxfSMzLgBon1

54
`,
55
	},
56 57

	Arguments: []cmds.Argument{
58
		cmds.StringArg("name", true, false, "The name to resolve.").EnableStdin(),
59
	},
60
	Options: []cmds.Option{
61
		cmds.BoolOption("recursive", "r", "Resolve until the result is an IPFS name"),
62
	},
63
	Run: func(req cmds.Request, res cmds.Response) {
64

Jeromy's avatar
Jeromy committed
65
		n, err := req.InvocContext().GetNode()
66
		if err != nil {
67 68
			res.SetError(err, cmds.ErrNormal)
			return
69 70
		}

71 72 73 74 75 76
		if !n.OnlineMode() {
			err := n.SetupOfflineRouting()
			if err != nil {
				res.SetError(err, cmds.ErrNormal)
				return
			}
77 78
		}

79
		name := req.Arguments()[0]
80
		recursive, _, _ := req.Option("recursive").Bool()
81 82 83 84 85 86 87 88 89 90

		// the case when ipns is resolved step by step
		if strings.HasPrefix(name, "/ipns/") && !recursive {
			p, err := n.Namesys.ResolveN(req.Context(), name, 1)
			if err != nil {
				res.SetError(err, cmds.ErrNormal)
				return
			}
			res.SetOutput(&ResolvedPath{p})
			return
91 92
		}

93 94 95 96 97
		// else, ipfs path or ipns with recursive flag
		p, err := path.ParsePath(name)
		if err != nil {
			res.SetError(err, cmds.ErrNormal)
			return
98
		}
99 100

		node, err := core.Resolve(req.Context(), n, p)
101 102
		if err != nil {
			res.SetError(err, cmds.ErrNormal)
103 104 105
			return
		}

106
		key, err := node.Key()
107
		if err != nil {
108 109
			res.SetError(err, cmds.ErrNormal)
			return
110 111
		}

112
		res.SetOutput(&ResolvedPath{path.FromKey(key)})
113
	},
114
	Marshalers: cmds.MarshalerMap{
115
		cmds.Text: func(res cmds.Response) (io.Reader, error) {
116
			output, ok := res.Output().(*ResolvedPath)
117 118 119
			if !ok {
				return nil, u.ErrCast()
			}
120
			return strings.NewReader(output.Path.String()), nil
121
		},
122
	},
123
	Type: ResolvedPath{},
124
}