fix(commands): Use post-run to remove flag
License: MIT Signed-off-by: hannahhoward <hannah@hannahhoward.net>
hannahhoward committed
Nov 14, 2018 at 17:00 UTC
b0dc73c45fc1814990a843f8ae64819797375037
1 file changed
+71
-52
core/commands/ls.go
+71
-52
@@ -3,6 +3,7 @@ package commands
3
import (
4
"fmt"
5
"io"
6
+ "os"
7
"text/tabwriter"
8
9
cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
@@ -38,9 +39,6 @@ type LsObject struct {
39
// it can be complete or partial
40
type LsOutput struct {
41
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
42
}
43
44
const (
@@ -102,7 +100,6 @@ The JSON output contains type information.
100
if err != nil {
101
return err
102
}
105
-
103
dagnode, err := api.ResolveNode(req.Context, p)
104
if err != nil {
105
return err
@@ -113,7 +110,6 @@ The JSON output contains type information.
110
ro := merkledag.NewReadOnlyDagService(ng)
111
112
stream, _ := req.Options[lsStreamOptionName].(bool)
116
- lastObjectHash := ""
113
114
if !stream {
115
output := make([]LsObject, len(req.Arguments))
@@ -147,7 +143,7 @@ The JSON output contains type information.
143
}
144
}
145
150
- return cmds.EmitOnce(res, &LsOutput{output, lastObjectHash})
146
+ return cmds.EmitOnce(res, &LsOutput{output})
147
}
148
149
for i, dagnode := range dagnodes {
@@ -173,62 +169,42 @@ The JSON output contains type information.
169
if err != nil {
170
return err
171
}
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 {
177
return err
178
}
185
- lastObjectHash = paths[i]
179
}
180
}
181
return nil
182
},
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 := ""
187
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
193
}
227
-
228
- fmt.Fprintf(tw, "%s\t%v\t%s\n", link.Hash, link.Size, link.Name)
194
+ return err
195
}
196
+ out := v.(*LsOutput)
197
+ lastObjectHash = tabularOutput(req, os.Stdout, out, lastObjectHash, false)
198
}
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)
208
return nil
209
}),
210
},
@@ -284,3 +260,46 @@ func makeLsLink(req *cmds.Request, dserv ipld.DAGService, resolve bool, link *ip
260
Type: t,
261
}, nil
262
}
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
+}