Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmds/file: use new cmds lib #5756

Merged
merged 1 commit into from
Nov 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
103 changes: 42 additions & 61 deletions core/commands/unixfs/ls.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand Down Expand Up @@ -72,39 +71,39 @@ 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{},
Objects: map[string]*LsObject{},
}

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()
Expand All @@ -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()
Expand All @@ -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{
Expand All @@ -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" {
Expand All @@ -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{},
}
20 changes: 2 additions & 18 deletions core/commands/unixfs/unixfs.go
Original file line number Diff line number Diff line change
@@ -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{
Expand All @@ -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
}