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

Commit ef2730f

Browse files
committed
cli - add 'dag get' command
1 parent 0247fa7 commit ef2730f

File tree

4 files changed

+80
-1
lines changed

4 files changed

+80
-1
lines changed

src/cli/commands/dag.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict'
2+
3+
module.exports = {
4+
command: 'dag',
5+
6+
description: 'Interact with ipld dag objects.',
7+
8+
builder (yargs) {
9+
return yargs
10+
.commandDir('dag')
11+
},
12+
13+
handler (argv) {
14+
}
15+
}

src/cli/commands/dag/get.js

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'use strict'
2+
3+
const utils = require('../../utils')
4+
const CID = require('cids')
5+
const debug = require('debug')
6+
const log = debug('cli:block')
7+
log.error = debug('cli:block:error')
8+
9+
module.exports = {
10+
command: 'get <ref>',
11+
12+
describe: 'Get a dag node from ipfs.',
13+
14+
builder: {},
15+
16+
handler (argv) {
17+
utils.getIPFS((err, ipfs) => {
18+
if (err) {
19+
throw err
20+
}
21+
22+
const refParts = argv.ref.split('/')
23+
const cidString = refParts[0]
24+
const pathString = refParts.slice(1).join('/')
25+
const cid = new CID(cidString)
26+
27+
let lookupFn
28+
if (pathString) {
29+
lookupFn = () => ipfs.dag.resolve(cid, pathString)
30+
} else {
31+
lookupFn = () => ipfs.dag.get(cid)
32+
}
33+
34+
lookupFn()
35+
.then((obj) => {
36+
console.log(obj)
37+
})
38+
.catch((err) => {
39+
console.error(err)
40+
})
41+
})
42+
}
43+
}

test/cli/commands.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
const expect = require('chai').expect
55
const runOnAndOff = require('../utils/on-and-off')
66

7-
const commandCount = 61
7+
const commandCount = 63
88

99
describe('commands', () => runOnAndOff((thing) => {
1010
let ipfs

test/cli/dag.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const expect = require('chai').expect
5+
const runOnAndOff = require('../utils/on-and-off')
6+
7+
describe('dag', () => runOnAndOff((thing) => {
8+
let ipfs
9+
10+
before(() => {
11+
ipfs = thing.ipfs
12+
})
13+
14+
it('get', () => {
15+
return ipfs('dag get z43AaGF23fmvRnDP56Ub9WcJCfzSfqtmzNCCvmz5eudT8dtdCDS/parentHash').then((out) => {
16+
let expectHash = new Buffer('c8c0a17305adea9bbb4b98a52d44f0c1478f5c48fc4b64739ee805242501b256', 'hex')
17+
expect(out).to.be.eql(expectHash)
18+
})
19+
})
20+
21+
}))

0 commit comments

Comments
 (0)