@cryptotaxi247 / kubo / commits / 708e47fcb

ipfs blocks local command

ipfs blocks local returns _all_ local refs. For now this is one long op. future commits will make it async.

Juan Batiz-Benet committed Jan 9, 2015 at 23:27 UTC 708e47fcbc8ef3bfb6ddd11bcae4130ba5b72125
1 file changed +45 -2
core/commands/refs.go
+45 -2
@@ -31,7 +31,7 @@ func KeyListTextMarshaler(res cmds.Response) (io.Reader, error) {
31
32 var RefsCmd = &cmds.Command{
33 Helptext: cmds.HelpText{
34 - Tagline: "Lists link hashes from an object",
34 + Tagline: "Lists links (references) from an object",
35 ShortDescription: `
36 Retrieves the object named by <ipfs-path> and displays the link
37 hashes it contains, with the following format:
@@ -41,7 +41,9 @@ hashes it contains, with the following format:
41 Note: list all refs recursively with -r.
42 `,
43 },
44 -
44 + Subcommands: map[string]*cmds.Command{
45 + "local": RefsLocalCmd,
46 + },
47 Arguments: []cmds.Argument{
48 cmds.StringArg("ipfs-path", true, true, "Path to the object(s) to list refs from"),
49 },
@@ -102,6 +104,47 @@ Note: list all refs recursively with -r.
104 if _, err := rw.WriteRefs(o); err != nil {
105 log.Error(err)
106 eptr.SetError(err)
107 + return
108 + }
109 + }
110 + }()
111 +
112 + return eptr, nil
113 + },
114 +}
115 +
116 +var RefsLocalCmd = &cmds.Command{
117 + Helptext: cmds.HelpText{
118 + Tagline: "Lists all local references",
119 + ShortDescription: `
120 +Displays the hashes of all local objects.
121 +`,
122 + },
123 +
124 + Run: func(req cmds.Request) (interface{}, error) {
125 + n, err := req.Context().GetNode()
126 + if err != nil {
127 + return nil, err
128 + }
129 +
130 + // todo: make async
131 + allKeys, err := n.Blockstore.AllKeys(0, 0)
132 + if err != nil {
133 + return nil, err
134 + }
135 +
136 + piper, pipew := io.Pipe()
137 + eptr := &ErrPassThroughReader{R: piper}
138 +
139 + go func() {
140 + defer pipew.Close()
141 +
142 + for _, k := range allKeys {
143 + s := k.Pretty() + "\n"
144 + if _, err := pipew.Write([]byte(s)); err != nil {
145 + log.Error(err)
146 + eptr.SetError(err)
147 + return
148 }
149 }
150 }()