From 5210e3224429a716927f6257e03c5bae01909a84 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Fri, 19 Jun 2015 00:14:55 -0700 Subject: [PATCH] Add Shell.FileList(path) So I can bind to the Unix-filesystem-layer version of ls that landed with [1]. I've spun this layer off into it's own file to avoid crowding shell.go. [1]: https://github.com/ipfs/go-ipfs/pull/1348 --- unixfs.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 unixfs.go diff --git a/unixfs.go b/unixfs.go new file mode 100644 index 000000000..e237d7122 --- /dev/null +++ b/unixfs.go @@ -0,0 +1,49 @@ +package shell + +import ( + "encoding/json" + "fmt" + + cmds "github.com/ipfs/go-ipfs/commands" + cc "github.com/ipfs/go-ipfs/core/commands" + unixfs "github.com/ipfs/go-ipfs/core/commands/unixfs" +) + +// FileList entries at the given path using the UnixFS commands +func (s *Shell) FileList(path string) (*unixfs.LsObject, error) { + ropts, err := cc.Root.GetOptions([]string{"file", "ls"}) + if err != nil { + return nil, err + } + + req, err := cmds.NewRequest([]string{"file", "ls"}, nil, []string{path}, nil, unixfs.LsCmd, ropts) + if err != nil { + return nil, err + } + + resp, err := s.client.Send(req) + if err != nil { + return nil, err + } + if resp.Error() != nil { + return nil, resp.Error() + } + + read, err := resp.Reader() + if err != nil { + return nil, err + } + + dec := json.NewDecoder(read) + out := unixfs.LsOutput{} + err = dec.Decode(&out) + if err != nil { + return nil, err + } + + for _, object := range out.Objects { + return object, nil + } + + return nil, fmt.Errorf("no object in results") +}