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:

committed by
Juan Batiz-Benet

parent
109af01396
commit
bbca445298
74
core/commands2/resolve.go
Normal file
74
core/commands2/resolve.go
Normal 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
|
||||
}
|
@ -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
|
||||
|
Reference in New Issue
Block a user