|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const Command = require('ronin').Command |
| 4 | +const utils = require('../../../utils') |
| 5 | +const bs58 = require('bs58') |
| 6 | +const debug = require('debug') |
| 7 | +const log = debug('cli:object') |
| 8 | +const mDAG = require('ipfs-merkle-dag') |
| 9 | +const DAGLink = mDAG.DAGLink |
| 10 | +log.error = debug('cli:object:error') |
| 11 | + |
| 12 | +module.exports = Command.extend({ |
| 13 | + desc: 'Add a link to a given object', |
| 14 | + |
| 15 | + options: {}, |
| 16 | + |
| 17 | + run: (root, name, ref) => { |
| 18 | + if (!root) { |
| 19 | + throw new Error("Argument 'root' is required") |
| 20 | + } |
| 21 | + if (!name) { |
| 22 | + throw new Error("Argument 'name' is required") |
| 23 | + } |
| 24 | + if (!ref) { |
| 25 | + throw new Error("Argument 'ref' is required") |
| 26 | + } |
| 27 | + |
| 28 | + utils.getIPFS((err, ipfs) => { |
| 29 | + if (err) { |
| 30 | + throw err |
| 31 | + } |
| 32 | + |
| 33 | + if (utils.isDaemonOn()) { |
| 34 | + return ipfs.object.patch.addLink(root, name, ref, (err, obj) => { |
| 35 | + if (err) { |
| 36 | + log.error(err) |
| 37 | + throw err |
| 38 | + } |
| 39 | + |
| 40 | + console.log(obj.Hash) |
| 41 | + }) |
| 42 | + } |
| 43 | + |
| 44 | + // when running locally we first need to get the ref object, |
| 45 | + // so we can create the link with the correct size |
| 46 | + const refMh = new Buffer(bs58.decode(ref)) |
| 47 | + ipfs.object.get(refMh, (err, linkedObj) => { |
| 48 | + if (err) { |
| 49 | + log.error(err) |
| 50 | + throw err |
| 51 | + } |
| 52 | + |
| 53 | + const rootMh = new Buffer(bs58.decode(root)) |
| 54 | + const link = new DAGLink(name, linkedObj.size(), linkedObj.multihash()) |
| 55 | + ipfs.object.patch.addLink(rootMh, link, (err, obj) => { |
| 56 | + if (err) { |
| 57 | + log.error(err) |
| 58 | + throw err |
| 59 | + } |
| 60 | + |
| 61 | + console.log(bs58.encode(obj.multihash()).toString()) |
| 62 | + }) |
| 63 | + }) |
| 64 | + }) |
| 65 | + } |
| 66 | +}) |
0 commit comments