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

Commit

Permalink
feat: migrate cli to use new async DAGNode interface
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddias committed Oct 29, 2016
1 parent 01e56ec commit 1b0b22d
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 23 deletions.
10 changes: 7 additions & 3 deletions src/cli/commands/object/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ module.exports = {
throw err
}

const res = node.toJSON()
res.Data = res.Data ? res.Data.toString() : ''
console.log(JSON.stringify(res))
node.toJSON((err, nodeJSON) => {
if (err) {
throw err
}
nodeJSON.Data = nodeJSON.Data ? nodeJSON.Data.toString() : ''
console.log(JSON.stringify(nodeJSON))
})
})
})
}
Expand Down
7 changes: 6 additions & 1 deletion src/cli/commands/object/new.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ module.exports = {
throw err
}

console.log(node.toJSON().Hash)
node.toJSON((err, nodeJSON) => {
if (err) {
throw err
}
console.log(nodeJSON.Hash)
})
})
})
}
Expand Down
42 changes: 30 additions & 12 deletions src/cli/commands/object/patch/add-link.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
const utils = require('../../../utils')
const debug = require('debug')
const log = debug('cli:object')
const mDAG = require('ipfs-merkle-dag')
const DAGLink = mDAG.DAGLink
const dagPB = require('ipld-dag-pb')
const DAGLink = dagPB.DAGLink
log.error = debug('cli:object:error')

module.exports = {
Expand All @@ -15,23 +15,41 @@ module.exports = {
builder: {},

handler (argv) {
utils.getIPFS((err, ipfs) => {
utils.getIPFS(gotIPFS)

function gotIPFS (err, ipfs) {
if (err) {
throw err
}

ipfs.object.get(argv.ref, {enc: 'base58'}).then((linkedObj) => {
const link = new DAGLink(
argv.name,
linkedObj.size(),
linkedObj.multihash()
)
return ipfs.object.patch.addLink(argv.root, link, {enc: 'base58'})
}).then((node) => {
console.log(node.toJSON().Hash)
linkedObj.size((err, size) => {
if (err) {
throw err
}
linkedObj.multihash((err, multihash) => {
if (err) {
throw err
}

const link = new DAGLink(argv.name, size, multihash)

ipfs.object.patch.addLink(argv.root, link, {enc: 'base58'})
.then((node) => {
node.toJSON(gotJSON)

function gotJSON (err, nodeJSON) {
if (err) {
throw err
}
console.log(nodeJSON.Hash)
}
})
})
})
}).catch((err) => {
throw err
})
})
}
}
}
8 changes: 7 additions & 1 deletion src/cli/commands/object/patch/append-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ function appendData (key, data) {
throw err
}

console.log(node.toJSON().Hash)
node.toJSON((err, nodeJSON) => {
if (err) {
throw err
}

console.log(nodeJSON.Hash)
})
})
})
}
Expand Down
9 changes: 7 additions & 2 deletions src/cli/commands/object/patch/rm-link.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const DAGLink = require('ipfs-merkle-dag').DAGLink
const DAGLink = require('ipld-dag-pb').DAGLink
const utils = require('../../../utils')
const debug = require('debug')
const log = debug('cli:object')
Expand All @@ -26,7 +26,12 @@ module.exports = {
throw err
}

console.log(node.toJSON().Hash)
node.toJSON((err, nodeJSON) => {
if (err) {
throw err
}
console.log(nodeJSON.Hash)
})
})
})
}
Expand Down
8 changes: 7 additions & 1 deletion src/cli/commands/object/patch/set-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ function parseAndAddNode (key, data) {
throw err
}

console.log(node.toJSON().Hash)
node.toJSON((err, nodeJSON) => {
if (err) {
throw err
}

console.log(nodeJSON.Hash)
})
})
})
}
Expand Down
8 changes: 7 additions & 1 deletion src/cli/commands/object/put.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ function putNode (buf, enc) {
throw err
}

console.log('added', node.toJSON().Hash)
node.toJSON((err, nodeJSON) => {
if (err) {
throw err
}

console.log('added', nodeJSON.Hash)
})
})
})
}
Expand Down
6 changes: 4 additions & 2 deletions src/http-api/resources/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -443,15 +443,17 @@ exports.patchAddLink = {
}).code(500)
}

node.toJSON((err, nodeJSON) => {
node.toJSON(gotJSON)

function gotJSON (err, nodeJSON) {
if (err) {
return reply({
Message: 'Failed to get object: ' + err,
Code: 0
}).code(500)
}
return reply(nodeJSON)
})
}
})
})
})
Expand Down
1 change: 1 addition & 0 deletions test/http-api/ipfs-api/test-object.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-env mocha */
/* eslint max-nested-callbacks: ["error", 8] */
'use strict'

const expect = require('chai').expect
Expand Down

0 comments on commit 1b0b22d

Please sign in to comment.