-
Notifications
You must be signed in to change notification settings - Fork 1.2k
CLI - add block put
flags and block get
CID support
#652
Changes from all commits
2c3db47
55e006c
4b5f692
1e6fca8
ec82fe5
5319a41
5a00279
29bf12e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,93 @@ | ||
'use strict' | ||
|
||
const utils = require('../../utils') | ||
const mh = require('multihashes') | ||
const bl = require('bl') | ||
const fs = require('fs') | ||
const Block = require('ipfs-block') | ||
const CID = require('cids') | ||
const multihashing = require('multihashing-async') | ||
const waterfall = require('async/waterfall') | ||
const debug = require('debug') | ||
const log = debug('cli:block') | ||
log.error = debug('cli:block:error') | ||
|
||
function addBlock (buf) { | ||
function addBlock (buf, opts) { | ||
let block = new Block(buf) | ||
let cid | ||
|
||
utils.getIPFS((err, ipfs) => { | ||
if (err) { | ||
throw err | ||
} | ||
|
||
waterfall([ | ||
(cb) => ipfs.block.put(new Block(buf), cb), | ||
(block, cb) => block.key(cb) | ||
], (err, key) => { | ||
(cb) => generateHash(block, opts, cb), | ||
(mhash, cb) => generateCid(mhash, block, opts, cb), | ||
(cb) => ipfs.block.put(block, cid, cb) | ||
], (err) => { | ||
if (err) { | ||
throw err | ||
} | ||
|
||
console.log(mh.toB58String(key)) | ||
console.log(cid.toBaseEncodedString()) | ||
}) | ||
}) | ||
|
||
function generateHash (block, opts, cb) { | ||
if (opts.mhlen === undefined) { | ||
multihashing(buf, opts.mhtype, cb) | ||
} else { | ||
multihashing(buf, opts.mhtype, opts.mhlen, cb) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. since you are doing waterfall, you can pass the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. right - i previously needed the reference elsewhere, but that got factored out. will clean up. |
||
} | ||
|
||
function generateCid (mhash, block, opts, cb) { | ||
cid = new CID(opts.verison, opts.format, mhash) | ||
cb() | ||
} | ||
} | ||
|
||
module.exports = { | ||
command: 'put [data]', | ||
|
||
describe: 'Stores input as an IPFS block', | ||
|
||
builder: {}, | ||
builder: { | ||
format: { | ||
alias: 'f', | ||
describe: 'cid format for blocks to be created with.', | ||
default: 'v0' | ||
}, | ||
mhtype: { | ||
describe: 'multihash hash function', | ||
default: 'sha2-256' | ||
}, | ||
mhlen: { | ||
describe: 'multihash hash length', | ||
default: undefined | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @whyrusleeping in what case is mhlength used ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we resolved this in the chats. |
||
}, | ||
|
||
handler (argv) { | ||
// parse options | ||
if (argv.format === 'v0') { | ||
argv.verison = 0 | ||
argv.format = 'dag-pb' | ||
argv.mhtype = 'sha2-256' | ||
} else { | ||
argv.verison = 1 | ||
} | ||
|
||
if (argv.data) { | ||
return addBlock(fs.readFileSync(argv.data)) | ||
return addBlock(fs.readFileSync(argv.data), argv) | ||
} | ||
|
||
process.stdin.pipe(bl((err, input) => { | ||
if (err) { | ||
throw err | ||
} | ||
|
||
addBlock(input) | ||
addBlock(input, argv) | ||
})) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍