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

import (
4 5
	"io"
	"strings"
6

7
	cmds "github.com/ipfs/go-ipfs/commands"
8
	namesys "github.com/ipfs/go-ipfs/namesys"
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

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

Jeromy's avatar
Jeromy committed
60
		n, err := req.InvocContext().GetNode()
61
		if err != nil {
62 63
			res.SetError(err, cmds.ErrNormal)
			return
64 65
		}

66 67 68 69 70 71
		if !n.OnlineMode() {
			err := n.SetupOfflineRouting()
			if err != nil {
				res.SetError(err, cmds.ErrNormal)
				return
			}
72 73
		}

74
		name := req.Arguments()[0]
75 76 77 78 79 80
		recursive, _, _ := req.Option("recursive").Bool()
		depth := 1
		if recursive {
			depth = namesys.DefaultDepthLimit
		}

Jeromy's avatar
Jeromy committed
81
		output, err := n.Namesys.ResolveN(req.Context(), name, depth)
82
		if err != nil {
83 84
			res.SetError(err, cmds.ErrNormal)
			return
85 86
		}

87
		res.SetOutput(&ResolvedPath{output})
88
	},
89
	Marshalers: cmds.MarshalerMap{
90
		cmds.Text: func(res cmds.Response) (io.Reader, error) {
91
			output, ok := res.Output().(*ResolvedPath)
92 93 94
			if !ok {
				return nil, u.ErrCast()
			}
95
			return strings.NewReader(output.Path.String()), nil
96
		},
97
	},
98
	Type: ResolvedPath{},
99
}