@cryptotaxi247 / kubo / commits / d72c9fcd3

Add flag to ls to not resolve type of subnodes (#2824)

* 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 <kubuxu@protonmail.ch> * Make ipfs ls show type if it is avaliable locally License: MIT Signed-off-by: Jakub Sztandera <kubuxu@protonmail.ch> * Make resolve-type default to true License: MIT Signed-off-by: Jakub Sztandera <kubuxu@protonmail.ch>

Jakub Sztandera committed Jun 28, 2016 at 21:06 UTC d72c9fcd3ece11fb60288c8a081ba9c3030165e6
1 file changed +38 -9
core/commands/ls.go
+38 -9
@@ -6,6 +6,7 @@ import (
6 "io"
7 "text/tabwriter"
8
9 + key "github.com/ipfs/go-ipfs/blocks/key"
10 cmds "github.com/ipfs/go-ipfs/commands"
11 core "github.com/ipfs/go-ipfs/core"
12 merkledag "github.com/ipfs/go-ipfs/merkledag"
@@ -45,6 +46,7 @@ format:
46 },
47 Options: []cmds.Option{
48 cmds.BoolOption("headers", "v", "Print table headers (Hash, Size, Name).").Default(false),
49 + cmds.BoolOption("resolve-type", "Resolve linked objects to find out their types.").Default(true),
50 },
51 Run: func(req cmds.Request, res cmds.Response) {
52 node, err := req.InvocContext().GetNode()
@@ -59,6 +61,12 @@ format:
61 return
62 }
63
64 + resolve, _, err := req.Option("resolve-type").Bool()
65 + if err != nil {
66 + res.SetError(err, cmds.ErrNormal)
67 + return
68 + }
69 +
70 paths := req.Arguments()
71
72 var dagnodes []*merkledag.Node
@@ -79,21 +87,42 @@ format:
87 }
88 for j, link := range dagnode.Links {
89 var linkNode *merkledag.Node
82 - linkNode, err = link.GetNode(req.Context(), node.DAG)
83 - if err != nil {
84 - res.SetError(err, cmds.ErrNormal)
85 - return
90 + t := unixfspb.Data_DataType(-1)
91 + linkKey := key.Key(link.Hash)
92 + if ok, err := node.Blockstore.Has(linkKey); ok && err == nil {
93 + b, err := node.Blockstore.Get(linkKey)
94 + if err != nil {
95 + res.SetError(err, cmds.ErrNormal)
96 + return
97 + }
98 + linkNode, err = merkledag.DecodeProtobuf(b.Data())
99 + if err != nil {
100 + res.SetError(err, cmds.ErrNormal)
101 + return
102 + }
103 + }
104 +
105 + if linkNode == nil && resolve {
106 + linkNode, err = link.GetNode(req.Context(), node.DAG)
107 + if err != nil {
108 + res.SetError(err, cmds.ErrNormal)
109 + return
110 + }
111 }
87 - d, err := unixfs.FromBytes(linkNode.Data)
88 - if err != nil {
89 - res.SetError(err, cmds.ErrNormal)
90 - return
112 + if linkNode != nil {
113 + d, err := unixfs.FromBytes(linkNode.Data)
114 + if err != nil {
115 + res.SetError(err, cmds.ErrNormal)
116 + return
117 + }
118 +
119 + t = d.GetType()
120 }
121 output[i].Links[j] = LsLink{
122 Name: link.Name,
123 Hash: link.Hash.B58String(),
124 Size: link.Size,
96 - Type: d.GetType(),
125 + Type: t,
126 }
127 }
128 }