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

feat: add CLI support for different hash func and type #748

Merged
merged 1 commit into from
Jan 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
52 changes: 38 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,66 @@ 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 || 'sha2-256', cb),
(multihash, cb) => {
if (!opts.version || opts.version !== 0) {
cid = new CID(1, opts.format || 'dag-pb', multihash)
} else {
cid = new CID(0, 'dag-pb', 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)
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: 'dag-pb'
},
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
38 changes: 21 additions & 17 deletions test/cli/test-block.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,37 @@ describe('block', () => {
describeOnlineAndOffline(repoPath, () => {
it('put', () => {
return ipfs('block put test/test-data/hello').then((out) => {
expect(out).to.be.eql(
'QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp'
)
expect(out).to.eql('zdj7Wgpi9yzsvjJerghrdhPFpe1p1jZFyB5GKLyXEzFQyaxVk')
})
})

it('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')
})
return ipfs('block get QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp')
.then((out) => expect(out).to.eql('hello world\n'))
})

it('stat', () => {
return ipfs('block stat QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp').then((out) => {
expect(out).to.be.eql([
'Key: QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp',
'Size: 12'
].join('\n'))
})
return ipfs('block stat QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp')
.then((out) => {
expect(out).to.eql([
'Key: QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp',
'Size: 12'
].join('\n'))
})
})

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