From 08bb6f149f136529c57fdc94c449f09534e49f86 Mon Sep 17 00:00:00 2001
From: Henry <cryptix@riseup.net>
Date: Mon, 13 Oct 2014 20:31:14 +0200
Subject: [PATCH] templates for block commands (updates #138)

---
 cmd/ipfs/block.go      | 57 ++++++++++++++++++++++++++++++++++++++++++
 cmd/ipfs/ipfs.go       |  1 +
 core/commands/block.go | 44 ++++++++++++++++++++++++++++++++
 3 files changed, 102 insertions(+)
 create mode 100644 cmd/ipfs/block.go
 create mode 100644 core/commands/block.go

diff --git a/cmd/ipfs/block.go b/cmd/ipfs/block.go
new file mode 100644
index 000000000..870bcf67d
--- /dev/null
+++ b/cmd/ipfs/block.go
@@ -0,0 +1,57 @@
+package main
+
+import (
+	flag "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag"
+	commander "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander"
+	"github.com/jbenet/go-ipfs/core/commands"
+)
+
+var cmdIpfsBlock = &commander.Command{
+	UsageLine: "block",
+	Short:     "get/put **raw** ipfs blocks",
+	Long: `ipfs block (get|put) - get/put **raw** ipfs blocks.
+
+    ipfs block get <key> > valfile    - get block of <key> and write it to valfile
+    ipfs block put <key>  < valfile   - pipe valfile to block <key>
+Examples:
+
+  Get the value of the 'datastore.path' key:
+
+      ipfs config datastore.path
+
+  Set the value of the 'datastore.path' key:
+
+      ipfs config datastore.path ~/.go-ipfs/datastore
+
+`,
+	// Run: blockGetCmd,
+	Subcommands: []*commander.Command{
+		cmdIpfsBlockGet,
+		cmdIpfsBlockPut,
+	},
+	Flag: *flag.NewFlagSet("ipfs-block", flag.ExitOnError),
+}
+
+var cmdIpfsBlockGet = &commander.Command{
+	UsageLine: "get",
+	Short:     "get a row ipfs block",
+	Run: makeCommand(command{
+		name:   "get",
+		args:   1,
+		flags:  nil,
+		online: true,
+		cmdFn:  commands.BlockGet,
+	}),
+}
+
+var cmdIpfsBlockPut = &commander.Command{
+	UsageLine: "put",
+	Short:     "put a row ipfs block",
+	Run: makeCommand(command{
+		name:   "put",
+		args:   1,
+		flags:  nil,
+		online: true,
+		cmdFn:  commands.BlockPut,
+	}),
+}
diff --git a/cmd/ipfs/ipfs.go b/cmd/ipfs/ipfs.go
index 5935b090f..96a463cba 100644
--- a/cmd/ipfs/ipfs.go
+++ b/cmd/ipfs/ipfs.go
@@ -62,6 +62,7 @@ Use "ipfs help <command>" for more information about a command.
 		cmdIpfsName,
 		cmdIpfsBootstrap,
 		cmdIpfsDiag,
+		cmdIpfsBlock,
 	},
 	Flag: *flag.NewFlagSet("ipfs", flag.ExitOnError),
 }
diff --git a/core/commands/block.go b/core/commands/block.go
new file mode 100644
index 000000000..0281c86bc
--- /dev/null
+++ b/core/commands/block.go
@@ -0,0 +1,44 @@
+package commands
+
+import (
+	"io"
+	"io/ioutil"
+	"os"
+
+	"github.com/jbenet/go-ipfs/blocks"
+	"github.com/jbenet/go-ipfs/core"
+	u "github.com/jbenet/go-ipfs/util"
+)
+
+func BlockGet(n *core.IpfsNode, args []string, opts map[string]interface{}, out io.Writer) error {
+	k := u.Key(args[0])
+	u.PErr("Getting block[%s]\n", k)
+
+	b, err := n.Blocks.GetBlock(k)
+	if err != nil {
+		return err
+	}
+
+	out.Write(b.Data)
+	return nil
+}
+
+func BlockPut(n *core.IpfsNode, args []string, opts map[string]interface{}, out io.Writer) error {
+
+	data, err := ioutil.ReadAll(os.Stdin)
+	if err != nil {
+		return err
+	}
+
+	b := blocks.NewBlock(data)
+	u.PErr("Putting block[%s]\n", b.Key())
+
+	key, err := n.Blocks.AddBlock(b)
+	if err != nil {
+		return err
+	}
+
+	u.PErr("Done. Key: %s\n", key)
+
+	return nil
+}
-- 
GitLab