This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #99 from ipfs/feature/object-patch
Add object patch endpoints and cli commands
- Loading branch information
Showing
12 changed files
with
1,074 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
'use strict' | ||
|
||
const Command = require('ronin').Command | ||
const utils = require('../../../utils') | ||
const bs58 = require('bs58') | ||
const debug = require('debug') | ||
const log = debug('cli:object') | ||
const mDAG = require('ipfs-merkle-dag') | ||
const DAGLink = mDAG.DAGLink | ||
log.error = debug('cli:object:error') | ||
|
||
module.exports = Command.extend({ | ||
desc: 'Add a link to a given object', | ||
|
||
options: {}, | ||
|
||
run: (root, name, ref) => { | ||
if (!root) { | ||
throw new Error("Argument 'root' is required") | ||
} | ||
if (!name) { | ||
throw new Error("Argument 'name' is required") | ||
} | ||
if (!ref) { | ||
throw new Error("Argument 'ref' is required") | ||
} | ||
|
||
utils.getIPFS((err, ipfs) => { | ||
if (err) { | ||
throw err | ||
} | ||
|
||
if (utils.isDaemonOn()) { | ||
return ipfs.object.patch.addLink(root, name, ref, (err, obj) => { | ||
if (err) { | ||
log.error(err) | ||
throw err | ||
} | ||
|
||
console.log(obj.Hash) | ||
}) | ||
} | ||
|
||
// when running locally we first need to get the ref object, | ||
// so we can create the link with the correct size | ||
const refMh = new Buffer(bs58.decode(ref)) | ||
ipfs.object.get(refMh, (err, linkedObj) => { | ||
if (err) { | ||
log.error(err) | ||
throw err | ||
} | ||
|
||
const rootMh = new Buffer(bs58.decode(root)) | ||
const link = new DAGLink(name, linkedObj.size(), linkedObj.multihash()) | ||
ipfs.object.patch.addLink(rootMh, link, (err, obj) => { | ||
if (err) { | ||
log.error(err) | ||
throw err | ||
} | ||
|
||
console.log(bs58.encode(obj.multihash()).toString()) | ||
}) | ||
}) | ||
}) | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
'use strict' | ||
|
||
const Command = require('ronin').Command | ||
const utils = require('../../../utils') | ||
const bs58 = require('bs58') | ||
const bl = require('bl') | ||
const fs = require('fs') | ||
const debug = require('debug') | ||
const log = debug('cli:object') | ||
log.error = debug('cli:object:error') | ||
|
||
function appendData (keyStr, data) { | ||
utils.getIPFS((err, ipfs) => { | ||
if (err) { | ||
throw err | ||
} | ||
|
||
const key = utils.isDaemonOn() ? keyStr : new Buffer(bs58.decode(keyStr)) | ||
|
||
ipfs.object.patch.appendData(key, data, (err, obj) => { | ||
if (err) { | ||
log.error(err) | ||
throw err | ||
} | ||
|
||
if (typeof obj.multihash === 'function') { | ||
console.log(bs58.encode(obj.multihash()).toString()) | ||
return | ||
} | ||
|
||
console.log(obj.Hash) | ||
}) | ||
}) | ||
} | ||
|
||
module.exports = Command.extend({ | ||
desc: 'Append data to the data segment of a dag node', | ||
|
||
options: {}, | ||
|
||
run: (key, filePath) => { | ||
if (!key) { | ||
throw new Error("Argument 'root' is required") | ||
} | ||
|
||
if (filePath) { | ||
return appendData(key, fs.readFileSync(filePath)) | ||
} | ||
|
||
process.stdin.pipe(bl((err, input) => { | ||
if (err) { | ||
log.error(err) | ||
throw err | ||
} | ||
|
||
appendData(key, input) | ||
})) | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
'use strict' | ||
|
||
const Command = require('ronin').Command | ||
const utils = require('../../../utils') | ||
const bs58 = require('bs58') | ||
const debug = require('debug') | ||
const log = debug('cli:object') | ||
log.error = debug('cli:object:error') | ||
|
||
module.exports = Command.extend({ | ||
desc: 'Remove a link from an object', | ||
|
||
options: {}, | ||
|
||
run: (root, link) => { | ||
if (!root) { | ||
throw new Error("Argument 'root' is required") | ||
} | ||
if (!link) { | ||
throw new Error("Argument 'link' is required") | ||
} | ||
|
||
utils.getIPFS((err, ipfs) => { | ||
if (err) { | ||
throw err | ||
} | ||
|
||
if (utils.isDaemonOn()) { | ||
return ipfs.object.patch.rmLink(root, link, (err, obj) => { | ||
if (err) { | ||
log.error(err) | ||
throw err | ||
} | ||
|
||
console.log(obj.Hash) | ||
}) | ||
} | ||
|
||
const mh = new Buffer(bs58.decode(root)) | ||
ipfs.object.patch.rmLink(mh, link, (err, obj) => { | ||
if (err) { | ||
log.error(err) | ||
throw err | ||
} | ||
|
||
console.log(bs58.encode(obj.multihash()).toString()) | ||
}) | ||
}) | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
'use strict' | ||
|
||
const Command = require('ronin').Command | ||
const utils = require('../../../utils') | ||
const bs58 = require('bs58') | ||
const bl = require('bl') | ||
const fs = require('fs') | ||
const debug = require('debug') | ||
const log = debug('cli:object') | ||
log.error = debug('cli:object:error') | ||
|
||
function parseAndAddNode (keyStr, data) { | ||
utils.getIPFS((err, ipfs) => { | ||
if (err) { | ||
throw err | ||
} | ||
|
||
const key = utils.isDaemonOn() ? keyStr : new Buffer(bs58.decode(keyStr)) | ||
|
||
ipfs.object.patch.setData(key, data, (err, obj) => { | ||
if (err) { | ||
log.error(err) | ||
throw err | ||
} | ||
|
||
if (typeof obj.multihash === 'function') { | ||
console.log(bs58.encode(obj.multihash()).toString()) | ||
return | ||
} | ||
|
||
console.log(obj.Hash) | ||
}) | ||
}) | ||
} | ||
|
||
module.exports = Command.extend({ | ||
desc: 'Set data field of an ipfs object', | ||
|
||
options: {}, | ||
|
||
run: (key, filePath) => { | ||
if (!key) { | ||
throw new Error("Argument 'root' is required") | ||
} | ||
|
||
if (filePath) { | ||
return parseAndAddNode(key, fs.readFileSync(filePath)) | ||
} | ||
|
||
process.stdin.pipe(bl((err, input) => { | ||
if (err) { | ||
log.error(err) | ||
throw err | ||
} | ||
|
||
parseAndAddNode(key, input) | ||
})) | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.