ls.go 1.13 KB
Newer Older
Matt Bell's avatar
Matt Bell committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
package commands

import (
	"fmt"

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

type Link struct {
	Name, Hash string
	Size       uint64
}

var ls = &cmds.Command{
	Help: "TODO",
	Run: func(res cmds.Response, req cmds.Request) {
		node := req.Context().Node
		output := make(map[string][]Link, len(req.Arguments()))

		for _, path := range req.Arguments() {
			dagnode, err := node.Resolver.ResolvePath(path)
			if err != nil {
				res.SetError(err, cmds.ErrNormal)
				return
			}

			output[path] = make([]Link, len(dagnode.Links))
			for i, link := range dagnode.Links {
				output[path][i] = Link{
					Name: link.Name,
					Hash: link.Hash.B58String(),
					Size: link.Size,
				}
			}
		}

		res.SetValue(output)
	},
	Format: func(res cmds.Response) (string, error) {
		s := ""
		output := res.Value().(*map[string][]Link)

		for path, links := range *output {
			if len(*output) > 1 {
				s += fmt.Sprintf("%s:\n", path)
			}

			for _, link := range links {
				s += fmt.Sprintf("-> %s %s (%v bytes)\n", link.Name, link.Hash, link.Size)
			}

			if len(*output) > 1 {
				s += "\n"
			}
		}

		return s, nil
	},
	Type: &map[string][]Link{},
}