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