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

Commit

Permalink
Add object patch endpoints and cli commands
Browse files Browse the repository at this point in the history
  • Loading branch information
fbaiodias committed Mar 27, 2016
1 parent da0bb36 commit 796bd83
Show file tree
Hide file tree
Showing 12 changed files with 1,055 additions and 26 deletions.
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",
"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)
}))
}
})
18 changes: 7 additions & 11 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,20 @@ function IPFS (repo) {
if (err) {
return callback(err)
}
callback(null, obj.multihash())
callback(null, obj)
})
})
},
rmLink: (multihash, multihashLink, callback) => {
rmLink: (multihash, linkName, 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
}
return true
})
obj.links = obj.links
.filter((link) => link.name !== linkName)
dagS.add(obj, (err) => {
if (err) {
return callback(err)
}
callback(null, obj.multihash())
callback(null, obj)
})
})
},
Expand All @@ -248,7 +244,7 @@ function IPFS (repo) {
if (err) {
return callback(err)
}
callback(null, obj.multihash())
callback(null, obj)
})
})
}
Expand Down
Loading

0 comments on commit 796bd83

Please sign in to comment.