Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-dms3
Commits
607468a9
Commit
607468a9
authored
10 years ago
by
Henry
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
beautify 'ipfs ls' and 'ipfs object links' (updates #799)
parent
dd588785
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
39 additions
and
16 deletions
+39
-16
core/commands/ls.go
core/commands/ls.go
+32
-13
core/commands/object.go
core/commands/object.go
+7
-3
No files found.
core/commands/ls.go
View file @
607468a9
package
commands
import
(
"bytes"
"fmt"
"io"
"
strings
"
"
text/tabwriter
"
cmds
"github.com/jbenet/go-ipfs/commands"
merkledag
"github.com/jbenet/go-ipfs/merkledag"
path
"github.com/jbenet/go-ipfs/path"
"github.com/jbenet/go-ipfs/unixfs"
unixfspb
"github.com/jbenet/go-ipfs/unixfs/pb"
)
type
Link
struct
{
Name
,
Hash
string
Size
uint64
IsDir
bool
}
type
Object
struct
{
...
...
@@ -64,10 +68,21 @@ it contains, with the following format:
Links
:
make
([]
Link
,
len
(
dagnode
.
Links
)),
}
for
j
,
link
:=
range
dagnode
.
Links
{
link
.
Node
,
err
=
link
.
GetNode
(
node
.
DAG
)
if
err
!=
nil
{
res
.
SetError
(
err
,
cmds
.
ErrNormal
)
return
}
d
,
err
:=
unixfs
.
FromBytes
(
link
.
Node
.
Data
)
if
err
!=
nil
{
res
.
SetError
(
err
,
cmds
.
ErrNormal
)
return
}
output
[
i
]
.
Links
[
j
]
=
Link
{
Name
:
link
.
Name
,
Hash
:
link
.
Hash
.
B58String
(),
Size
:
link
.
Size
,
Name
:
link
.
Name
,
Hash
:
link
.
Hash
.
B58String
(),
Size
:
link
.
Size
,
IsDir
:
d
.
GetType
()
==
unixfspb
.
Data_Directory
,
}
}
}
...
...
@@ -76,28 +91,32 @@ it contains, with the following format:
},
Marshalers
:
cmds
.
MarshalerMap
{
cmds
.
Text
:
func
(
res
cmds
.
Response
)
(
io
.
Reader
,
error
)
{
s
:=
""
output
:=
res
.
Output
()
.
(
*
LsOutput
)
.
Objects
var
buf
bytes
.
Buffer
w
:=
tabwriter
.
NewWriter
(
&
buf
,
1
,
2
,
1
,
' '
,
0
)
for
_
,
object
:=
range
output
{
if
len
(
output
)
>
1
{
s
+=
fmt
.
S
printf
(
"%s:
\n
"
,
object
.
Hash
)
fmt
.
F
printf
(
w
,
"%s:
\n
"
,
object
.
Hash
)
}
s
+=
marshalLinks
(
object
.
Links
)
marshalLinks
(
w
,
object
.
Links
)
if
len
(
output
)
>
1
{
s
+=
"
\n
"
fmt
.
Fprintln
(
w
)
}
}
w
.
Flush
()
return
strings
.
NewReader
(
s
)
,
nil
return
&
buf
,
nil
},
},
Type
:
LsOutput
{},
}
func
marshalLinks
(
links
[]
Link
)
(
s
string
)
{
func
marshalLinks
(
w
io
.
Writer
,
links
[]
Link
)
{
fmt
.
Fprintln
(
w
,
"Hash
\t
Size
\t
Name
\t
"
)
for
_
,
link
:=
range
links
{
s
+=
fmt
.
Sprintf
(
"%s %v %s
\n
"
,
link
.
Hash
,
link
.
Size
,
link
.
Name
)
if
link
.
IsDir
{
link
.
Name
+=
"/"
}
fmt
.
Fprintf
(
w
,
"%s
\t
%v
\t
%s
\t\n
"
,
link
.
Hash
,
link
.
Size
,
link
.
Name
)
}
return
s
}
This diff is collapsed.
Click to expand it.
core/commands/object.go
View file @
607468a9
...
...
@@ -8,6 +8,7 @@ import (
"io"
"io/ioutil"
"strings"
"text/tabwriter"
mh
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
...
...
@@ -120,8 +121,11 @@ multihash.
Marshalers
:
cmds
.
MarshalerMap
{
cmds
.
Text
:
func
(
res
cmds
.
Response
)
(
io
.
Reader
,
error
)
{
object
:=
res
.
Output
()
.
(
*
Object
)
marshalled := marshalLinks(object.Links)
return strings.NewReader(marshalled), nil
var
buf
bytes
.
Buffer
w
:=
tabwriter
.
NewWriter
(
&
buf
,
1
,
2
,
1
,
' '
,
0
)
marshalLinks
(
w
,
object
.
Links
)
w
.
Flush
()
return
&
buf
,
nil
},
},
Type
:
Object
{},
...
...
@@ -246,7 +250,7 @@ var objectStatCmd = &cmds.Command{
var
buf
bytes
.
Buffer
w
:=
func
(
s
string
,
n
int
)
{
buf.Write([]byte(
fmt.
S
printf("%s: %d\n", s, n)
))
fmt
.
F
printf
(
&
buf
,
"%s: %d
\n
"
,
s
,
n
)
}
w
(
"NumLinks"
,
ns
.
NumLinks
)
w
(
"BlockSize"
,
ns
.
BlockSize
)
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment