mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-21 19:50:56 +08:00
ls: introduce specific output types (removes IsDir from object plumbing commands)
This commit is contained in:
@ -13,19 +13,19 @@ import (
|
|||||||
unixfspb "github.com/jbenet/go-ipfs/unixfs/pb"
|
unixfspb "github.com/jbenet/go-ipfs/unixfs/pb"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Link struct {
|
type LsLink struct {
|
||||||
Name, Hash string
|
Name, Hash string
|
||||||
Size uint64
|
Size uint64
|
||||||
IsDir bool
|
Type unixfspb.Data_DataType
|
||||||
}
|
}
|
||||||
|
|
||||||
type Object struct {
|
type LsObject struct {
|
||||||
Hash string
|
Hash string
|
||||||
Links []Link
|
Links []LsLink
|
||||||
}
|
}
|
||||||
|
|
||||||
type LsOutput struct {
|
type LsOutput struct {
|
||||||
Objects []Object
|
Objects []LsObject
|
||||||
}
|
}
|
||||||
|
|
||||||
var LsCmd = &cmds.Command{
|
var LsCmd = &cmds.Command{
|
||||||
@ -61,11 +61,11 @@ it contains, with the following format:
|
|||||||
dagnodes = append(dagnodes, dagnode)
|
dagnodes = append(dagnodes, dagnode)
|
||||||
}
|
}
|
||||||
|
|
||||||
output := make([]Object, len(req.Arguments()))
|
output := make([]LsObject, len(req.Arguments()))
|
||||||
for i, dagnode := range dagnodes {
|
for i, dagnode := range dagnodes {
|
||||||
output[i] = Object{
|
output[i] = LsObject{
|
||||||
Hash: paths[i],
|
Hash: paths[i],
|
||||||
Links: make([]Link, len(dagnode.Links)),
|
Links: make([]LsLink, len(dagnode.Links)),
|
||||||
}
|
}
|
||||||
for j, link := range dagnode.Links {
|
for j, link := range dagnode.Links {
|
||||||
link.Node, err = link.GetNode(node.DAG)
|
link.Node, err = link.GetNode(node.DAG)
|
||||||
@ -78,11 +78,11 @@ it contains, with the following format:
|
|||||||
res.SetError(err, cmds.ErrNormal)
|
res.SetError(err, cmds.ErrNormal)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
output[i].Links[j] = Link{
|
output[i].Links[j] = LsLink{
|
||||||
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,
|
Type: d.GetType(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -98,7 +98,13 @@ it contains, with the following format:
|
|||||||
if len(output) > 1 {
|
if len(output) > 1 {
|
||||||
fmt.Fprintf(w, "%s:\n", object.Hash)
|
fmt.Fprintf(w, "%s:\n", object.Hash)
|
||||||
}
|
}
|
||||||
marshalLinks(w, object.Links)
|
fmt.Fprintln(w, "Hash\tSize\tName\t")
|
||||||
|
for _, link := range object.Links {
|
||||||
|
if link.Type == unixfspb.Data_Directory {
|
||||||
|
link.Name += "/"
|
||||||
|
}
|
||||||
|
fmt.Fprintf(w, "%s\t%v\t%s\t\n", link.Hash, link.Size, link.Name)
|
||||||
|
}
|
||||||
if len(output) > 1 {
|
if len(output) > 1 {
|
||||||
fmt.Fprintln(w)
|
fmt.Fprintln(w)
|
||||||
}
|
}
|
||||||
@ -110,13 +116,3 @@ it contains, with the following format:
|
|||||||
},
|
},
|
||||||
Type: LsOutput{},
|
Type: LsOutput{},
|
||||||
}
|
}
|
||||||
|
|
||||||
func marshalLinks(w io.Writer, links []Link) {
|
|
||||||
fmt.Fprintln(w, "Hash\tSize\tName\t")
|
|
||||||
for _, link := range links {
|
|
||||||
if link.IsDir {
|
|
||||||
link.Name += "/"
|
|
||||||
}
|
|
||||||
fmt.Fprintf(w, "%s\t%v\t%s\t\n", link.Hash, link.Size, link.Name)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -28,6 +28,16 @@ type Node struct {
|
|||||||
Data string
|
Data string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Link struct {
|
||||||
|
Name, Hash string
|
||||||
|
Size uint64
|
||||||
|
}
|
||||||
|
|
||||||
|
type Object struct {
|
||||||
|
Hash string
|
||||||
|
Links []Link
|
||||||
|
}
|
||||||
|
|
||||||
var ObjectCmd = &cmds.Command{
|
var ObjectCmd = &cmds.Command{
|
||||||
Helptext: cmds.HelpText{
|
Helptext: cmds.HelpText{
|
||||||
Tagline: "Interact with ipfs objects",
|
Tagline: "Interact with ipfs objects",
|
||||||
@ -123,7 +133,10 @@ multihash.
|
|||||||
object := res.Output().(*Object)
|
object := res.Output().(*Object)
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
w := tabwriter.NewWriter(&buf, 1, 2, 1, ' ', 0)
|
w := tabwriter.NewWriter(&buf, 1, 2, 1, ' ', 0)
|
||||||
marshalLinks(w, object.Links)
|
fmt.Fprintln(w, "Hash\tSize\tName\t")
|
||||||
|
for _, link := range object.Links {
|
||||||
|
fmt.Fprintf(w, "%s\t%v\t%s\t\n", link.Hash, link.Size, link.Name)
|
||||||
|
}
|
||||||
w.Flush()
|
w.Flush()
|
||||||
return &buf, nil
|
return &buf, nil
|
||||||
},
|
},
|
||||||
|
Reference in New Issue
Block a user