coreapi: stream only ls, handle storting in command
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
Łukasz Magiera committed
Feb 2, 2019 at 03:42 UTC
d1b6ccaae9610248f95af61bf55f54536578bba3
5 files changed
+9
-53
core/commands/ls.go
+6
-6
@@ -4,6 +4,7 @@ import (
4
"fmt"
5
"io"
6
"os"
7
+ "sort"
8
"text/tabwriter"
9
10
cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
@@ -112,6 +113,10 @@ The JSON output contains type information.
113
return nil
114
}, func(i int) {
115
// after each dir
116
+ sort.Slice(outputLinks, func(i, j int) bool {
117
+ return outputLinks[i].Name < outputLinks[j].Name
118
+ })
119
+
120
output[i] = LsObject{
121
Hash: paths[i],
122
Links: outputLinks,
@@ -131,7 +136,6 @@ The JSON output contains type information.
136
}
137
138
results, err := api.Unixfs().Ls(req.Context, p,
134
- options.Unixfs.Async(stream),
139
options.Unixfs.ResolveType(resolveType),
140
options.Unixfs.ResolveSize(resolveSize))
141
if err != nil {
@@ -156,11 +160,7 @@ The JSON output contains type information.
160
}
161
dirDone(i)
162
}
159
- if err := done(); err != nil {
160
- return err
161
- }
162
-
163
- return nil
163
+ return done()
164
},
165
PostRun: cmds.PostRunMap{
166
cmds.CLI: func(res cmds.Response, re cmds.ResponseEmitter) error {
core/coreapi/interface/options/unixfs.go
-14
@@ -43,8 +43,6 @@ type UnixfsAddSettings struct {
43
}
44
45
type UnixfsLsSettings struct {
46
- Async bool
47
-
46
ResolveType bool
47
ResolveSize bool
48
}
@@ -132,8 +130,6 @@ func UnixfsAddOptions(opts ...UnixfsAddOption) (*UnixfsAddSettings, cid.Prefix,
130
131
func UnixfsLsOptions(opts ...UnixfsLsOption) (*UnixfsLsSettings, error) {
132
options := &UnixfsLsSettings{
135
- Async: true,
136
-
133
ResolveSize: true,
134
ResolveType: true,
135
}
@@ -317,16 +313,6 @@ func (unixfsOpts) Nocopy(enable bool) UnixfsAddOption {
313
}
314
}
315
320
-// Async tells ls to return results as soon as they are available, which can be
321
-// useful for listing HAMT directories. When this option is set to true returned
322
-// results won't be returned in order
323
-func (unixfsOpts) Async(async bool) UnixfsLsOption {
324
- return func(settings *UnixfsLsSettings) error {
325
- settings.Async = async
326
- return nil
327
- }
328
-}
329
-
316
func (unixfsOpts) ResolveSize(resolve bool) UnixfsLsOption {
317
return func(settings *UnixfsLsSettings) error {
318
settings.ResolveSize = resolve
core/coreapi/interface/tests/unixfs.go
+1
-20
@@ -749,7 +749,7 @@ func (tp *provider) TestLs(t *testing.T) {
749
t.Error(err)
750
}
751
752
- links, err := api.Unixfs().Ls(ctx, p, options.Unixfs.Async(false))
752
+ links, err := api.Unixfs().Ls(ctx, p)
753
if err != nil {
754
t.Error(err)
755
}
@@ -767,25 +767,6 @@ func (tp *provider) TestLs(t *testing.T) {
767
if _, ok := <-links; ok {
768
t.Errorf("didn't expect a second link")
769
}
770
-
771
- links, err = api.Unixfs().Ls(ctx, p, options.Unixfs.Async(true))
772
- if err != nil {
773
- t.Error(err)
774
- }
775
-
776
- link = (<-links).Link
777
- if link.Size != 23 {
778
- t.Fatalf("expected size = 23, got %d", link.Size)
779
- }
780
- if link.Name != "name-of-file" {
781
- t.Fatalf("expected name = name-of-file, got %s", link.Name)
782
- }
783
- if link.Cid.String() != "QmX3qQVKxDGz3URVC3861Z3CKtQKGBn6ffXRBBWGMFz9Lr" {
784
- t.Fatalf("expected cid = QmX3qQVKxDGz3URVC3861Z3CKtQKGBn6ffXRBBWGMFz9Lr, got %s", link.Cid)
785
- }
786
- if _, ok := <-links; ok {
787
- t.Errorf("didn't expect a second link")
788
- }
770
}
771
772
func (tp *provider) TestEntriesExpired(t *testing.T) {
core/coreapi/interface/unixfs.go
+2
-1
@@ -38,6 +38,7 @@ type UnixfsAPI interface {
38
// to operations performed on the returned file
39
Get(context.Context, Path) (files.Node, error)
40
41
- // Ls returns the list of links in a directory
41
+ // Ls returns the list of links in a directory. Links aren't guaranteed to be
42
+ // returned in order
43
Ls(context.Context, Path, ...options.UnixfsLsOption) (<-chan LsLink, error)
44
}
core/coreapi/unixfs.go
-12
@@ -167,10 +167,6 @@ func (api *UnixfsAPI) Ls(ctx context.Context, p coreiface.Path, opts ...options.
167
return nil, err
168
}
169
170
- if !settings.Async {
171
- return uses.lsFromDir(ctx, dir, settings)
172
- }
173
-
170
return uses.lsFromLinksAsync(ctx, dir, settings)
171
}
172
@@ -234,14 +230,6 @@ func (api *UnixfsAPI) lsFromLinksAsync(ctx context.Context, dir uio.Directory, s
230
return out, nil
231
}
232
237
-func (api *UnixfsAPI) lsFromDir(ctx context.Context, dir uio.Directory, settings *options.UnixfsLsSettings) (<-chan coreiface.LsLink, error) {
238
- l, err := dir.Links(ctx)
239
- if err != nil {
240
- return nil, err
241
- }
242
- return api.lsFromLinks(ctx, l, settings)
243
-}
244
-
233
func (api *UnixfsAPI) lsFromLinks(ctx context.Context, ndlinks []*ipld.Link, settings *options.UnixfsLsSettings) (<-chan coreiface.LsLink, error) {
234
links := make(chan coreiface.LsLink, len(ndlinks))
235
for _, l := range ndlinks {