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

cli - add 'dag get' command #753

Merged
merged 2 commits into from
Feb 3, 2017
Merged
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
15 changes: 15 additions & 0 deletions src/cli/commands/dag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict'

module.exports = {
command: 'dag',

description: 'Interact with ipld dag objects.',

builder (yargs) {
return yargs
.commandDir('dag')
},

handler (argv) {
}
}
47 changes: 47 additions & 0 deletions src/cli/commands/dag/get.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'use strict'

const utils = require('../../utils')
const CID = require('cids')
const debug = require('debug')
const log = debug('cli:block')
log.error = debug('cli:block:error')

module.exports = {
command: 'get <ref>',

describe: 'Get a dag node from ipfs.',

builder: {},

handler (argv) {
utils.getIPFS((err, ipfs) => {
if (err) {
throw err
}

const refParts = argv.ref.split('/')
const cidString = refParts[0]
const pathString = refParts.slice(1).join('/')
const cid = new CID(cidString)

let lookupFn
if (pathString) {
lookupFn = () => ipfs.dag.resolve(cid, pathString)
} else {
lookupFn = () => ipfs.dag.get(cid)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@whyrusleeping convinced me that we should merge this two funcs yesterday, will be writing something explaining what will happen in the next hour, but yeah, for now 👌🏽


lookupFn()
.then((obj) => {
if (Buffer.isBuffer(obj)) {
console.log('0x' + obj.toString('hex'))
} else {
console.log(obj)
}
})
.catch((err) => {
console.error(err)
})
})
}
}
2 changes: 1 addition & 1 deletion test/cli/commands.js
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@
const expect = require('chai').expect
const runOnAndOff = require('../utils/on-and-off')

const commandCount = 61
const commandCount = 63

describe('commands', () => runOnAndOff((thing) => {
let ipfs
25 changes: 25 additions & 0 deletions test/cli/dag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* eslint-env mocha */
'use strict'

const expect = require('chai').expect
const runOnAndOff = require('../utils/on-and-off')

describe('dag', () => runOnAndOff.off((thing) => {
let ipfs

before(() => {
ipfs = thing.ipfs
})

it('get', () => {
// put test eth-block
return ipfs('block put --format eth-block --mhtype keccak-256 test/test-data/eth-block').then((out) => {
expect(out).to.eql('z43AaGF23fmvRnDP56Ub9WcJCfzSfqtmzNCCvmz5eudT8dtdCDS')
// lookup path on eth-block
return ipfs('dag get z43AaGF23fmvRnDP56Ub9WcJCfzSfqtmzNCCvmz5eudT8dtdCDS/parentHash')
}).then((out) => {
let expectHash = new Buffer('c8c0a17305adea9bbb4b98a52d44f0c1478f5c48fc4b64739ee805242501b256', 'hex')
expect(out).to.be.eql('0x' + expectHash.toString('hex'))
})
})
}))