ls.go 2.03 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 29
	Helptext: cmds.HelpText{
		Tagline: "List links from an object.",
		ShortDescription: `
Retrieves the object named by <ipfs-path> and displays the links
30 31 32
it contains, with the following format:

  <link base58 hash> <link size in bytes> <link name>
33
`,
34
	},
35

36
	Arguments: []cmds.Argument{
Brian Tiger Chow's avatar
Brian Tiger Chow committed
37
		cmds.StringArg("ipfs-path", true, true, "The path to the IPFS object(s) to list links from"),
38
	},
39
	Run: func(req cmds.Request) (interface{}, error) {
40 41 42 43
		node, err := req.Context().GetNode()
		if err != nil {
			return nil, err
		}
Matt Bell's avatar
Matt Bell committed
44

45
		paths, err := internal.CastToStrings(req.Arguments())
Brian Tiger Chow's avatar
Brian Tiger Chow committed
46
		if err != nil {
47
			return nil, err
48 49 50 51
		}

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

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

74
		return &LsOutput{output}, nil
Matt Bell's avatar
Matt Bell committed
75
	},
76 77 78 79
	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
80

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

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

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

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