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
Add object patch endpoints and cli commands #99
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is because that we are implementing the latest http-api ✔️