Commit bbca4452 authored by Matt Bell's avatar Matt Bell Committed by Juan Batiz-Benet

core/commands2: Added 'resolve' command

parent 109af013
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
}
......@@ -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
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment