提交 7391cfd4 作者: Łukasz Magiera 提交者: hannahhoward

fix(commands): reduce complexity w/ typed encoder

uses single flag to support state needed by PostRun
supports encoding=text

License: MIT
Signed-off-by: 's avatarhannahhoward <hannah@hannahhoward.net>
上级 20b92809
......@@ -3,11 +3,9 @@ package commands
import (
"fmt"
"io"
"os"
"text/tabwriter"
cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
e "github.com/ipfs/go-ipfs/core/commands/e"
iface "github.com/ipfs/go-ipfs/core/coreapi/interface"
cid "gx/ipfs/QmR8BauakNcBa3RbE4nbQu76PDiJgoQgz8AJdhJuiU4TAw/go-cid"
......@@ -39,7 +37,8 @@ type LsObject struct {
// LsOutput is a set of printable data for directories,
// it can be complete or partial
type LsOutput struct {
Objects []LsObject
Objects []LsObject
LastObjectHash string
}
const (
......@@ -112,6 +111,8 @@ The JSON output contains type information.
ro := merkledag.NewReadOnlyDagService(ng)
stream, _ := req.Options[lsStreamOptionName].(bool)
lastObjectHash := ""
if !stream {
output := make([]LsObject, len(req.Arguments))
......@@ -144,7 +145,7 @@ The JSON output contains type information.
}
}
return cmds.EmitOnce(res, &LsOutput{output})
return cmds.EmitOnce(res, &LsOutput{output, lastObjectHash})
}
for i, dagnode := range dagnodes {
......@@ -161,15 +162,6 @@ The JSON output contains type information.
}
for linkResult := range linkResults {
output := make([]LsObject, len(req.Arguments))
for i, path := range paths {
output[i] = LsObject{
Hash: path,
Links: nil,
}
}
outputLinks := make([]LsLink, 1)
if linkResult.Err != nil {
return linkResult.Err
......@@ -179,21 +171,24 @@ The JSON output contains type information.
if err != nil {
return err
}
outputLinks[0] = *lsLink
output[i].Links = outputLinks
if err = res.Emit(&LsOutput{output}); err != nil {
output := []LsObject{
{
Hash: paths[i],
Links: []LsLink{*lsLink},
},
}
if err = res.Emit(&LsOutput{output, lastObjectHash}); err != nil {
return err
}
lastObjectHash = paths[i]
}
}
return nil
},
PostRun: cmds.PostRunMap{
cmds.CLI: func(res cmds.Response, re cmds.ResponseEmitter) error {
req := res.Request()
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *LsOutput) error {
headers, _ := req.Options[lsHeadersOptionNameTime].(bool)
stream, _ := req.Options[lsStreamOptionName].(bool)
// in streaming mode we can't automatically align the tabs
// so we take a best guess
var minTabWidth int
......@@ -204,56 +199,36 @@ The JSON output contains type information.
}
multipleFolders := len(req.Arguments) > 1
lastDirectoryWritten := -1
tw := tabwriter.NewWriter(os.Stdout, minTabWidth, 2, 1, ' ', 0)
for {
v, err := res.Next()
if err != nil {
if err == io.EOF {
if multipleFolders {
fmt.Fprintln(os.Stdout)
}
return nil
}
lastObjectHash := out.LastObjectHash
return err
}
tw := tabwriter.NewWriter(w, minTabWidth, 2, 1, ' ', 0)
output, ok := v.(*LsOutput)
if !ok {
return e.TypeErr(output, v)
}
for _, object := range out.Objects {
for i, object := range output.Objects {
if len(object.Links) == 0 {
continue
}
if i > lastDirectoryWritten {
if i > 0 {
if multipleFolders {
fmt.Fprintln(tw)
}
}
if multipleFolders {
fmt.Fprintf(tw, "%s:\n", object.Hash)
if object.Hash != lastObjectHash {
if multipleFolders {
if lastObjectHash != "" {
fmt.Fprintln(tw)
}
if headers {
fmt.Fprintln(tw, "Hash\tSize\tName")
}
lastDirectoryWritten = i
fmt.Fprintf(tw, "%s:\n", object.Hash)
}
for _, link := range object.Links {
if link.Type == unixfs.TDirectory {
link.Name += "/"
}
if headers {
fmt.Fprintln(tw, "Hash\tSize\tName")
}
lastObjectHash = object.Hash
}
fmt.Fprintf(tw, "%s\t%v\t%s\n", link.Hash, link.Size, link.Name)
for _, link := range object.Links {
if link.Type == unixfs.TDirectory {
link.Name += "/"
}
fmt.Fprintf(tw, "%s\t%v\t%s\n", link.Hash, link.Size, link.Name)
}
tw.Flush()
}
},
tw.Flush()
return nil
}),
},
Type: LsOutput{},
}
......
......@@ -57,7 +57,6 @@ QmaRGe7bVmVaLmxbrMiVNXqW4pRNNp3xq7hFtyRKA3mtJL 14 a
QmSix55yz8CzWXf5ZVM9vgEvijnEeeXiTSarVtsqiiCJss:
QmQNd6ubRXaNG6Prov8o6vk3bn6eWsj9FxLGrAVDUAGkGe 139 128
QmZULkCELmmk5XNfCgTnCyFgAVxBRBXyDHGGMVoLFLiXEN 14 a
EOF
test_cmp expected_ls actual_ls
'
......@@ -84,7 +83,6 @@ QmSix55yz8CzWXf5ZVM9vgEvijnEeeXiTSarVtsqiiCJss:
Hash Size Name
QmQNd6ubRXaNG6Prov8o6vk3bn6eWsj9FxLGrAVDUAGkGe 139 128
QmZULkCELmmk5XNfCgTnCyFgAVxBRBXyDHGGMVoLFLiXEN 14 a
EOF
test_cmp expected_ls_headers actual_ls_headers
'
......@@ -138,7 +136,6 @@ QmaRGe7bVmVaLmxbrMiVNXqW4pRNNp3xq7hFtyRKA3mtJL 14 a
QmSix55yz8CzWXf5ZVM9vgEvijnEeeXiTSarVtsqiiCJss:
QmQNd6ubRXaNG6Prov8o6vk3bn6eWsj9FxLGrAVDUAGkGe 139 128
QmZULkCELmmk5XNfCgTnCyFgAVxBRBXyDHGGMVoLFLiXEN 14 a
EOF
test_cmp expected_ls_stream actual_ls_stream
'
......@@ -165,7 +162,6 @@ QmSix55yz8CzWXf5ZVM9vgEvijnEeeXiTSarVtsqiiCJss:
Hash Size Name
QmQNd6ubRXaNG6Prov8o6vk3bn6eWsj9FxLGrAVDUAGkGe 139 128
QmZULkCELmmk5XNfCgTnCyFgAVxBRBXyDHGGMVoLFLiXEN 14 a
EOF
test_cmp expected_ls_stream_headers actual_ls_stream_headers
'
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论