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

     1  package commands
     2  
     3  import (
     4  	"errors"
     5  	"io"
     6  	"strings"
     7  
     8  	cmds "github.com/ipfs/go-ipfs/commands"
     9  	namesys "github.com/ipfs/go-ipfs/namesys"
    10  	u "github.com/ipfs/go-ipfs/util"
    11  )
    12  
    13  var IpnsCmd = &cmds.Command{
    14  	Helptext: cmds.HelpText{
    15  		Tagline: "Gets the value currently published at an IPNS name",
    16  		ShortDescription: `
    17  IPNS is a PKI namespace, where names are the hashes of public keys, and
    18  the private key enables publishing new (signed) values. In resolve, the
    19  default value of <name> is your own identity public key.
    20  `,
    21  		LongDescription: `
    22  IPNS is a PKI namespace, where names are the hashes of public keys, and
    23  the private key enables publishing new (signed) values. In resolve, the
    24  default value of <name> is your own identity public key.
    25  
    26  
    27  Examples:
    28  
    29  Resolve the value of your identity:
    30  
    31    > ipfs name resolve
    32    QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy
    33  
    34  Resolve the value of another name:
    35  
    36    > ipfs name resolve QmbCMUZw6JFeZ7Wp9jkzbye3Fzp2GGcPgC3nmeUjfVF87n
    37    QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy
    38  
    39  `,
    40  	},
    41  
    42  	Arguments: []cmds.Argument{
    43  		cmds.StringArg("name", false, false, "The IPNS name to resolve. Defaults to your node's peerID.").EnableStdin(),
    44  	},
    45  	Options: []cmds.Option{
    46  		cmds.BoolOption("recursive", "r", "Resolve until the result is not an IPNS name"),
    47  	},
    48  	Run: func(req cmds.Request, res cmds.Response) {
    49  
    50  		n, err := req.InvocContext().GetNode()
    51  		if err != nil {
    52  			res.SetError(err, cmds.ErrNormal)
    53  			return
    54  		}
    55  
    56  		if !n.OnlineMode() {
    57  			err := n.SetupOfflineRouting()
    58  			if err != nil {
    59  				res.SetError(err, cmds.ErrNormal)
    60  				return
    61  			}
    62  		}
    63  
    64  		var name string
    65  
    66  		if len(req.Arguments()) == 0 {
    67  			if n.Identity == "" {
    68  				res.SetError(errors.New("Identity not loaded!"), cmds.ErrNormal)
    69  				return
    70  			}
    71  			name = n.Identity.Pretty()
    72  
    73  		} else {
    74  			name = req.Arguments()[0]
    75  		}
    76  
    77  		recursive, _, _ := req.Option("recursive").Bool()
    78  		depth := 1
    79  		if recursive {
    80  			depth = namesys.DefaultDepthLimit
    81  		}
    82  
    83  		resolver := namesys.NewRoutingResolver(n.Routing)
    84  		output, err := resolver.ResolveN(req.Context(), name, depth)
    85  		if err != nil {
    86  			res.SetError(err, cmds.ErrNormal)
    87  			return
    88  		}
    89  
    90  		// TODO: better errors (in the case of not finding the name, we get "failed to find any peer in table")
    91  
    92  		res.SetOutput(&ResolvedPath{output})
    93  	},
    94  	Marshalers: cmds.MarshalerMap{
    95  		cmds.Text: func(res cmds.Response) (io.Reader, error) {
    96  			output, ok := res.Output().(*ResolvedPath)
    97  			if !ok {
    98  				return nil, u.ErrCast()
    99  			}
   100  			return strings.NewReader(output.Path.String()), nil
   101  		},
   102  	},
   103  	Type: ResolvedPath{},
   104  }