From bbca4452982b785933ed924b78b82c5cbb2b8a9d Mon Sep 17 00:00:00 2001 From: Matt Bell Date: Tue, 4 Nov 2014 23:46:08 -0800 Subject: [PATCH] core/commands2: Added 'resolve' command --- core/commands2/resolve.go | 74 +++++++++++++++++++++++++++++++++++++++ core/commands2/root.go | 1 + 2 files changed, 75 insertions(+) create mode 100644 core/commands2/resolve.go diff --git a/core/commands2/resolve.go b/core/commands2/resolve.go new file mode 100644 index 000000000..ac85d583f --- /dev/null +++ b/core/commands2/resolve.go @@ -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 +} diff --git a/core/commands2/root.go b/core/commands2/root.go index 52563e708..60da85cb7 100644 --- a/core/commands2/root.go +++ b/core/commands2/root.go @@ -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