block.go 774 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 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
}