1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-07-02 12:20:03 +08:00

core/commands2: Added 'resolve' command

This commit is contained in:
Matt Bell
2014-11-04 23:46:08 -08:00
committed by Juan Batiz-Benet
parent 109af01396
commit bbca445298
2 changed files with 75 additions and 0 deletions

74
core/commands2/resolve.go Normal file
View File

@ -0,0 +1,74 @@
package commands
import (
"errors"
cmds "github.com/jbenet/go-ipfs/commands"
"github.com/jbenet/go-ipfs/core"
)
type ResolveOutput struct {
Entries []IpnsEntry
}
var resolveCmd = &cmds.Command{
Arguments: []cmds.Argument{
cmds.Argument{"name", cmds.ArgString, false, true},
},
Run: func(res cmds.Response, req cmds.Request) {
name := ""
args := req.Arguments()
n := req.Context().Node
var output []IpnsEntry
if len(args) == 0 {
if n.Identity == nil {
res.SetError(errors.New("Identity not loaded!"), cmds.ErrNormal)
return
}
name = n.Identity.ID().String()
entry, err := resolve(name, n)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
output = []IpnsEntry{entry}
} else {
output = make([]IpnsEntry, len(args))
for i, arg := range args {
var ok bool
name, ok = arg.(string)
if !ok {
res.SetError(errors.New("cast error"), cmds.ErrNormal)
return
}
entry, err := resolve(name, n)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
output[i] = entry
}
}
res.SetOutput(&ResolveOutput{output})
},
Type: &ResolveOutput{},
}
func resolve(name string, n *core.IpfsNode) (IpnsEntry, error) {
resolved, err := n.Namesys.Resolve(name)
if err != nil {
return IpnsEntry{}, err
}
return IpnsEntry{
Name: name,
Value: resolved,
}, nil
}

View File

@ -65,6 +65,7 @@ var rootSubcommands = map[string]*cmds.Command{
"diag": diagCmd,
"pin": pinCmd,
"unpin": unpinCmd,
"resolve": resolveCmd,
// test subcommands
// TODO: remove these when we don't need them anymore