1
1
package commands
2
2
3
3
import (
4
+ "bytes"
4
5
"fmt"
5
6
"io"
6
- "strings "
7
+ "text/tabwriter "
7
8
8
9
cmds "github.com/jbenet/go-ipfs/commands"
9
10
merkledag "github.com/jbenet/go-ipfs/merkledag"
10
11
path "github.com/jbenet/go-ipfs/path"
12
+ "github.com/jbenet/go-ipfs/unixfs"
13
+ unixfspb "github.com/jbenet/go-ipfs/unixfs/pb"
11
14
)
12
15
13
16
type Link struct {
14
17
Name , Hash string
15
18
Size uint64
19
+ IsDir bool
16
20
}
17
21
18
22
type Object struct {
@@ -64,10 +68,21 @@ it contains, with the following format:
64
68
Links : make ([]Link , len (dagnode .Links )),
65
69
}
66
70
for j , link := range dagnode .Links {
71
+ link .Node , err = link .GetNode (node .DAG )
72
+ if err != nil {
73
+ res .SetError (err , cmds .ErrNormal )
74
+ return
75
+ }
76
+ d , err := unixfs .FromBytes (link .Node .Data )
77
+ if err != nil {
78
+ res .SetError (err , cmds .ErrNormal )
79
+ return
80
+ }
67
81
output [i ].Links [j ] = Link {
68
- Name : link .Name ,
69
- Hash : link .Hash .B58String (),
70
- Size : link .Size ,
82
+ Name : link .Name ,
83
+ Hash : link .Hash .B58String (),
84
+ Size : link .Size ,
85
+ IsDir : d .GetType () == unixfspb .Data_Directory ,
71
86
}
72
87
}
73
88
}
@@ -76,28 +91,32 @@ it contains, with the following format:
76
91
},
77
92
Marshalers : cmds.MarshalerMap {
78
93
cmds .Text : func (res cmds.Response ) (io.Reader , error ) {
79
- s := ""
80
94
output := res .Output ().(* LsOutput ).Objects
81
-
95
+ var buf bytes.Buffer
96
+ w := tabwriter .NewWriter (& buf , 1 , 2 , 1 , ' ' , 0 )
82
97
for _ , object := range output {
83
98
if len (output ) > 1 {
84
- s += fmt .Sprintf ( "%s:\n " , object .Hash )
99
+ fmt .Fprintf ( w , "%s:\n " , object .Hash )
85
100
}
86
- s += marshalLinks (object .Links )
101
+ marshalLinks (w , object .Links )
87
102
if len (output ) > 1 {
88
- s += " \n "
103
+ fmt . Fprintln ( w )
89
104
}
90
105
}
106
+ w .Flush ()
91
107
92
- return strings . NewReader ( s ) , nil
108
+ return & buf , nil
93
109
},
94
110
},
95
111
Type : LsOutput {},
96
112
}
97
113
98
- func marshalLinks (links []Link ) (s string ) {
114
+ func marshalLinks (w io.Writer , links []Link ) {
115
+ fmt .Fprintln (w , "Hash\t Size\t Name\t " )
99
116
for _ , link := range links {
100
- s += fmt .Sprintf ("%s %v %s\n " , link .Hash , link .Size , link .Name )
117
+ if link .IsDir {
118
+ link .Name += "/"
119
+ }
120
+ fmt .Fprintf (w , "%s\t %v\t %s\t \n " , link .Hash , link .Size , link .Name )
101
121
}
102
- return s
103
122
}
0 commit comments