ls.go 1.26 KB
Newer Older
Matt Bell's avatar
Matt Bell committed
1 2 3 4 5 6 7 8 9 10 11 12 13
package commands

import (
	"fmt"

	cmds "github.com/jbenet/go-ipfs/commands"
)

type Link struct {
	Name, Hash string
	Size       uint64
}

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

type LsOutput struct {
	Objects []Object
}

Matt Bell's avatar
Matt Bell committed
23 24 25 26
var ls = &cmds.Command{
	Help: "TODO",
	Run: func(res cmds.Response, req cmds.Request) {
		node := req.Context().Node
27
		output := make([]Object, len(req.Arguments()))
Matt Bell's avatar
Matt Bell committed
28

29
		for i, path := range req.Arguments() {
Matt Bell's avatar
Matt Bell committed
30 31 32 33 34 35
			dagnode, err := node.Resolver.ResolvePath(path)
			if err != nil {
				res.SetError(err, cmds.ErrNormal)
				return
			}

36 37 38 39 40 41
			output[i] = Object{
				Hash:  path,
				Links: make([]Link, len(dagnode.Links)),
			}
			for j, link := range dagnode.Links {
				output[i].Links[j] = Link{
Matt Bell's avatar
Matt Bell committed
42 43 44 45 46 47 48
					Name: link.Name,
					Hash: link.Hash.B58String(),
					Size: link.Size,
				}
			}
		}

49
		res.SetValue(&LsOutput{output})
Matt Bell's avatar
Matt Bell committed
50 51 52
	},
	Format: func(res cmds.Response) (string, error) {
		s := ""
53
		output := res.Value().(*LsOutput).Objects
Matt Bell's avatar
Matt Bell committed
54

55 56 57
		for _, object := range output {
			if len(output) > 1 {
				s += fmt.Sprintf("%s:\n", object.Hash)
Matt Bell's avatar
Matt Bell committed
58 59
			}

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

64
			if len(output) > 1 {
Matt Bell's avatar
Matt Bell committed
65 66 67 68 69 70
				s += "\n"
			}
		}

		return s, nil
	},
71
	Type: &LsOutput{},
Matt Bell's avatar
Matt Bell committed
72
}