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

Commit 6179d4e

Browse files
committed
Add block commands to cli
1 parent c942c3f commit 6179d4e

File tree

7 files changed

+308
-2
lines changed

7 files changed

+308
-2
lines changed

src/cli/commands/block/get.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const Command = require('ronin').Command
2+
const utils = require('../../utils')
3+
const bs58 = require('bs58')
4+
const debug = require('debug')
5+
const log = debug('cli:block')
6+
log.error = debug('cli:block:error')
7+
8+
module.exports = Command.extend({
9+
desc: 'Get a raw IPFS block',
10+
11+
options: {},
12+
13+
run: (key) => {
14+
if (!key) {
15+
throw new Error("Argument 'key' is required")
16+
}
17+
18+
utils.getIPFS((err, ipfs) => {
19+
if (err) {
20+
throw err
21+
}
22+
23+
const mh = utils.isDaemonOn()
24+
? key
25+
: new Buffer(bs58.decode(key))
26+
27+
ipfs.block.get(mh, (err, block) => {
28+
if (err) {
29+
log.error(err)
30+
throw err
31+
}
32+
33+
if (block.data) {
34+
console.log(block.data.toString())
35+
return
36+
}
37+
38+
console.log(block.toString())
39+
})
40+
})
41+
}
42+
})

src/cli/commands/block/put.js

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
const Command = require('ronin').Command
2+
const utils = require('../../utils')
3+
const bs58 = require('bs58')
4+
const bl = require('bl')
5+
const fs = require('fs')
6+
const Block = require('ipfs-blocks').Block
7+
const debug = require('debug')
8+
const log = debug('cli:block')
9+
log.error = debug('cli:block:error')
10+
11+
function addBlock (buf) {
12+
utils.getIPFS((err, ipfs) => {
13+
if (err) {
14+
throw err
15+
}
16+
17+
if (utils.isDaemonOn()) {
18+
return ipfs.block.put(buf, (err, block) => {
19+
if (err) {
20+
log.error(err)
21+
throw err
22+
}
23+
24+
console.log(block.Key)
25+
})
26+
}
27+
28+
const block = new Block(buf)
29+
30+
ipfs.block.put(block, (err, obj) => {
31+
if (err) {
32+
log.error(err)
33+
throw err
34+
}
35+
36+
console.log(bs58.encode(block.key).toString())
37+
})
38+
})
39+
}
40+
41+
module.exports = Command.extend({
42+
desc: 'Stores input as an IPFS block',
43+
44+
options: {},
45+
46+
run: (filePath) => {
47+
if (filePath) {
48+
return addBlock(fs.readFileSync(filePath))
49+
}
50+
51+
process.stdin.pipe(bl((err, input) => {
52+
if (err) {
53+
log.error(err)
54+
throw err
55+
}
56+
57+
addBlock(input)
58+
}))
59+
}
60+
})

src/cli/commands/block/rm.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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:block')
8+
log.error = debug('cli:block:error')
9+
10+
module.exports = Command.extend({
11+
desc: 'Remove a raw IPFS block',
12+
13+
options: {},
14+
15+
run: (key) => {
16+
if (!key) {
17+
throw new Error("Argument 'key' is required")
18+
}
19+
20+
utils.getIPFS((err, ipfs) => {
21+
if (err) {
22+
throw err
23+
}
24+
25+
if (utils.isDaemonOn()) {
26+
// TODO implement this once `js-ipfs-api` supports it
27+
throw new Error('rm block with daemon running is not yet implemented')
28+
}
29+
30+
const mh = new Buffer(bs58.decode(key))
31+
32+
ipfs.block.del(mh, (err) => {
33+
if (err) {
34+
log.error(err)
35+
throw err
36+
}
37+
38+
console.log('removed', key)
39+
})
40+
})
41+
}
42+
})

src/cli/commands/block/stat.js

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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:block')
8+
log.error = debug('cli:block:error')
9+
10+
module.exports = Command.extend({
11+
desc: 'Print information of a raw IPFS block',
12+
13+
options: {},
14+
15+
run: (key) => {
16+
if (!key) {
17+
throw new Error("Argument 'key' is required")
18+
}
19+
20+
utils.getIPFS((err, ipfs) => {
21+
if (err) {
22+
throw err
23+
}
24+
25+
const mh = utils.isDaemonOn()
26+
? key
27+
: new Buffer(bs58.decode(key))
28+
29+
ipfs.block.stat(mh, (err, block) => {
30+
if (err) {
31+
log.error(err)
32+
throw err
33+
}
34+
35+
if (typeof block.Key !== 'string') {
36+
block.Key = bs58.encode(block.Key).toString()
37+
}
38+
39+
Object.keys(block).forEach((key) => {
40+
console.log(`${key}: ${block[key]}`)
41+
})
42+
})
43+
})
44+
}
45+
})

test/cli-tests/test-block.js

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/* eslint-env mocha */
2+
3+
const expect = require('chai').expect
4+
const nexpect = require('nexpect')
5+
const httpAPI = require('../../src/http-api')
6+
7+
describe('block', () => {
8+
describe('api offline', () => {
9+
it('put', (done) => {
10+
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'block', 'put', process.cwd() + '/test/test-data/hello'])
11+
.run((err, stdout, exitcode) => {
12+
expect(err).to.not.exist
13+
expect(exitcode).to.equal(0)
14+
expect(stdout[0])
15+
.to.equal('QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp')
16+
done()
17+
})
18+
})
19+
20+
it('get', (done) => {
21+
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'block', 'get', 'QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp'])
22+
.run((err, stdout, exitcode) => {
23+
expect(err).to.not.exist
24+
expect(exitcode).to.equal(0)
25+
expect(stdout[0])
26+
.to.equal('hello world')
27+
done()
28+
})
29+
})
30+
31+
it('stat', (done) => {
32+
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'block', 'stat', 'QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp'])
33+
.run((err, stdout, exitcode) => {
34+
expect(err).to.not.exist
35+
expect(exitcode).to.equal(0)
36+
expect(stdout[0])
37+
.to.equal('Key: QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp')
38+
expect(stdout[1])
39+
.to.equal('Size: 12')
40+
done()
41+
})
42+
})
43+
44+
it('rm', (done) => {
45+
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'block', 'rm', 'QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp'])
46+
.run((err, stdout, exitcode) => {
47+
expect(err).to.not.exist
48+
expect(exitcode).to.equal(0)
49+
expect(stdout[0])
50+
.to.equal('removed QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp')
51+
done()
52+
})
53+
})
54+
})
55+
56+
describe('api running', () => {
57+
before((done) => {
58+
httpAPI.start((err) => {
59+
expect(err).to.not.exist
60+
done()
61+
})
62+
})
63+
64+
after((done) => {
65+
httpAPI.stop((err) => {
66+
expect(err).to.not.exist
67+
done()
68+
})
69+
})
70+
71+
it('put', (done) => {
72+
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'block', 'put', process.cwd() + '/test/test-data/hello'])
73+
.run((err, stdout, exitcode) => {
74+
expect(err).to.not.exist
75+
expect(exitcode).to.equal(0)
76+
expect(stdout[0])
77+
.to.equal('QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp')
78+
done()
79+
})
80+
})
81+
82+
it('get', (done) => {
83+
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'block', 'get', 'QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp'])
84+
.run((err, stdout, exitcode) => {
85+
expect(err).to.not.exist
86+
expect(exitcode).to.equal(0)
87+
expect(stdout[0])
88+
.to.equal('hello world')
89+
done()
90+
})
91+
})
92+
93+
it('stat', (done) => {
94+
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'block', 'stat', 'QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp'])
95+
.run((err, stdout, exitcode) => {
96+
expect(err).to.not.exist
97+
expect(exitcode).to.equal(0)
98+
expect(stdout[0])
99+
.to.equal('Key: QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp')
100+
expect(stdout[1])
101+
.to.equal('Size: 12')
102+
done()
103+
})
104+
})
105+
106+
it.skip('rm', (done) => {
107+
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'block', 'rm', 'QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp'])
108+
.run((err, stdout, exitcode) => {
109+
expect(err).to.not.exist
110+
expect(exitcode).to.equal(0)
111+
expect(stdout[0])
112+
.to.equal('removed QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp')
113+
done()
114+
})
115+
})
116+
})
117+
})

test/cli-tests/test-commands.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ describe('commands', () => {
99
.run((err, stdout, exitcode) => {
1010
expect(err).to.not.exist
1111
expect(exitcode).to.equal(0)
12-
expect(stdout.length).to.equal(36)
12+
expect(stdout.length).to.equal(40)
1313
done()
1414
})
1515
})

test/http-api-tests/test-block.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ describe('block', () => {
177177
done()
178178
})
179179

180-
describe('ipfs.object.put', () => {
180+
describe('ipfs.block.put', () => {
181181
it('returns error for request without argument', (done) => {
182182
const filePath = null
183183

0 commit comments

Comments
 (0)