github.com/zignig/go-ipfs@v0.0.0-20141111235910-c9e5fdf55a52/core/commands2/ls.go (about) 1 package commands 2 3 import ( 4 "fmt" 5 6 cmds "github.com/jbenet/go-ipfs/commands" 7 "github.com/jbenet/go-ipfs/core/commands2/internal" 8 merkledag "github.com/jbenet/go-ipfs/merkledag" 9 ) 10 11 type Link struct { 12 Name, Hash string 13 Size uint64 14 } 15 16 type Object struct { 17 Hash string 18 Links []Link 19 } 20 21 type LsOutput struct { 22 Objects []Object 23 } 24 25 var lsCmd = &cmds.Command{ 26 Description: "List links from an object.", 27 Help: `Retrieves the object named by <ipfs-path> and displays the links 28 it contains, with the following format: 29 30 <link base58 hash> <link size in bytes> <link name> 31 `, 32 33 Arguments: []cmds.Argument{ 34 cmds.StringArg("ipfs-path", false, true, "The path to the IPFS object(s) to list links from"), 35 }, 36 Run: func(req cmds.Request) (interface{}, error) { 37 node := req.Context().Node 38 39 paths, err := internal.CastToStrings(req.Arguments()) 40 if err != nil { 41 return nil, err 42 } 43 44 dagnodes := make([]*merkledag.Node, 0) 45 for _, path := range paths { 46 dagnode, err := node.Resolver.ResolvePath(path) 47 if err != nil { 48 return nil, err 49 } 50 dagnodes = append(dagnodes, dagnode) 51 } 52 53 output := make([]Object, len(req.Arguments())) 54 for i, dagnode := range dagnodes { 55 output[i] = Object{ 56 Hash: paths[i], 57 Links: make([]Link, len(dagnode.Links)), 58 } 59 for j, link := range dagnode.Links { 60 output[i].Links[j] = Link{ 61 Name: link.Name, 62 Hash: link.Hash.B58String(), 63 Size: link.Size, 64 } 65 } 66 } 67 68 return &LsOutput{output}, nil 69 }, 70 Marshallers: map[cmds.EncodingType]cmds.Marshaller{ 71 cmds.Text: func(res cmds.Response) ([]byte, error) { 72 s := "" 73 output := res.Output().(*LsOutput).Objects 74 75 for _, object := range output { 76 if len(output) > 1 { 77 s += fmt.Sprintf("%s:\n", object.Hash) 78 } 79 80 for _, link := range object.Links { 81 s += fmt.Sprintf("%s %v %s\n", link.Hash, link.Size, link.Name) 82 } 83 84 if len(output) > 1 { 85 s += "\n" 86 } 87 } 88 89 return []byte(s), nil 90 }, 91 }, 92 Type: &LsOutput{}, 93 }