mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-20 02:21:48 +08:00
templates for block commands (updates #138)
This commit is contained in:
57
cmd/ipfs/block.go
Normal file
57
cmd/ipfs/block.go
Normal file
@ -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,
|
||||
}),
|
||||
}
|
@ -62,6 +62,7 @@ Use "ipfs help <command>" for more information about a command.
|
||||
cmdIpfsName,
|
||||
cmdIpfsBootstrap,
|
||||
cmdIpfsDiag,
|
||||
cmdIpfsBlock,
|
||||
},
|
||||
Flag: *flag.NewFlagSet("ipfs", flag.ExitOnError),
|
||||
}
|
||||
|
44
core/commands/block.go
Normal file
44
core/commands/block.go
Normal file
@ -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
|
||||
}
|
Reference in New Issue
Block a user