resolve.go 3.15 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 "gx/ipfs/QmZNVWh8LLjAavuQ2JXuFmuYH3C11xo988vSgp7UQrTRj1/go-ipfs-util"
11 12
)

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

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

32
EXAMPLES
33 34 35

Resolve the value of your identity:

36
  $ ipfs resolve /ipns/QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy
37 38 39 40
  /ipfs/Qmcqtw8FfrVSBaRmbWwHxt3AuySBhJLcvmFYi3Lbc4xnwj

Resolve the value of another name:

41
  $ ipfs resolve /ipns/QmbCMUZw6JFeZ7Wp9jkzbye3Fzp2GGcPgC3nmeUjfVF87n
42
  /ipns/QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy
43

44
Resolve the value of another name recursively:
45

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

49 50
Resolve the value of an IPFS DAG path:

51
  $ ipfs resolve /ipfs/QmeZy1fGbwgVSrqbfh9fKQrAWgeyRnj7h8fsHS1oy3k99x/beep/boop
52 53
  /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{
Richard Littauer's avatar
Richard Littauer committed
61
		cmds.BoolOption("recursive", "r", "Resolve until the result is an IPFS name. Default: false."),
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() + "\n"), nil
121
		},
122
	},
123
	Type: ResolvedPath{},
124
}