Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added block cmd #165

Merged
merged 8 commits into from
Oct 14, 2014
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions cmd/ipfs/block.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
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 < valfile - saves the conents of valfile and returns it's <key>
`,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's do:

  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",
Short: "get a raw ipfs block",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  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: "put a raw ipfs block",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  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,
}),
}
1 change: 1 addition & 0 deletions cmd/ipfs/ipfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ Use "ipfs help <command>" for more information about a command.
cmdIpfsName,
cmdIpfsBootstrap,
cmdIpfsDiag,
cmdIpfsBlock,
},
Flag: *flag.NewFlagSet("ipfs", flag.ExitOnError),
}
Expand Down
55 changes: 55 additions & 0 deletions core/commands/block.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
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 {
data, err := ioutil.ReadAll(os.Stdin)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add:

// TODO: this should read from an io.Reader arg

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
}
4 changes: 4 additions & 0 deletions daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down