mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-21 11:44:16 +08:00
beautify 'ipfs ls' and 'ipfs object links' (updates #799)
This commit is contained in:
@ -1,18 +1,22 @@
|
|||||||
package commands
|
package commands
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"strings"
|
"text/tabwriter"
|
||||||
|
|
||||||
cmds "github.com/jbenet/go-ipfs/commands"
|
cmds "github.com/jbenet/go-ipfs/commands"
|
||||||
merkledag "github.com/jbenet/go-ipfs/merkledag"
|
merkledag "github.com/jbenet/go-ipfs/merkledag"
|
||||||
path "github.com/jbenet/go-ipfs/path"
|
path "github.com/jbenet/go-ipfs/path"
|
||||||
|
"github.com/jbenet/go-ipfs/unixfs"
|
||||||
|
unixfspb "github.com/jbenet/go-ipfs/unixfs/pb"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Link struct {
|
type Link struct {
|
||||||
Name, Hash string
|
Name, Hash string
|
||||||
Size uint64
|
Size uint64
|
||||||
|
IsDir bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type Object struct {
|
type Object struct {
|
||||||
@ -64,10 +68,21 @@ it contains, with the following format:
|
|||||||
Links: make([]Link, len(dagnode.Links)),
|
Links: make([]Link, len(dagnode.Links)),
|
||||||
}
|
}
|
||||||
for j, link := range dagnode.Links {
|
for j, link := range dagnode.Links {
|
||||||
|
link.Node, err = link.GetNode(node.DAG)
|
||||||
|
if err != nil {
|
||||||
|
res.SetError(err, cmds.ErrNormal)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
d, err := unixfs.FromBytes(link.Node.Data)
|
||||||
|
if err != nil {
|
||||||
|
res.SetError(err, cmds.ErrNormal)
|
||||||
|
return
|
||||||
|
}
|
||||||
output[i].Links[j] = Link{
|
output[i].Links[j] = Link{
|
||||||
Name: link.Name,
|
Name: link.Name,
|
||||||
Hash: link.Hash.B58String(),
|
Hash: link.Hash.B58String(),
|
||||||
Size: link.Size,
|
Size: link.Size,
|
||||||
|
IsDir: d.GetType() == unixfspb.Data_Directory,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -76,28 +91,32 @@ it contains, with the following format:
|
|||||||
},
|
},
|
||||||
Marshalers: cmds.MarshalerMap{
|
Marshalers: cmds.MarshalerMap{
|
||||||
cmds.Text: func(res cmds.Response) (io.Reader, error) {
|
cmds.Text: func(res cmds.Response) (io.Reader, error) {
|
||||||
s := ""
|
|
||||||
output := res.Output().(*LsOutput).Objects
|
output := res.Output().(*LsOutput).Objects
|
||||||
|
var buf bytes.Buffer
|
||||||
|
w := tabwriter.NewWriter(&buf, 1, 2, 1, ' ', 0)
|
||||||
for _, object := range output {
|
for _, object := range output {
|
||||||
if len(output) > 1 {
|
if len(output) > 1 {
|
||||||
s += fmt.Sprintf("%s:\n", object.Hash)
|
fmt.Fprintf(w, "%s:\n", object.Hash)
|
||||||
}
|
}
|
||||||
s += marshalLinks(object.Links)
|
marshalLinks(w, object.Links)
|
||||||
if len(output) > 1 {
|
if len(output) > 1 {
|
||||||
s += "\n"
|
fmt.Fprintln(w)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
w.Flush()
|
||||||
|
|
||||||
return strings.NewReader(s), nil
|
return &buf, nil
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Type: LsOutput{},
|
Type: LsOutput{},
|
||||||
}
|
}
|
||||||
|
|
||||||
func marshalLinks(links []Link) (s string) {
|
func marshalLinks(w io.Writer, links []Link) {
|
||||||
|
fmt.Fprintln(w, "Hash\tSize\tName\t")
|
||||||
for _, link := range links {
|
for _, link := range links {
|
||||||
s += fmt.Sprintf("%s %v %s\n", link.Hash, link.Size, link.Name)
|
if link.IsDir {
|
||||||
|
link.Name += "/"
|
||||||
|
}
|
||||||
|
fmt.Fprintf(w, "%s\t%v\t%s\t\n", link.Hash, link.Size, link.Name)
|
||||||
}
|
}
|
||||||
return s
|
|
||||||
}
|
}
|
||||||
|
@ -8,6 +8,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"strings"
|
"strings"
|
||||||
|
"text/tabwriter"
|
||||||
|
|
||||||
mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
|
mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
|
||||||
|
|
||||||
@ -120,8 +121,11 @@ multihash.
|
|||||||
Marshalers: cmds.MarshalerMap{
|
Marshalers: cmds.MarshalerMap{
|
||||||
cmds.Text: func(res cmds.Response) (io.Reader, error) {
|
cmds.Text: func(res cmds.Response) (io.Reader, error) {
|
||||||
object := res.Output().(*Object)
|
object := res.Output().(*Object)
|
||||||
marshalled := marshalLinks(object.Links)
|
var buf bytes.Buffer
|
||||||
return strings.NewReader(marshalled), nil
|
w := tabwriter.NewWriter(&buf, 1, 2, 1, ' ', 0)
|
||||||
|
marshalLinks(w, object.Links)
|
||||||
|
w.Flush()
|
||||||
|
return &buf, nil
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Type: Object{},
|
Type: Object{},
|
||||||
@ -246,7 +250,7 @@ var objectStatCmd = &cmds.Command{
|
|||||||
|
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
w := func(s string, n int) {
|
w := func(s string, n int) {
|
||||||
buf.Write([]byte(fmt.Sprintf("%s: %d\n", s, n)))
|
fmt.Fprintf(&buf, "%s: %d\n", s, n)
|
||||||
}
|
}
|
||||||
w("NumLinks", ns.NumLinks)
|
w("NumLinks", ns.NumLinks)
|
||||||
w("BlockSize", ns.BlockSize)
|
w("BlockSize", ns.BlockSize)
|
||||||
|
Reference in New Issue
Block a user