templates for block commands (updates #138)
Henry committed
Oct 13, 2014 at 20:31 UTC
08bb6f149f136529c57fdc94c449f09534e49f86
3 files changed
+102
cmd/ipfs/block.go
new
+57
@@ -0,0 +1,57 @@
1
+package main
2
+
3
+import (
4
+ flag "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag"
5
+ commander "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander"
6
+ "github.com/jbenet/go-ipfs/core/commands"
7
+)
8
+
9
+var cmdIpfsBlock = &commander.Command{
10
+ UsageLine: "block",
11
+ Short: "get/put **raw** ipfs blocks",
12
+ Long: `ipfs block (get|put) - get/put **raw** ipfs blocks.
13
+
14
+ ipfs block get <key> > valfile - get block of <key> and write it to valfile
15
+ ipfs block put <key> < valfile - pipe valfile to block <key>
16
+Examples:
17
+
18
+ Get the value of the 'datastore.path' key:
19
+
20
+ ipfs config datastore.path
21
+
22
+ Set the value of the 'datastore.path' key:
23
+
24
+ ipfs config datastore.path ~/.go-ipfs/datastore
25
+
26
+`,
27
+ // Run: blockGetCmd,
28
+ Subcommands: []*commander.Command{
29
+ cmdIpfsBlockGet,
30
+ cmdIpfsBlockPut,
31
+ },
32
+ Flag: *flag.NewFlagSet("ipfs-block", flag.ExitOnError),
33
+}
34
+
35
+var cmdIpfsBlockGet = &commander.Command{
36
+ UsageLine: "get",
37
+ Short: "get a row ipfs block",
38
+ Run: makeCommand(command{
39
+ name: "get",
40
+ args: 1,
41
+ flags: nil,
42
+ online: true,
43
+ cmdFn: commands.BlockGet,
44
+ }),
45
+}
46
+
47
+var cmdIpfsBlockPut = &commander.Command{
48
+ UsageLine: "put",
49
+ Short: "put a row ipfs block",
50
+ Run: makeCommand(command{
51
+ name: "put",
52
+ args: 1,
53
+ flags: nil,
54
+ online: true,
55
+ cmdFn: commands.BlockPut,
56
+ }),
57
+}
cmd/ipfs/ipfs.go
+1
@@ -62,6 +62,7 @@ Use "ipfs help <command>" for more information about a command.
62
cmdIpfsName,
63
cmdIpfsBootstrap,
64
cmdIpfsDiag,
65
+ cmdIpfsBlock,
66
},
67
Flag: *flag.NewFlagSet("ipfs", flag.ExitOnError),
68
}
core/commands/block.go
new
+44
@@ -0,0 +1,44 @@
1
+package commands
2
+
3
+import (
4
+ "io"
5
+ "io/ioutil"
6
+ "os"
7
+
8
+ "github.com/jbenet/go-ipfs/blocks"
9
+ "github.com/jbenet/go-ipfs/core"
10
+ u "github.com/jbenet/go-ipfs/util"
11
+)
12
+
13
+func BlockGet(n *core.IpfsNode, args []string, opts map[string]interface{}, out io.Writer) error {
14
+ k := u.Key(args[0])
15
+ u.PErr("Getting block[%s]\n", k)
16
+
17
+ b, err := n.Blocks.GetBlock(k)
18
+ if err != nil {
19
+ return err
20
+ }
21
+
22
+ out.Write(b.Data)
23
+ return nil
24
+}
25
+
26
+func BlockPut(n *core.IpfsNode, args []string, opts map[string]interface{}, out io.Writer) error {
27
+
28
+ data, err := ioutil.ReadAll(os.Stdin)
29
+ if err != nil {
30
+ return err
31
+ }
32
+
33
+ b := blocks.NewBlock(data)
34
+ u.PErr("Putting block[%s]\n", b.Key())
35
+
36
+ key, err := n.Blocks.AddBlock(b)
37
+ if err != nil {
38
+ return err
39
+ }
40
+
41
+ u.PErr("Done. Key: %s\n", key)
42
+
43
+ return nil
44
+}