From cd108a0f88ca56e327b94491440c8527a7ea31ab Mon Sep 17 00:00:00 2001 From: Overbool Date: Fri, 9 Nov 2018 09:26:52 +0800 Subject: [PATCH] cmds/file: use new cmds lib License: MIT Signed-off-by: Overbool --- core/commands/root.go | 2 +- core/commands/unixfs/ls.go | 103 ++++++++++++++------------------- core/commands/unixfs/unixfs.go | 20 +------ 3 files changed, 45 insertions(+), 80 deletions(-) diff --git a/core/commands/root.go b/core/commands/root.go index 6e9d009d967..7796b7d65fc 100644 --- a/core/commands/root.go +++ b/core/commands/root.go @@ -138,7 +138,7 @@ var rootSubcommands = map[string]*cmds.Command{ "resolve": ResolveCmd, "swarm": SwarmCmd, "tar": TarCmd, - "file": lgc.NewCommand(unixfs.UnixFSCmd), + "file": unixfs.UnixFSCmd, "update": lgc.NewCommand(ExternalBinary()), "urlstore": urlStoreCmd, "version": VersionCmd, diff --git a/core/commands/unixfs/ls.go b/core/commands/unixfs/ls.go index ba3711beab6..551448e22b2 100644 --- a/core/commands/unixfs/ls.go +++ b/core/commands/unixfs/ls.go @@ -1,17 +1,16 @@ package unixfs import ( - "bytes" "fmt" "io" "sort" "text/tabwriter" - cmds "github.com/ipfs/go-ipfs/commands" - e "github.com/ipfs/go-ipfs/core/commands/e" + cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv" iface "github.com/ipfs/go-ipfs/core/coreapi/interface" unixfs "gx/ipfs/QmXLCwhHh7bxRsBnCKNE9BAN87V44aSxXLquZYTtjr6fZ3/go-unixfs" + cmds "gx/ipfs/Qma6uuSyjkecGhMFFLfzyJDPyoDtNJSHJNweDccZhaWkgU/go-ipfs-cmds" merkledag "gx/ipfs/QmaDBne4KeY3UepeqSVKYpSmQGa3q9zP6x3LfVF2UjF3Hc/go-merkledag" cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit" ) @@ -72,20 +71,22 @@ possible, please use 'ipfs ls' instead. Arguments: []cmdkit.Argument{ cmdkit.StringArg("ipfs-path", true, true, "The path to the IPFS object(s) to list links from.").EnableStdin(), }, - Run: func(req cmds.Request, res cmds.Response) { - node, err := req.InvocContext().GetNode() + Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error { + nd, err := cmdenv.GetNode(env) if err != nil { - res.SetError(err, cmdkit.ErrNormal) - return + return err } - api, err := req.InvocContext().GetApi() + api, err := cmdenv.GetApi(env) if err != nil { - res.SetError(err, cmdkit.ErrNormal) - return + return err } - paths := req.Arguments() + if err := req.ParseBodyArgs(); err != nil { + return err + } + + paths := req.Arguments output := LsOutput{ Arguments: map[string]string{}, @@ -93,18 +94,16 @@ possible, please use 'ipfs ls' instead. } for _, p := range paths { - ctx := req.Context() + ctx := req.Context fpath, err := iface.ParsePath(p) if err != nil { - res.SetError(err, cmdkit.ErrNormal) - return + return err } merkleNode, err := api.ResolveNode(ctx, fpath) if err != nil { - res.SetError(err, cmdkit.ErrNormal) - return + return err } c := merkleNode.Cid() @@ -119,14 +118,12 @@ possible, please use 'ipfs ls' instead. ndpb, ok := merkleNode.(*merkledag.ProtoNode) if !ok { - res.SetError(merkledag.ErrNotProtobuf, cmdkit.ErrNormal) - return + return merkledag.ErrNotProtobuf } unixFSNode, err := unixfs.FSNodeFromBytes(ndpb.Data()) if err != nil { - res.SetError(err, cmdkit.ErrNormal) - return + return err } t := unixFSNode.Type() @@ -142,27 +139,23 @@ possible, please use 'ipfs ls' instead. break case unixfs.THAMTShard: // We need a streaming ls API for this. - res.SetError(fmt.Errorf("cannot list large directories yet"), cmdkit.ErrNormal) - return + return fmt.Errorf("cannot list large directories yet") case unixfs.TDirectory: links := make([]LsLink, len(merkleNode.Links())) output.Objects[hash].Links = links for i, link := range merkleNode.Links() { - linkNode, err := link.GetNode(ctx, node.DAG) + linkNode, err := link.GetNode(ctx, nd.DAG) if err != nil { - res.SetError(err, cmdkit.ErrNormal) - return + return err } lnpb, ok := linkNode.(*merkledag.ProtoNode) if !ok { - res.SetError(merkledag.ErrNotProtobuf, cmdkit.ErrNormal) - return + return merkledag.ErrNotProtobuf } d, err := unixfs.FSNodeFromBytes(lnpb.Data()) if err != nil { - res.SetError(err, cmdkit.ErrNormal) - return + return err } t := d.Type() lsLink := LsLink{ @@ -178,36 +171,24 @@ possible, please use 'ipfs ls' instead. links[i] = lsLink } case unixfs.TSymlink: - res.SetError(fmt.Errorf("cannot list symlinks yet"), cmdkit.ErrNormal) - return + return fmt.Errorf("cannot list symlinks yet") default: - res.SetError(fmt.Errorf("unrecognized type: %s", t), cmdkit.ErrImplementation) - return + return fmt.Errorf("unrecognized type: %s", t) } } - res.SetOutput(&output) + return cmds.EmitOnce(res, &output) }, - Marshalers: cmds.MarshalerMap{ - cmds.Text: func(res cmds.Response) (io.Reader, error) { - v, err := unwrapOutput(res.Output()) - if err != nil { - return nil, err - } - - output, ok := v.(*LsOutput) - if !ok { - return nil, e.TypeErr(output, v) - } - buf := new(bytes.Buffer) - w := tabwriter.NewWriter(buf, 1, 2, 1, ' ', 0) + Encoders: cmds.EncoderMap{ + cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *LsOutput) error { + tw := tabwriter.NewWriter(w, 1, 2, 1, ' ', 0) nonDirectories := []string{} directories := []string{} - for argument, hash := range output.Arguments { - object, ok := output.Objects[hash] + for argument, hash := range out.Arguments { + object, ok := out.Objects[hash] if !ok { - return nil, fmt.Errorf("unresolved hash: %s", hash) + return fmt.Errorf("unresolved hash: %s", hash) } if object.Type == "Directory" { @@ -220,36 +201,36 @@ possible, please use 'ipfs ls' instead. sort.Strings(directories) for _, argument := range nonDirectories { - fmt.Fprintf(w, "%s\n", argument) + fmt.Fprintf(tw, "%s\n", argument) } seen := map[string]bool{} for i, argument := range directories { - hash := output.Arguments[argument] + hash := out.Arguments[argument] if _, ok := seen[hash]; ok { continue } seen[hash] = true - object := output.Objects[hash] + object := out.Objects[hash] if i > 0 || len(nonDirectories) > 0 { - fmt.Fprintln(w) + fmt.Fprintln(tw) } - if len(output.Arguments) > 1 { + if len(out.Arguments) > 1 { for _, arg := range directories[i:] { - if output.Arguments[arg] == hash { - fmt.Fprintf(w, "%s:\n", arg) + if out.Arguments[arg] == hash { + fmt.Fprintf(tw, "%s:\n", arg) } } } for _, link := range object.Links { - fmt.Fprintf(w, "%s\n", link.Name) + fmt.Fprintf(tw, "%s\n", link.Name) } } - w.Flush() + tw.Flush() - return buf, nil - }, + return nil + }), }, Type: LsOutput{}, } diff --git a/core/commands/unixfs/unixfs.go b/core/commands/unixfs/unixfs.go index ed89ca66f09..25852f4ad5c 100644 --- a/core/commands/unixfs/unixfs.go +++ b/core/commands/unixfs/unixfs.go @@ -1,10 +1,8 @@ package unixfs import ( - cmds "github.com/ipfs/go-ipfs/commands" - e "github.com/ipfs/go-ipfs/core/commands/e" - - "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit" + cmds "gx/ipfs/Qma6uuSyjkecGhMFFLfzyJDPyoDtNJSHJNweDccZhaWkgU/go-ipfs-cmds" + cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit" ) var UnixFSCmd = &cmds.Command{ @@ -26,17 +24,3 @@ objects (e.g. fanout and chunking). "ls": LsCmd, }, } - -// copy+pasted from ../commands.go -func unwrapOutput(i interface{}) (interface{}, error) { - var ( - ch <-chan interface{} - ok bool - ) - - if ch, ok = i.(<-chan interface{}); !ok { - return nil, e.TypeErr(ch, i) - } - - return <-ch, nil -}