Skip to content

Commit b0dc73c

Browse files
committed
fix(commands): Use post-run to remove flag
License: MIT Signed-off-by: hannahhoward <[email protected]>
1 parent be4c4ab commit b0dc73c

File tree

1 file changed

+71
-52
lines changed

1 file changed

+71
-52
lines changed

core/commands/ls.go

+71-52
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package commands
33
import (
44
"fmt"
55
"io"
6+
"os"
67
"text/tabwriter"
78

89
cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
@@ -38,9 +39,6 @@ type LsObject struct {
3839
// it can be complete or partial
3940
type LsOutput struct {
4041
Objects []LsObject
41-
// temporary flag to help us figure out where we are in the process of ls-ing
42-
// the directory when we are streaming
43-
LastObjectHash string
4442
}
4543

4644
const (
@@ -102,7 +100,6 @@ The JSON output contains type information.
102100
if err != nil {
103101
return err
104102
}
105-
106103
dagnode, err := api.ResolveNode(req.Context, p)
107104
if err != nil {
108105
return err
@@ -113,7 +110,6 @@ The JSON output contains type information.
113110
ro := merkledag.NewReadOnlyDagService(ng)
114111

115112
stream, _ := req.Options[lsStreamOptionName].(bool)
116-
lastObjectHash := ""
117113

118114
if !stream {
119115
output := make([]LsObject, len(req.Arguments))
@@ -147,7 +143,7 @@ The JSON output contains type information.
147143
}
148144
}
149145

150-
return cmds.EmitOnce(res, &LsOutput{output, lastObjectHash})
146+
return cmds.EmitOnce(res, &LsOutput{output})
151147
}
152148

153149
for i, dagnode := range dagnodes {
@@ -173,62 +169,42 @@ The JSON output contains type information.
173169
if err != nil {
174170
return err
175171
}
176-
output := []LsObject{
177-
{
178-
Hash: paths[i],
179-
Links: []LsLink{*lsLink},
180-
},
181-
}
182-
if err = res.Emit(&LsOutput{output, lastObjectHash}); err != nil {
172+
output := []LsObject{{
173+
Hash: paths[i],
174+
Links: []LsLink{*lsLink},
175+
}}
176+
if err = res.Emit(&LsOutput{output}); err != nil {
183177
return err
184178
}
185-
lastObjectHash = paths[i]
186179
}
187180
}
188181
return nil
189182
},
190-
Encoders: cmds.EncoderMap{
191-
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *LsOutput) error {
192-
headers, _ := req.Options[lsHeadersOptionNameTime].(bool)
193-
stream, _ := req.Options[lsStreamOptionName].(bool)
194-
// in streaming mode we can't automatically align the tabs
195-
// so we take a best guess
196-
var minTabWidth int
197-
if stream {
198-
minTabWidth = 10
199-
} else {
200-
minTabWidth = 1
201-
}
202-
203-
multipleFolders := len(req.Arguments) > 1
204-
lastObjectHash := out.LastObjectHash
205-
206-
tw := tabwriter.NewWriter(w, minTabWidth, 2, 1, ' ', 0)
183+
PostRun: cmds.PostRunMap{
184+
cmds.CLI: func(res cmds.Response, re cmds.ResponseEmitter) error {
185+
req := res.Request()
186+
lastObjectHash := ""
207187

208-
for _, object := range out.Objects {
209-
210-
if object.Hash != lastObjectHash {
211-
if multipleFolders {
212-
if lastObjectHash != "" {
213-
fmt.Fprintln(tw)
214-
}
215-
fmt.Fprintf(tw, "%s:\n", object.Hash)
216-
}
217-
if headers {
218-
fmt.Fprintln(tw, "Hash\tSize\tName")
219-
}
220-
lastObjectHash = object.Hash
221-
}
222-
223-
for _, link := range object.Links {
224-
if link.Type == unixfs.TDirectory {
225-
link.Name += "/"
188+
for {
189+
v, err := res.Next()
190+
if err != nil {
191+
if err == io.EOF {
192+
return nil
226193
}
227-
228-
fmt.Fprintf(tw, "%s\t%v\t%s\n", link.Hash, link.Size, link.Name)
194+
return err
229195
}
196+
out := v.(*LsOutput)
197+
lastObjectHash = tabularOutput(req, os.Stdout, out, lastObjectHash, false)
230198
}
231-
tw.Flush()
199+
},
200+
},
201+
Encoders: cmds.EncoderMap{
202+
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *LsOutput) error {
203+
// when streaming over HTTP using a text encoder, we cannot render breaks
204+
// between directories because we don't know the hash of the last
205+
// directory encoder
206+
ignoreBreaks, _ := req.Options[lsStreamOptionName].(bool)
207+
tabularOutput(req, w, out, "", ignoreBreaks)
232208
return nil
233209
}),
234210
},
@@ -284,3 +260,46 @@ func makeLsLink(req *cmds.Request, dserv ipld.DAGService, resolve bool, link *ip
284260
Type: t,
285261
}, nil
286262
}
263+
264+
func tabularOutput(req *cmds.Request, w io.Writer, out *LsOutput, lastObjectHash string, ignoreBreaks bool) string {
265+
headers, _ := req.Options[lsHeadersOptionNameTime].(bool)
266+
stream, _ := req.Options[lsStreamOptionName].(bool)
267+
// in streaming mode we can't automatically align the tabs
268+
// so we take a best guess
269+
var minTabWidth int
270+
if stream {
271+
minTabWidth = 10
272+
} else {
273+
minTabWidth = 1
274+
}
275+
276+
multipleFolders := len(req.Arguments) > 1
277+
278+
tw := tabwriter.NewWriter(w, minTabWidth, 2, 1, ' ', 0)
279+
280+
for _, object := range out.Objects {
281+
282+
if !ignoreBreaks && object.Hash != lastObjectHash {
283+
if multipleFolders {
284+
if lastObjectHash != "" {
285+
fmt.Fprintln(tw)
286+
}
287+
fmt.Fprintf(tw, "%s:\n", object.Hash)
288+
}
289+
if headers {
290+
fmt.Fprintln(tw, "Hash\tSize\tName")
291+
}
292+
lastObjectHash = object.Hash
293+
}
294+
295+
for _, link := range object.Links {
296+
if link.Type == unixfs.TDirectory {
297+
link.Name += "/"
298+
}
299+
300+
fmt.Fprintf(tw, "%s\t%v\t%s\n", link.Hash, link.Size, link.Name)
301+
}
302+
}
303+
tw.Flush()
304+
return lastObjectHash
305+
}

0 commit comments

Comments
 (0)