From 1cf151ec6f9ab846e5e4158b05da3b8c6f2621eb Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Mon, 4 Mar 2019 20:29:16 -0800 Subject: [PATCH] fix ls command to use the new coreinterface types See: https://github.com/ipfs/interface-go-ipfs-core/pull/14 License: MIT Signed-off-by: Steven Allen --- core/commands/ls.go | 42 +++++++++++++++++++++++++++++------------- core/coreapi/unixfs.go | 23 ++++++++++++----------- 2 files changed, 41 insertions(+), 24 deletions(-) diff --git a/core/commands/ls.go b/core/commands/ls.go index eb86bb2a7ae1..676db91ae58b 100644 --- a/core/commands/ls.go +++ b/core/commands/ls.go @@ -12,6 +12,8 @@ import ( cmds "gx/ipfs/QmQkW9fnCsg9SLHdViiAh6qfBppodsPZVpU92dZLqYtEfs/go-ipfs-cmds" iface "gx/ipfs/QmbSzsh2WV1ddj8i1MgEB4WEkFj6cLeCtvAV9pXFeVEPem/interface-go-ipfs-core" options "gx/ipfs/QmbSzsh2WV1ddj8i1MgEB4WEkFj6cLeCtvAV9pXFeVEPem/interface-go-ipfs-core/options" + unixfs "gx/ipfs/QmcYUTQ7tBZeH1CLsZM2S3xhMEZdvUgXvbjhpMsLDpk3oJ/go-unixfs" + unixfs_pb "gx/ipfs/QmcYUTQ7tBZeH1CLsZM2S3xhMEZdvUgXvbjhpMsLDpk3oJ/go-unixfs/pb" cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit" ) @@ -19,7 +21,7 @@ import ( type LsLink struct { Name, Hash string Size uint64 - Type iface.FileType + Type unixfs_pb.Data_DataType } // LsObject is an element of LsOutput @@ -144,12 +146,21 @@ 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, + Type: ftype, } if err := processLink(paths[i], lsLink); err != nil { return err @@ -227,15 +238,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) diff --git a/core/coreapi/unixfs.go b/core/coreapi/unixfs.go index 7fab24392438..c0056b44414b 100644 --- a/core/coreapi/unixfs.go +++ b/core/coreapi/unixfs.go @@ -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: // ` ` -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 @@ -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 @@ -209,8 +210,8 @@ func (api *UnixfsAPI) processLink(ctx context.Context, linkres ft.LinkResult, se 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) @@ -226,8 +227,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}}