ls.go 2.05 KB
Newer Older
Matt Bell's avatar
Matt Bell committed
1 2 3 4 5 6
package commands

import (
	"fmt"

	cmds "github.com/jbenet/go-ipfs/commands"
Brian Tiger Chow's avatar
Brian Tiger Chow committed
7
	"github.com/jbenet/go-ipfs/core/commands2/internal"
8
	merkledag "github.com/jbenet/go-ipfs/merkledag"
Matt Bell's avatar
Matt Bell committed
9 10 11 12 13 14 15
)

type Link struct {
	Name, Hash string
	Size       uint64
}

16 17 18 19 20 21 22 23 24
type Object struct {
	Hash  string
	Links []Link
}

type LsOutput struct {
	Objects []Object
}

25
var lsCmd = &cmds.Command{
26 27 28
	Arguments: []cmds.Argument{
		cmds.Argument{"object", cmds.ArgString, false, true},
	},
Brian Tiger Chow's avatar
Brian Tiger Chow committed
29 30 31 32 33 34 35 36 37 38
	// TODO UsageLine: "ls",
	// TODO Short:     "List links from an object.",
	Help: `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>

`,
Matt Bell's avatar
Matt Bell committed
39 40 41
	Run: func(res cmds.Response, req cmds.Request) {
		node := req.Context().Node

Brian Tiger Chow's avatar
Brian Tiger Chow committed
42 43 44 45
		paths, err := internal.ToStrings(req.Arguments())
		if err != nil {
			res.SetError(err, cmds.ErrNormal)
			return
46 47 48 49
		}

		dagnodes := make([]*merkledag.Node, 0)
		for _, path := range paths {
Matt Bell's avatar
Matt Bell committed
50 51 52 53 54
			dagnode, err := node.Resolver.ResolvePath(path)
			if err != nil {
				res.SetError(err, cmds.ErrNormal)
				return
			}
55 56
			dagnodes = append(dagnodes, dagnode)
		}
Matt Bell's avatar
Matt Bell committed
57

58 59
		output := make([]Object, len(req.Arguments()))
		for i, dagnode := range dagnodes {
60
			output[i] = Object{
61
				Hash:  paths[i],
62 63 64 65
				Links: make([]Link, len(dagnode.Links)),
			}
			for j, link := range dagnode.Links {
				output[i].Links[j] = Link{
Matt Bell's avatar
Matt Bell committed
66 67 68 69 70 71 72
					Name: link.Name,
					Hash: link.Hash.B58String(),
					Size: link.Size,
				}
			}
		}

73
		res.SetOutput(&LsOutput{output})
Matt Bell's avatar
Matt Bell committed
74
	},
75 76 77 78
	Marshallers: map[cmds.EncodingType]cmds.Marshaller{
		cmds.Text: func(res cmds.Response) ([]byte, error) {
			s := ""
			output := res.Output().(*LsOutput).Objects
Matt Bell's avatar
Matt Bell committed
79

80 81 82 83
			for _, object := range output {
				if len(output) > 1 {
					s += fmt.Sprintf("%s:\n", object.Hash)
				}
Matt Bell's avatar
Matt Bell committed
84

85 86 87
				for _, link := range object.Links {
					s += fmt.Sprintf("-> %s %s (%v bytes)\n", link.Name, link.Hash, link.Size)
				}
Matt Bell's avatar
Matt Bell committed
88

89 90 91
				if len(output) > 1 {
					s += "\n"
				}
Matt Bell's avatar
Matt Bell committed
92 93
			}

94 95
			return []byte(s), nil
		},
Matt Bell's avatar
Matt Bell committed
96
	},
97
	Type: &LsOutput{},
Matt Bell's avatar
Matt Bell committed
98
}