beautify 'ipfs ls' and 'ipfs object links' (updates #799)
Henry committed
Feb 26, 2015 at 23:57 UTC
607468a96da6019f1273eecf2dc094f96aee2a8c
2 files changed
+39
-16
core/commands/ls.go
+32
-13
@@ -1,18 +1,22 @@
1
package commands
2
3
import (
4
+ "bytes"
5
"fmt"
6
"io"
6
- "strings"
7
+ "text/tabwriter"
8
9
cmds "github.com/jbenet/go-ipfs/commands"
10
merkledag "github.com/jbenet/go-ipfs/merkledag"
11
path "github.com/jbenet/go-ipfs/path"
12
+ "github.com/jbenet/go-ipfs/unixfs"
13
+ unixfspb "github.com/jbenet/go-ipfs/unixfs/pb"
14
)
15
16
type Link struct {
17
Name, Hash string
18
Size uint64
19
+ IsDir bool
20
}
21
22
type Object struct {
@@ -64,10 +68,21 @@ it contains, with the following format:
68
Links: make([]Link, len(dagnode.Links)),
69
}
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
+ }
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,
86
}
87
}
88
}
@@ -76,28 +91,32 @@ it contains, with the following format:
91
},
92
Marshalers: cmds.MarshalerMap{
93
cmds.Text: func(res cmds.Response) (io.Reader, error) {
79
- s := ""
94
output := res.Output().(*LsOutput).Objects
81
-
95
+ var buf bytes.Buffer
96
+ w := tabwriter.NewWriter(&buf, 1, 2, 1, ' ', 0)
97
for _, object := range output {
98
if len(output) > 1 {
84
- s += fmt.Sprintf("%s:\n", object.Hash)
99
+ fmt.Fprintf(w, "%s:\n", object.Hash)
100
}
86
- s += marshalLinks(object.Links)
101
+ marshalLinks(w, object.Links)
102
if len(output) > 1 {
88
- s += "\n"
103
+ fmt.Fprintln(w)
104
}
105
}
106
+ w.Flush()
107
92
- return strings.NewReader(s), nil
108
+ return &buf, nil
109
},
110
},
111
Type: LsOutput{},
112
}
113
98
-func marshalLinks(links []Link) (s string) {
114
+func marshalLinks(w io.Writer, links []Link) {
115
+ fmt.Fprintln(w, "Hash\tSize\tName\t")
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)
121
}
102
- return s
122
}
core/commands/object.go
+7
-3
@@ -8,6 +8,7 @@ import (
8
"io"
9
"io/ioutil"
10
"strings"
11
+ "text/tabwriter"
12
13
mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
14
@@ -120,8 +121,11 @@ multihash.
121
Marshalers: cmds.MarshalerMap{
122
cmds.Text: func(res cmds.Response) (io.Reader, error) {
123
object := res.Output().(*Object)
123
- marshalled := marshalLinks(object.Links)
124
- return strings.NewReader(marshalled), nil
124
+ var buf bytes.Buffer
125
+ w := tabwriter.NewWriter(&buf, 1, 2, 1, ' ', 0)
126
+ marshalLinks(w, object.Links)
127
+ w.Flush()
128
+ return &buf, nil
129
},
130
},
131
Type: Object{},
@@ -246,7 +250,7 @@ var objectStatCmd = &cmds.Command{
250
251
var buf bytes.Buffer
252
w := func(s string, n int) {
249
- buf.Write([]byte(fmt.Sprintf("%s: %d\n", s, n)))
253
+ fmt.Fprintf(&buf, "%s: %d\n", s, n)
254
}
255
w("NumLinks", ns.NumLinks)
256
w("BlockSize", ns.BlockSize)