Commit 707f7018 authored by Juan Batiz-Benet's avatar Juan Batiz-Benet

Merge pull request #165 from cryptix/blockCmd

added block cmd
parents 1a4bd39f 03e42d8e
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: "manipulate raw ipfs blocks",
Long: `ipfs block - manipulate raw ipfs blocks
ipfs block get <key> - get and output block named by <key>
ipfs block put - store stdin as a block, outputs <key>
ipfs block is a plumbing command used to manipulate raw ipfs blocks.
Reads from stdin or writes to stdout, and <key> is a base58 encoded
multihash.`,
// Run: blockGetCmd,
Subcommands: []*commander.Command{
cmdIpfsBlockGet,
cmdIpfsBlockPut,
},
Flag: *flag.NewFlagSet("ipfs-block", flag.ExitOnError),
}
var cmdIpfsBlockGet = &commander.Command{
UsageLine: "get <key>",
Short: "get and output block named by <key>",
Long: `ipfs get <key> - get and output block named by <key>
ipfs block get is a plumbing command for retreiving raw ipfs blocks.
It outputs to stdout, and <key> is a base58 encoded multihash.`,
Run: makeCommand(command{
name: "blockGet",
args: 1,
flags: nil,
online: true,
cmdFn: commands.BlockGet,
}),
}
var cmdIpfsBlockPut = &commander.Command{
UsageLine: "put",
Short: "store stdin as a block, outputs <key>",
Long: `ipfs put - store stdin as a block, outputs <key>
ipfs block put is a plumbing command for storing raw ipfs blocks.
It reads from stding, and <key> is a base58 encoded multihash.`,
Run: makeCommand(command{
name: "blockPut",
args: 0,
flags: nil,
online: true,
cmdFn: commands.BlockPut,
}),
}
......@@ -62,6 +62,7 @@ Use "ipfs help <command>" for more information about a command.
cmdIpfsName,
cmdIpfsBootstrap,
cmdIpfsDiag,
cmdIpfsBlock,
},
Flag: *flag.NewFlagSet("ipfs", flag.ExitOnError),
}
......
package commands
import (
"fmt"
"io"
"io/ioutil"
"os"
mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
"github.com/jbenet/go-ipfs/blocks"
"github.com/jbenet/go-ipfs/core"
u "github.com/jbenet/go-ipfs/util"
)
// BlockGet retrives a raw ipfs block from the node's BlockService
func BlockGet(n *core.IpfsNode, args []string, opts map[string]interface{}, out io.Writer) error {
if !u.IsValidHash(args[0]) {
return fmt.Errorf("block get: not a valid hash")
}
h, err := mh.FromB58String(args[0])
if err != nil {
return fmt.Errorf("block get: %v", err)
}
k := u.Key(h)
log.Debug("BlockGet key: '%q'", k)
b, err := n.Blocks.GetBlock(k)
if err != nil {
return fmt.Errorf("block get: %v", err)
}
_, err = out.Write(b.Data)
return err
}
// BlockPut reads everything from conn and saves the data to the nodes BlockService
func BlockPut(n *core.IpfsNode, args []string, opts map[string]interface{}, out io.Writer) error {
// TODO: this should read from an io.Reader arg
data, err := ioutil.ReadAll(os.Stdin)
if err != nil {
return err
}
b := blocks.NewBlock(data)
log.Debug("BlockPut key: '%q'", b.Key())
k, err := n.Blocks.AddBlock(b)
if err != nil {
return err
}
fmt.Fprintf(out, "added as '%s'\n", k)
return nil
}
......@@ -133,6 +133,10 @@ func (dl *DaemonListener) handleConnection(conn manet.Conn) {
err = commands.Resolve(dl.node, command.Args, command.Opts, conn)
case "diag":
err = commands.Diag(dl.node, command.Args, command.Opts, conn)
case "blockGet":
err = commands.BlockGet(dl.node, command.Args, command.Opts, conn)
case "blockPut":
err = commands.BlockPut(dl.node, command.Args, command.Opts, conn)
default:
err = fmt.Errorf("Invalid Command: '%s'", command.Command)
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment