Skip to content

Commit

Permalink
Merge pull request #6051 from ipfs/feat/nicer-ls
Browse files Browse the repository at this point in the history
fix ls command to use the new coreinterface types
  • Loading branch information
Stebalien committed Mar 7, 2019
2 parents f24ba4d + c6d85c2 commit 245c40b
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 29 deletions.
46 changes: 32 additions & 14 deletions core/commands/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (

cmdkit "github.com/ipfs/go-ipfs-cmdkit"
cmds "github.com/ipfs/go-ipfs-cmds"
unixfs "github.com/ipfs/go-unixfs"
unixfs_pb "github.com/ipfs/go-unixfs/pb"
iface "github.com/ipfs/interface-go-ipfs-core"
options "github.com/ipfs/interface-go-ipfs-core/options"
)
Expand All @@ -19,7 +21,8 @@ import (
type LsLink struct {
Name, Hash string
Size uint64
Type iface.FileType
Type unixfs_pb.Data_DataType
Target string
}

// LsObject is an element of LsOutput
Expand Down Expand Up @@ -144,12 +147,22 @@ The JSON output contains type information.
if link.Err != nil {
return link.Err
}
var ftype unixfs_pb.Data_DataType
switch link.Type {
case iface.TFile:
ftype = unixfs.TFile
case iface.TDirectory:
ftype = unixfs.TDirectory
case iface.TSymlink:
ftype = unixfs.TSymlink
}
lsLink := LsLink{
Name: link.Link.Name,
Hash: enc.Encode(link.Link.Cid),
Name: link.Name,
Hash: enc.Encode(link.Cid),

Size: link.Size,
Type: link.Type,
Size: link.Size,
Type: ftype,
Target: link.Target,
}
if err := processLink(paths[i], lsLink); err != nil {
return err
Expand Down Expand Up @@ -227,15 +240,20 @@ func tabularOutput(req *cmds.Request, w io.Writer, out *LsOutput, lastObjectHash
}

for _, link := range object.Links {
s := "%[1]s\t%[3]s\n"

switch {
case link.Type == iface.TDirectory && size:
s = "%[1]s\t-\t%[3]s/\n"
case link.Type == iface.TDirectory && !size:
s = "%[1]s\t%[3]s/\n"
case size:
s = "%s\t%v\t%s\n"
var s string
switch link.Type {
case unixfs.TDirectory, unixfs.THAMTShard, unixfs.TMetadata:
if size {
s = "%[1]s\t-\t%[3]s/\n"
} else {
s = "%[1]s\t%[3]s/\n"
}
default:
if size {
s = "%s\t%v\t%s\n"
} else {
s = "%[1]s\t%[3]s\n"
}
}

fmt.Fprintf(tw, s, link.Hash, link.Size, link.Name)
Expand Down
33 changes: 21 additions & 12 deletions core/coreapi/unixfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func (api *UnixfsAPI) Get(ctx context.Context, p coreiface.Path) (files.Node, er

// Ls returns the contents of an IPFS or IPNS object(s) at path p, with the format:
// `<link base58 hash> <link size in bytes> <link name>`
func (api *UnixfsAPI) Ls(ctx context.Context, p coreiface.Path, opts ...options.UnixfsLsOption) (<-chan coreiface.LsLink, error) {
func (api *UnixfsAPI) Ls(ctx context.Context, p coreiface.Path, opts ...options.UnixfsLsOption) (<-chan coreiface.DirEntry, error) {
settings, err := options.UnixfsLsOptions(opts...)
if err != nil {
return nil, err
Expand All @@ -170,26 +170,27 @@ func (api *UnixfsAPI) Ls(ctx context.Context, p coreiface.Path, opts ...options.
return uses.lsFromLinksAsync(ctx, dir, settings)
}

func (api *UnixfsAPI) processLink(ctx context.Context, linkres ft.LinkResult, settings *options.UnixfsLsSettings) coreiface.LsLink {
lnk := coreiface.LsLink{
Link: linkres.Link,
func (api *UnixfsAPI) processLink(ctx context.Context, linkres ft.LinkResult, settings *options.UnixfsLsSettings) coreiface.DirEntry {
lnk := coreiface.DirEntry{
Name: linkres.Link.Name,
Cid: linkres.Link.Cid,
Err: linkres.Err,
}
if lnk.Err != nil {
return lnk
}

switch lnk.Link.Cid.Type() {
switch lnk.Cid.Type() {
case cid.Raw:
// No need to check with raw leaves
lnk.Type = coreiface.TFile
lnk.Size = lnk.Link.Size
lnk.Size = linkres.Link.Size
case cid.DagProtobuf:
if !settings.ResolveChildren {
break
}

linkNode, err := lnk.Link.GetNode(ctx, api.dag)
linkNode, err := linkres.Link.GetNode(ctx, api.dag)
if err != nil {
lnk.Err = err
break
Expand All @@ -201,16 +202,24 @@ func (api *UnixfsAPI) processLink(ctx context.Context, linkres ft.LinkResult, se
lnk.Err = err
break
}
lnk.Type = coreiface.FileType(d.Type())
switch d.Type() {
case ft.TFile, ft.TRaw:
lnk.Type = coreiface.TFile
case ft.THAMTShard, ft.TDirectory, ft.TMetadata:
lnk.Type = coreiface.TDirectory
case ft.TSymlink:
lnk.Type = coreiface.TSymlink
lnk.Target = string(d.Data())
}
lnk.Size = d.FileSize()
}
}

return lnk
}

func (api *UnixfsAPI) lsFromLinksAsync(ctx context.Context, dir uio.Directory, settings *options.UnixfsLsSettings) (<-chan coreiface.LsLink, error) {
out := make(chan coreiface.LsLink)
func (api *UnixfsAPI) lsFromLinksAsync(ctx context.Context, dir uio.Directory, settings *options.UnixfsLsSettings) (<-chan coreiface.DirEntry, error) {
out := make(chan coreiface.DirEntry)

go func() {
defer close(out)
Expand All @@ -226,8 +235,8 @@ func (api *UnixfsAPI) lsFromLinksAsync(ctx context.Context, dir uio.Directory, s
return out, nil
}

func (api *UnixfsAPI) lsFromLinks(ctx context.Context, ndlinks []*ipld.Link, settings *options.UnixfsLsSettings) (<-chan coreiface.LsLink, error) {
links := make(chan coreiface.LsLink, len(ndlinks))
func (api *UnixfsAPI) lsFromLinks(ctx context.Context, ndlinks []*ipld.Link, settings *options.UnixfsLsSettings) (<-chan coreiface.DirEntry, error) {
links := make(chan coreiface.DirEntry, len(ndlinks))
for _, l := range ndlinks {
lr := ft.LinkResult{Link: &ipld.Link{Name: l.Name, Size: l.Size, Cid: l.Cid}}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ require (
github.com/ipfs/go-unixfs v0.0.1
github.com/ipfs/go-verifcid v0.0.1
github.com/ipfs/hang-fds v0.0.1
github.com/ipfs/interface-go-ipfs-core v0.0.1
github.com/ipfs/interface-go-ipfs-core v0.0.2
github.com/ipfs/iptb v1.4.0
github.com/ipfs/iptb-plugins v0.0.1
github.com/jbenet/go-is-domain v0.0.0-20160119110217-ba9815c809e0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ github.com/ipfs/hang-fds v0.0.1 h1:KGAxiGtJPT3THVRNT6yxgpdFPeX4ZemUjENOt6NlOn4=
github.com/ipfs/hang-fds v0.0.1/go.mod h1:U4JNbzwTpk/qP2Ms4VgrZ4HcgJGVosBJqMXvwe4udSY=
github.com/ipfs/interface-go-ipfs-core v0.0.1 h1:IlAKkUPyw77UECt25WymL72A4KO+BGCMULKfMfc2R5g=
github.com/ipfs/interface-go-ipfs-core v0.0.1/go.mod h1:CbFOGVGV8B4NCA0fAO2VVZ1Jt/ZQYE3FzTC6nLVqiAE=
github.com/ipfs/interface-go-ipfs-core v0.0.2 h1:Qe6pzIQYEFLC2/YmfH3L7uWzcYVG12173BhRJKwplPk=
github.com/ipfs/interface-go-ipfs-core v0.0.2/go.mod h1:CbFOGVGV8B4NCA0fAO2VVZ1Jt/ZQYE3FzTC6nLVqiAE=
github.com/ipfs/iptb v1.4.0 h1:YFYTrCkLMRwk/35IMyC6+yjoQSHTEcNcefBStLJzgvo=
github.com/ipfs/iptb v1.4.0/go.mod h1:1rzHpCYtNp87/+hTxG5TfCVn/yMY3dKnLn8tBiMfdmg=
github.com/ipfs/iptb-plugins v0.0.1 h1:aUHbQ4y8/lKIBX/FN0KXe3c4NldPLsq7VyW2CcFXbhE=
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -608,9 +608,9 @@
},
{
"author": "magik6k",
"hash": "QmXLwxifxwfc2bAwq6rdjbYqAsGzWsDE9RM5TWMGtykyj6",
"hash": "QmY1veddRDFZZBUFp2Mysw7mf2sSmbx1ZGH5v11tTMCYN5",
"name": "interface-go-ipfs-core",
"version": "0.1.11"
"version": "0.1.12"
}
],
"gxVersion": "0.10.0",
Expand Down

0 comments on commit 245c40b

Please sign in to comment.