Skip to content
This repository was archived by the owner on Feb 7, 2024. It is now read-only.

Commit

Permalink
Add Shell.FileList(path)
Browse files Browse the repository at this point in the history
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]: ipfs/kubo#1348
  • Loading branch information
wking committed Jun 19, 2015
1 parent 01929f1 commit 5210e32
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions unixfs.go
Original file line number Diff line number Diff line change
@@ -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")
}

0 comments on commit 5210e32

Please sign in to comment.