Skip to content

Commit

Permalink
Add flag to ls to not resolve type of subnodes (#2824)
Browse files Browse the repository at this point in the history
* Disable resolving of the typ by default in ls

Add option to resolve type using ls but
set it to false by default.

License: MIT
Signed-off-by: Jakub Sztandera <[email protected]>

* Make ipfs ls show type if it is avaliable locally

License: MIT
Signed-off-by: Jakub Sztandera <[email protected]>

* Make resolve-type default to true

License: MIT
Signed-off-by: Jakub Sztandera <[email protected]>
  • Loading branch information
Kubuxu authored and whyrusleeping committed Jun 28, 2016
1 parent 7fa88fd commit d72c9fc
Showing 1 changed file with 38 additions and 9 deletions.
47 changes: 38 additions & 9 deletions core/commands/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"text/tabwriter"

key "github.com/ipfs/go-ipfs/blocks/key"
cmds "github.com/ipfs/go-ipfs/commands"
core "github.com/ipfs/go-ipfs/core"
merkledag "github.com/ipfs/go-ipfs/merkledag"
Expand Down Expand Up @@ -45,6 +46,7 @@ format:
},
Options: []cmds.Option{
cmds.BoolOption("headers", "v", "Print table headers (Hash, Size, Name).").Default(false),
cmds.BoolOption("resolve-type", "Resolve linked objects to find out their types.").Default(true),
},
Run: func(req cmds.Request, res cmds.Response) {
node, err := req.InvocContext().GetNode()
Expand All @@ -59,6 +61,12 @@ format:
return
}

resolve, _, err := req.Option("resolve-type").Bool()
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}

paths := req.Arguments()

var dagnodes []*merkledag.Node
Expand All @@ -79,21 +87,42 @@ format:
}
for j, link := range dagnode.Links {
var linkNode *merkledag.Node
linkNode, err = link.GetNode(req.Context(), node.DAG)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
t := unixfspb.Data_DataType(-1)
linkKey := key.Key(link.Hash)
if ok, err := node.Blockstore.Has(linkKey); ok && err == nil {
b, err := node.Blockstore.Get(linkKey)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
linkNode, err = merkledag.DecodeProtobuf(b.Data())
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
}

if linkNode == nil && resolve {
linkNode, err = link.GetNode(req.Context(), node.DAG)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
}
d, err := unixfs.FromBytes(linkNode.Data)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
if linkNode != nil {
d, err := unixfs.FromBytes(linkNode.Data)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}

t = d.GetType()
}
output[i].Links[j] = LsLink{
Name: link.Name,
Hash: link.Hash.B58String(),
Size: link.Size,
Type: d.GetType(),
Type: t,
}
}
}
Expand Down

0 comments on commit d72c9fc

Please sign in to comment.