Commit e42c9672 authored by Jeromy's avatar Jeromy

add option to only hash input

License: MIT
Signed-off-by: default avatarJeromy <jeromyj@gmail.com>
parent 3a1c8d7a
......@@ -23,6 +23,7 @@ type NodeBuilder struct {
peerhost HostOption
repo repo.Repo
built bool
nilrepo bool
}
func NewNodeBuilder() *NodeBuilder {
......@@ -33,7 +34,7 @@ func NewNodeBuilder() *NodeBuilder {
}
}
func defaultRepo() (repo.Repo, error) {
func defaultRepo(dstore ds.ThreadSafeDatastore) (repo.Repo, error) {
c := cfg.Config{}
priv, pub, err := ci.GenerateKeyPairWithReader(ci.RSA, 1024, rand.Reader)
if err != nil {
......@@ -56,7 +57,7 @@ func defaultRepo() (repo.Repo, error) {
c.Identity.PrivKey = base64.StdEncoding.EncodeToString(privkeyb)
return &repo.Mock{
D: dsync.MutexWrap(ds.NewMapDatastore()),
D: dstore,
C: c,
}, nil
}
......@@ -86,13 +87,23 @@ func (nb *NodeBuilder) SetRepo(r repo.Repo) *NodeBuilder {
return nb
}
func (nb *NodeBuilder) NilRepo() *NodeBuilder {
nb.nilrepo = true
return nb
}
func (nb *NodeBuilder) Build(ctx context.Context) (*IpfsNode, error) {
if nb.built {
return nil, ErrAlreadyBuilt
}
nb.built = true
if nb.repo == nil {
r, err := defaultRepo()
var d ds.Datastore
d = ds.NewMapDatastore()
if nb.nilrepo {
d = ds.NewNullDatastore()
}
r, err := defaultRepo(dsync.MutexWrap(d))
if err != nil {
return nil, err
}
......
......@@ -58,6 +58,7 @@ remains to be implemented.
cmds.BoolOption(progressOptionName, "p", "Stream progress data"),
cmds.BoolOption(wrapOptionName, "w", "Wrap files with a directory object"),
cmds.BoolOption(trickleOptionName, "t", "Use trickle-dag format for dag generation"),
cmds.BoolOption("only-hash", "n", "Only chunk and hash the specified content, don't write to disk"),
},
PreRun: func(req cmds.Request) error {
if quiet, _, _ := req.Option("quiet").Bool(); quiet {
......@@ -92,6 +93,16 @@ remains to be implemented.
progress, _, _ := req.Option(progressOptionName).Bool()
trickle, _, _ := req.Option(trickleOptionName).Bool()
wrap, _, _ := req.Option(wrapOptionName).Bool()
hash, _, _ := req.Option("only-hash").Bool()
if hash {
nilnode, err := core.NewNodeBuilder().NilRepo().Build(n.Context())
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
n = nilnode
}
outChan := make(chan interface{}, 8)
res.SetOutput((<-chan interface{})(outChan))
......
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