github.com/keltia/go-ipfs@v0.3.8-0.20150909044612-210793031c63/core/commands/resolve.go (about)

     1  package commands
     2  
     3  import (
     4  	"io"
     5  	"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  type ResolvedPath struct {
    14  	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  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  themselves and into IPNS.  For example IPNS references can (currently)
    28  point at IPFS object, and DNS links can point at other DNS links, IPNS
    29  entries, or IPFS objects.  This command accepts any of these
    30  identifiers and resolves them to the referenced item.
    31  
    32  Examples:
    33  
    34  Resolve the value of your identity:
    35  
    36    > ipfs resolve /ipns/QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy
    37    /ipfs/Qmcqtw8FfrVSBaRmbWwHxt3AuySBhJLcvmFYi3Lbc4xnwj
    38  
    39  Resolve the value of another name:
    40  
    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  	},
    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  
    60  		n, err := req.InvocContext().GetNode()
    61  		if err != nil {
    62  			res.SetError(err, cmds.ErrNormal)
    63  			return
    64  		}
    65  
    66  		if !n.OnlineMode() {
    67  			err := n.SetupOfflineRouting()
    68  			if err != nil {
    69  				res.SetError(err, cmds.ErrNormal)
    70  				return
    71  			}
    72  		}
    73  
    74  		name := req.Arguments()[0]
    75  		recursive, _, _ := req.Option("recursive").Bool()
    76  		depth := 1
    77  		if recursive {
    78  			depth = namesys.DefaultDepthLimit
    79  		}
    80  
    81  		output, err := n.Namesys.ResolveN(req.Context(), name, depth)
    82  		if err != nil {
    83  			res.SetError(err, cmds.ErrNormal)
    84  			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  			if !ok {
    93  				return nil, u.ErrCast()
    94  			}
    95  			return strings.NewReader(output.Path.String()), nil
    96  		},
    97  	},
    98  	Type: ResolvedPath{},
    99  }