Skip to content

Commit

Permalink
fix(commands): Use post-run to remove flag
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: hannahhoward <hannah@hannahhoward.net>
  • Loading branch information
hannahhoward committed Nov 16, 2018
1 parent be4c4ab commit b0dc73c
Showing 1 changed file with 71 additions and 52 deletions.
123 changes: 71 additions & 52 deletions core/commands/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package commands
import (
"fmt"
"io"
"os"
"text/tabwriter"

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

const (
Expand Down Expand Up @@ -102,7 +100,6 @@ The JSON output contains type information.
if err != nil {
return err
}

dagnode, err := api.ResolveNode(req.Context, p)
if err != nil {
return err
Expand All @@ -113,7 +110,6 @@ The JSON output contains type information.
ro := merkledag.NewReadOnlyDagService(ng)

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

if !stream {
output := make([]LsObject, len(req.Arguments))
Expand Down Expand Up @@ -147,7 +143,7 @@ The JSON output contains type information.
}
}

return cmds.EmitOnce(res, &LsOutput{output, lastObjectHash})
return cmds.EmitOnce(res, &LsOutput{output})
}

for i, dagnode := range dagnodes {
Expand All @@ -173,62 +169,42 @@ The JSON output contains type information.
if err != nil {
return err
}
output := []LsObject{
{
Hash: paths[i],
Links: []LsLink{*lsLink},
},
}
if err = res.Emit(&LsOutput{output, lastObjectHash}); err != nil {
output := []LsObject{{
Hash: paths[i],
Links: []LsLink{*lsLink},
}}
if err = res.Emit(&LsOutput{output}); err != nil {
return err
}
lastObjectHash = paths[i]
}
}
return nil
},
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
if stream {
minTabWidth = 10
} else {
minTabWidth = 1
}

multipleFolders := len(req.Arguments) > 1
lastObjectHash := out.LastObjectHash

tw := tabwriter.NewWriter(w, minTabWidth, 2, 1, ' ', 0)
PostRun: cmds.PostRunMap{
cmds.CLI: func(res cmds.Response, re cmds.ResponseEmitter) error {
req := res.Request()
lastObjectHash := ""

for _, object := range out.Objects {

if object.Hash != lastObjectHash {
if multipleFolders {
if lastObjectHash != "" {
fmt.Fprintln(tw)
}
fmt.Fprintf(tw, "%s:\n", object.Hash)
}
if headers {
fmt.Fprintln(tw, "Hash\tSize\tName")
}
lastObjectHash = object.Hash
}

for _, link := range object.Links {
if link.Type == unixfs.TDirectory {
link.Name += "/"
for {
v, err := res.Next()
if err != nil {
if err == io.EOF {
return nil
}

fmt.Fprintf(tw, "%s\t%v\t%s\n", link.Hash, link.Size, link.Name)
return err
}
out := v.(*LsOutput)
lastObjectHash = tabularOutput(req, os.Stdout, out, lastObjectHash, false)
}
tw.Flush()
},
},
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *LsOutput) error {
// when streaming over HTTP using a text encoder, we cannot render breaks
// between directories because we don't know the hash of the last
// directory encoder
ignoreBreaks, _ := req.Options[lsStreamOptionName].(bool)
tabularOutput(req, w, out, "", ignoreBreaks)
return nil
}),
},
Expand Down Expand Up @@ -284,3 +260,46 @@ func makeLsLink(req *cmds.Request, dserv ipld.DAGService, resolve bool, link *ip
Type: t,
}, nil
}

func tabularOutput(req *cmds.Request, w io.Writer, out *LsOutput, lastObjectHash string, ignoreBreaks bool) string {
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
if stream {
minTabWidth = 10
} else {
minTabWidth = 1
}

multipleFolders := len(req.Arguments) > 1

tw := tabwriter.NewWriter(w, minTabWidth, 2, 1, ' ', 0)

for _, object := range out.Objects {

if !ignoreBreaks && object.Hash != lastObjectHash {
if multipleFolders {
if lastObjectHash != "" {
fmt.Fprintln(tw)
}
fmt.Fprintf(tw, "%s:\n", object.Hash)
}
if headers {
fmt.Fprintln(tw, "Hash\tSize\tName")
}
lastObjectHash = object.Hash
}

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()
return lastObjectHash
}

0 comments on commit b0dc73c

Please sign in to comment.