github.com/keltia/go-ipfs@v0.3.8-0.20150909044612-210793031c63/core/commands/dns.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  	util "github.com/ipfs/go-ipfs/util"
    10  )
    11  
    12  var DNSCmd = &cmds.Command{
    13  	Helptext: cmds.HelpText{
    14  		Tagline: "DNS link resolver",
    15  		ShortDescription: `
    16  Multihashes are hard to remember, but domain names are usually easy to
    17  remember.  To create memorable aliases for multihashes, DNS TXT
    18  records can point to other DNS links, IPFS objects, IPNS keys, etc.
    19  This command resolves those links to the referenced object.
    20  `,
    21  		LongDescription: `
    22  Multihashes are hard to remember, but domain names are usually easy to
    23  remember.  To create memorable aliases for multihashes, DNS TXT
    24  records can point to other DNS links, IPFS objects, IPNS keys, etc.
    25  This command resolves those links to the referenced object.
    26  
    27  For example, with this DNS TXT record:
    28  
    29    ipfs.io. TXT "dnslink=/ipfs/QmRzTuh2Lpuz7Gr39stNr6mTFdqAghsZec1JoUnfySUzcy ..."
    30  
    31  The resolver will give:
    32  
    33    > ipfs dns ipfs.io
    34    /ipfs/QmRzTuh2Lpuz7Gr39stNr6mTFdqAghsZec1JoUnfySUzcy
    35  
    36  And with this DNS TXT record:
    37  
    38    ipfs.ipfs.io. TXT "dnslink=/dns/ipfs.io ..."
    39  
    40  The resolver will give:
    41  
    42    > ipfs dns ipfs.io
    43    /dns/ipfs.io
    44    > ipfs dns --recursive
    45    /ipfs/QmRzTuh2Lpuz7Gr39stNr6mTFdqAghsZec1JoUnfySUzcy
    46  `,
    47  	},
    48  
    49  	Arguments: []cmds.Argument{
    50  		cmds.StringArg("domain-name", true, false, "The domain-name name to resolve.").EnableStdin(),
    51  	},
    52  	Options: []cmds.Option{
    53  		cmds.BoolOption("recursive", "r", "Resolve until the result is not a DNS link"),
    54  	},
    55  	Run: func(req cmds.Request, res cmds.Response) {
    56  
    57  		recursive, _, _ := req.Option("recursive").Bool()
    58  		name := req.Arguments()[0]
    59  		resolver := namesys.NewDNSResolver()
    60  
    61  		depth := 1
    62  		if recursive {
    63  			depth = namesys.DefaultDepthLimit
    64  		}
    65  		output, err := resolver.ResolveN(req.Context(), name, depth)
    66  		if err != nil {
    67  			res.SetError(err, cmds.ErrNormal)
    68  			return
    69  		}
    70  		res.SetOutput(&ResolvedPath{output})
    71  	},
    72  	Marshalers: cmds.MarshalerMap{
    73  		cmds.Text: func(res cmds.Response) (io.Reader, error) {
    74  			output, ok := res.Output().(*ResolvedPath)
    75  			if !ok {
    76  				return nil, util.ErrCast()
    77  			}
    78  			return strings.NewReader(output.Path.String()), nil
    79  		},
    80  	},
    81  	Type: ResolvedPath{},
    82  }