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

Add object patch endpoints and cli commands #99

Merged
merged 1 commit into from
Mar 27, 2016
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
"bs58": "^3.0.0",
"debug": "^2.2.0",
"hapi": "^12.0.0",
"ipfs-api": "^2.13.1",
"ipfs-api": "github:ipfs/js-ipfs-api#1fd9749",
Copy link
Member

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 ✔️

"ipfs-blocks": "^0.1.0",
"ipfs-data-importing": "^0.3.3",
"ipfs-merkle-dag": "^0.2.1",
Expand Down
66 changes: 66 additions & 0 deletions src/cli/commands/object/patch/add-link.js
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())
})
})
})
}
})
59 changes: 59 additions & 0 deletions src/cli/commands/object/patch/append-data.js
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)
}))
}
})
50 changes: 50 additions & 0 deletions src/cli/commands/object/patch/rm-link.js
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())
})
})
}
})
59 changes: 59 additions & 0 deletions src/cli/commands/object/patch/set-data.js
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)
}))
}
})
17 changes: 9 additions & 8 deletions src/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ function IPFS (repo) {
if (err) {
return callback(err)
}
callback(null, obj.multihash())
callback(null, obj)
})
})
},
Expand All @@ -219,24 +219,25 @@ function IPFS (repo) {
if (err) {
return callback(err)
}
callback(null, obj.multihash())
callback(null, obj)
})
})
},
rmLink: (multihash, multihashLink, callback) => {
rmLink: (multihash, linkRef, callback) => {
this.object.get(multihash, (err, obj) => {
if (err) { return callback(err) }
obj.links = obj.links.filter((link) => {
if (link.hash.equals(multihashLink)) {
return false
// filter by name when linkRef is a string, or by hash otherwise
if (typeof linkRef === 'string') {
return link.name !== linkRef
}
return true
return !link.hash.equals(linkRef)
})
dagS.add(obj, (err) => {
if (err) {
return callback(err)
}
callback(null, obj.multihash())
callback(null, obj)
})
})
},
Expand All @@ -248,7 +249,7 @@ function IPFS (repo) {
if (err) {
return callback(err)
}
callback(null, obj.multihash())
callback(null, obj)
})
})
}
Expand Down
Loading