Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
feat: add CLI support for different hash func and type
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddias committed Jan 31, 2017
1 parent d3dc3d3 commit 2074e9b
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 36 deletions.
16 changes: 5 additions & 11 deletions src/cli/commands/block/get.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const utils = require('../../utils')
const mh = require('multihashes')
const CID = require('cids')
const debug = require('debug')
const log = debug('cli:block')
log.error = debug('cli:block:error')
Expand All @@ -19,21 +19,15 @@ module.exports = {
throw err
}

const hash = utils.isDaemonOn()
? argv.key
: mh.fromB58String(argv.key)
const cid = new CID(argv.key)

ipfs.block.get(hash, (err, block) => {
ipfs.block.get(cid, (err, block) => {
if (err) {
throw err
}

if (block.data) {
console.log(block.data.toString())
return
}

console.log(block.toString())
process.stdout.write(block.data)
process.stdout.write('\n')
})
})
}
Expand Down
51 changes: 37 additions & 14 deletions src/cli/commands/block/put.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict'

const utils = require('../../utils')
const mh = require('multihashes')
const CID = require('cids')
const multihashing = require('multihashing-async')
const bl = require('bl')
const fs = require('fs')
const Block = require('ipfs-block')
Expand All @@ -10,43 +11,65 @@ const debug = require('debug')
const log = debug('cli:block')
log.error = debug('cli:block:error')

function addBlock (buf) {
function addBlock (data, opts) {
utils.getIPFS((err, ipfs) => {
if (err) {
throw err
}

let cid

waterfall([
(cb) => ipfs.block.put(new Block(buf), cb),
(block, cb) => block.key(cb)
], (err, key) => {
if (err) {
throw err
}
(cb) => multihashing(data, opts.mhtype, cb),
(multihash, cb) => {
if (!opts.version || opts.version !== 0) {
cid = new CID(1, opts.format, multihash)
} else {
cid = new CID(multihash)
}
cb(null, cid)
},
(cid, cb) => ipfs.block.put(new Block(data), cid, cb)
], (err) => {
if (err) { throw err }

console.log(mh.toB58String(key))
console.log(cid.toBaseEncodedString())
})
})
}

module.exports = {
command: 'put [data]',
command: 'put [block]',

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
}
},

handler (argv) {
if (argv.data) {
return addBlock(fs.readFileSync(argv.data))
if (argv.block) {
return addBlock(fs.readFileSync(argv.block), argv)
}

process.stdin.pipe(bl((err, input) => {
if (err) {
throw err
}

addBlock(input)
addBlock(input, argv)
}))
}
}
12 changes: 5 additions & 7 deletions src/cli/commands/block/stat.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const utils = require('../../utils')
const debug = require('debug')
const CID = require('cids')
const log = debug('cli:block')
log.error = debug('cli:block:error')

Expand All @@ -14,14 +15,11 @@ module.exports = {

handler (argv) {
utils.getIPFS((err, ipfs) => {
if (err) {
throw err
}
if (err) { throw err }
const cid = new CID(argv.key)

ipfs.block.stat(argv.key, (err, stats) => {
if (err) {
throw err
}
ipfs.block.stat(cid, (err, stats) => {
if (err) { throw err }

console.log('Key:', stats.key)
console.log('Size:', stats.size)
Expand Down
14 changes: 10 additions & 4 deletions test/cli/test-block.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,27 @@ describe('block', () => {
describeOnlineAndOffline(repoPath, () => {
it('put', () => {
return ipfs('block put test/test-data/hello').then((out) => {
expect(out).to.be.eql(
expect(out).to.eql(
'QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp'
)
})
})

it.only('put with flags, format and mhtype', () => {
return ipfs('block put --format eth-block --mhtype keccak-256 test/test-data/eth-block').then((out) => {
expect(out).to.eql('z43AaGF23fmvRnDP56Ub9WcJCfzSfqtmzNCCvmz5eudT8dtdCDS')
})
})

it('get', () => {
return ipfs('block get QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp').then((out) => {
expect(out).to.be.eql('hello world\n')
expect(out).to.eql('hello world\n')
})
})

it('stat', () => {
return ipfs('block stat QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp').then((out) => {
expect(out).to.be.eql([
expect(out).to.eql([
'Key: QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp',
'Size: 12'
].join('\n'))
Expand All @@ -33,7 +39,7 @@ describe('block', () => {

it.skip('rm', () => {
return ipfs('block rm QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp').then((out) => {
expect(out).to.be.eql(
expect(out).to.eql(
'removed QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp'
)
})
Expand Down
Binary file added test/test-data/eth-block
Binary file not shown.

0 comments on commit 2074e9b

Please sign in to comment.