ls.go 1011 Bytes
Newer Older
1 2 3
package main

import (
4 5
	"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag"
	"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander"
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
	u "github.com/jbenet/go-ipfs/util"
)

var cmdIpfsLs = &commander.Command{
	UsageLine: "ls",
	Short:     "List links from an object.",
	Long: `ipfs ls <ipfs-path> - List links from an object.

    Retrieves the object named by <ipfs-path> and displays the links
    it contains, with the following format:

    <link base58 hash> <link size in bytes> <link name>

`,
	Run:  lsCmd,
	Flag: *flag.NewFlagSet("ipfs-ls", flag.ExitOnError),
}

func lsCmd(c *commander.Command, inp []string) error {
	if len(inp) < 1 {
		u.POut(c.Long)
		return nil
	}

30
	n, err := localNode(false)
31 32 33 34
	if err != nil {
		return err
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
35
	for _, fn := range inp {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
36
		nd, err := n.Resolver.ResolvePath(fn)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
37 38 39 40 41 42 43
		if err != nil {
			return err
		}

		for _, link := range nd.Links {
			u.POut("%s %d %s\n", link.Hash.B58String(), link.Size, link.Name)
		}
44 45 46
	}
	return nil
}