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

Commit

Permalink
feat(cli): migrate to awesome-dag-pb
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddias committed Nov 25, 2016
1 parent ca9935f commit 3bb3ba8
Show file tree
Hide file tree
Showing 11 changed files with 103 additions and 52 deletions.
27 changes: 22 additions & 5 deletions src/cli/commands/object/get.js
Expand Up @@ -16,15 +16,32 @@ module.exports = {
handler (argv) {
waterfall([
(cb) => utils.getIPFS(cb),
(ipfs, cb) => ipfs.object.get(argv.key, {enc: 'base58'}, cb),
(node, cb) => node.toJSON(cb)
], (err, nodeJson) => {
(ipfs, cb) => ipfs.object.get(
argv.key,
{ enc: 'base58' },
cb)
], (err, node) => {
if (err) {
throw err
}
const nodeJSON = node.toJSON()

nodeJson.Data = nodeJson.Data ? nodeJson.Data.toString() : ''
console.log(JSON.stringify(nodeJson))
nodeJSON.data = nodeJSON.data ? nodeJSON.data.toString() : ''

const answer = {
Data: nodeJSON.data,
Hash: nodeJSON.multihash,
Size: nodeJSON.size,
Links: nodeJSON.links.map((l) => {
return {
Name: l.name,
Size: l.size,
Hash: l.multihash
}
})
}

console.log(JSON.stringify(answer))
})
}
}
7 changes: 6 additions & 1 deletion src/cli/commands/object/links.js
Expand Up @@ -25,7 +25,12 @@ module.exports = {

links.forEach((link) => {
link = link.toJSON()
console.log(link.Hash, link.Size, link.Name)

console.log(
link.multihash,
link.size,
link.name
)
})
})
})
Expand Down
7 changes: 4 additions & 3 deletions src/cli/commands/object/new.js
Expand Up @@ -16,14 +16,15 @@ module.exports = {
handler (argv) {
waterfall([
(cb) => utils.getIPFS(cb),
(ipfs, cb) => ipfs.object.new(cb),
(node, cb) => node.toJSON(cb)
(ipfs, cb) => ipfs.object.new(cb)
], (err, node) => {
if (err) {
throw err
}

console.log(node.Hash)
const nodeJSON = node.toJSON()

console.log(nodeJSON.multihash)
})
}
}
72 changes: 51 additions & 21 deletions src/cli/commands/object/patch/add-link.js
Expand Up @@ -5,8 +5,7 @@ const debug = require('debug')
const log = debug('cli:object')
const dagPB = require('ipld-dag-pb')
const DAGLink = dagPB.DAGLink
const waterfall = require('async/waterfall')
const parallel = require('async/parallel')
const series = require('async/series')
log.error = debug('cli:object:error')

module.exports = {
Expand All @@ -17,29 +16,60 @@ module.exports = {
builder: {},

handler (argv) {
waterfall([
(cb) => utils.getIPFS(cb),
(ipfs, cb) => waterfall([
(cb) => ipfs.object.get(argv.ref, {enc: 'base58'}, cb),
(linkedObj, cb) => parallel([
(cb) => linkedObj.size(cb),
(cb) => linkedObj.multihash(cb)
], cb)
], (err, stats) => {
if (err) {
return cb(err)
}

const link = new DAGLink(argv.name, stats[0], stats[1])
ipfs.object.patch.addLink(argv.root, link, {enc: 'base58'}, cb)
}),
(node, cb) => node.toJSON(cb)
], (err, node) => {
let ipfs
let nodeA
let nodeB

series([
(cb) => {
utils.getIPFS((err, _ipfs) => {
if (err) {
return cb(err)
}
ipfs = _ipfs
cb()
})
},
(cb) => {
ipfs.object.get(
argv.ref,
{ enc: 'base58' },
(err, node) => {
console.log('Do I get my node')
if (err) {
return cb(err)
}
nodeA = node
cb()
})
},
(cb) => {
console.log('multihash is:', nodeA.multihash)
const link = new DAGLink(
argv.name,
nodeA.multihash,
nodeA.size
)

ipfs.object.patch.addLink(
argv.root,
link,
{ enc: 'base58' },
(err, node) => {
if (err) {
return cb(err)
}
nodeB = node
cb()
}
)
}
], (err) => {
if (err) {
throw err
}

console.log(node.Hash)
console.log(nodeB.toJSON().multihash)
})
}
}
6 changes: 3 additions & 3 deletions src/cli/commands/object/patch/append-data.js
Expand Up @@ -11,14 +11,14 @@ log.error = debug('cli:object:error')
function appendData (key, data) {
waterfall([
(cb) => utils.getIPFS(cb),
(ipfs, cb) => ipfs.object.patch.appendData(key, data, {enc: 'base58'}, cb),
(node, cb) => node.toJSON(cb)
(ipfs, cb) => ipfs.object.patch.appendData(key, data, {enc: 'base58'}, cb)
], (err, node) => {
if (err) {
throw err
}
const nodeJSON = node.toJSON()

console.log(node.Hash)
console.log(nodeJSON.multihash)
})
}

Expand Down
7 changes: 4 additions & 3 deletions src/cli/commands/object/patch/rm-link.js
Expand Up @@ -19,14 +19,15 @@ module.exports = {

waterfall([
(cb) => utils.getIPFS(cb),
(ipfs, cb) => ipfs.object.patch.rmLink(argv.root, dLink, {enc: 'base58'}, cb),
(node, cb) => node.toJSON(cb)
(ipfs, cb) => ipfs.object.patch.rmLink(argv.root, dLink, {enc: 'base58'}, cb)
], (err, node) => {
if (err) {
throw err
}

console.log(node.Hash)
const nodeJSON = node.toJSON()

console.log(nodeJSON.multihash)
})
}
}
6 changes: 3 additions & 3 deletions src/cli/commands/object/patch/set-data.js
Expand Up @@ -11,14 +11,14 @@ log.error = debug('cli:object:error')
function parseAndAddNode (key, data) {
waterfall([
(cb) => utils.getIPFS(cb),
(ipfs, cb) => ipfs.object.patch.setData(key, data, {enc: 'base58'}, cb),
(node, cb) => node.toJSON(cb)
(ipfs, cb) => ipfs.object.patch.setData(key, data, {enc: 'base58'}, cb)
], (err, node) => {
if (err) {
throw err
}
const nodeJSON = node.toJSON()

console.log(node.Hash)
console.log(nodeJSON.multihash)
})
}

Expand Down
11 changes: 7 additions & 4 deletions src/cli/commands/object/put.js
Expand Up @@ -11,14 +11,15 @@ log.error = debug('cli:object:error')
function putNode (buf, enc) {
waterfall([
(cb) => utils.getIPFS(cb),
(ipfs, cb) => ipfs.object.put(buf, {enc: enc}, cb),
(node, cb) => node.toJSON(cb)
(ipfs, cb) => ipfs.object.put(buf, {enc: enc}, cb)
], (err, node) => {
if (err) {
throw err
}

console.log('added', node.Hash)
const nodeJSON = node.toJSON()

console.log('added', nodeJSON.multihash)
})
}

Expand All @@ -36,7 +37,9 @@ module.exports = {

handler (argv) {
if (argv.data) {
return putNode(fs.readFileSync(argv.data), argv.inputenc)
const buf = fs.readFileSync(argv.data)
putNode(buf, argv.inputenc)
return
}

process.stdin.pipe(bl((err, input) => {
Expand Down
6 changes: 3 additions & 3 deletions src/cli/commands/swarm/peers.js
Expand Up @@ -22,13 +22,13 @@ module.exports = {
throw new Error('This command must be run in online mode. Try running \'ipfs daemon\' first.')
}

ipfs.swarm.peers((err, res) => {
ipfs.swarm.peers((err, result) => {
if (err) {
throw err
}

res.forEach((addr) => {
console.log(addr.toString())
result.forEach((item) => {
console.log(item.addr.toString())
})
})
})
Expand Down
2 changes: 0 additions & 2 deletions src/http-api/resources/object.js
Expand Up @@ -188,8 +188,6 @@ exports.put = {
const ipfs = request.server.app.ipfs
let node = request.pre.args.node

console.log('HANDLER')

series([
(cb) => {
DAGNode.create(new Buffer(node.Data), node.Links, (err, _node) => {
Expand Down
4 changes: 0 additions & 4 deletions test/core/both/test-bitswap.js
Expand Up @@ -103,7 +103,6 @@ describe('bitswap', () => {
const remoteNode = new API(apiUrl)

connectNodes(remoteNode, inProcNode, (err) => {
console.log('connected')
done(err, remoteNode)
})
}
Expand Down Expand Up @@ -207,22 +206,19 @@ describe('bitswap', () => {

it('2 peers', (done) => {
const file = new Buffer(`I love IPFS <3 ${Math.random()}`)
console.log('1')

waterfall([
// 0. Start node
(cb) => addNode(12, cb),
// 1. Add file to tmp instance
(remote, cb) => {
console.log('2')
remote.files.add([{
path: 'awesome.txt',
content: file
}], cb)
},
// 2. Request file from local instance
(val, cb) => {
console.log('3')
inProcNode.files.cat(val[0].hash, cb)
},
(res, cb) => res.pipe(bl(cb))
Expand Down

0 comments on commit 3bb3ba8

Please sign in to comment.