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

Commit ea24b50

Browse files
committed
Merge pull request #53 from xicombd/cli-config
Add config command to cli
2 parents de3aa7d + ebf25ef commit ea24b50

File tree

4 files changed

+164
-1
lines changed

4 files changed

+164
-1
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
"debug": "^2.2.0",
4747
"hapi": "^12.0.0",
4848
"ipfs-repo": "^0.2.2",
49+
"lodash.get": "^4.0.0",
50+
"lodash.set": "^4.0.0",
4951
"ronin": "^0.3.11"
5052
}
5153
}

src/cli/commands/config.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
'use strict'
2+
3+
const Command = require('ronin').Command
4+
const IPFS = require('../../ipfs-core')
5+
const debug = require('debug')
6+
const get = require('lodash.get')
7+
const set = require('lodash.set')
8+
const log = debug('cli:config')
9+
log.error = debug('cli:config:error')
10+
11+
module.exports = Command.extend({
12+
desc: 'Controls configuration variables.',
13+
14+
options: {
15+
bool: {
16+
type: 'boolean',
17+
default: false
18+
},
19+
json: {
20+
type: 'boolean',
21+
default: false
22+
}
23+
},
24+
25+
run: (bool, json, key, value) => {
26+
if (!key) {
27+
throw new Error('argument \'key\' is required')
28+
}
29+
30+
var node = new IPFS()
31+
32+
if (!value) {
33+
// Get the value of a given key
34+
35+
node.config.show((err, config) => {
36+
if (err) {
37+
log.error(err)
38+
throw new Error('failed to read the config')
39+
}
40+
41+
const value = get(config, key)
42+
console.log(value)
43+
})
44+
} else {
45+
// Set the new value of a given key
46+
47+
if (bool) {
48+
value = (value === 'true')
49+
} else if (json) {
50+
try {
51+
value = JSON.parse(value)
52+
} catch (err) {
53+
log.error(err)
54+
throw new Error('invalid JSON provided')
55+
}
56+
}
57+
58+
node.config.show((err, originalConfig) => {
59+
if (err) {
60+
log.error(err)
61+
throw new Error('failed to read the config')
62+
}
63+
64+
const updatedConfig = set(originalConfig, key, value)
65+
node.config.replace(updatedConfig, (err) => {
66+
if (err) {
67+
log.error(err)
68+
throw new Error('failed to save the config')
69+
}
70+
})
71+
})
72+
}
73+
}
74+
})

tests/test-cli/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const expect = require('chai').expect
99

1010
describe('cli', () => {
1111
const repoExample = process.cwd() + '/tests/repo-example'
12-
const repoTests = process.cwd() + '/tests/repo-tests' + Date.now()
12+
const repoTests = exports.repoTests = process.cwd() + '/tests/repo-tests' + Date.now()
1313

1414
before(done => {
1515
ncp(repoExample, repoTests, err => {

tests/test-cli/test-config.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/* globals describe, it */
2+
3+
'use strict'
4+
5+
const expect = require('chai').expect
6+
const nexpect = require('nexpect')
7+
const fs = require('fs')
8+
9+
describe('config', () => {
10+
const repoTests = require('./index').repoTests
11+
const configPath = repoTests + '/config'
12+
13+
const updatedConfig = () => JSON.parse(fs.readFileSync(configPath, 'utf8'))
14+
15+
it('get a config key value', done => {
16+
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'config', 'Identity.PeerID'])
17+
.run((err, stdout, exitcode) => {
18+
const expected = 'QmQ2zigjQikYnyYUSXZydNXrDRhBut2mubwJBaLXobMt3A'
19+
expect(stdout[0]).to.equal(expected)
20+
expect(err).to.not.exist
21+
expect(exitcode).to.equal(0)
22+
done()
23+
})
24+
})
25+
26+
it('set a config key with a string value', done => {
27+
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'config', 'foo', 'bar'])
28+
.run((err, stdout, exitcode) => {
29+
expect(err).to.not.exist
30+
expect(exitcode).to.equal(0)
31+
expect(updatedConfig().foo).to.equal('bar')
32+
done()
33+
})
34+
})
35+
36+
it('set a config key with true', done => {
37+
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'config', 'foo', true, '--bool'])
38+
.run((err, stdout, exitcode) => {
39+
expect(err).to.not.exist
40+
expect(exitcode).to.equal(0)
41+
expect(updatedConfig().foo).to.equal(true)
42+
done()
43+
})
44+
})
45+
46+
it('set a config key with false', done => {
47+
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'config', 'foo', false, '--bool'])
48+
.run((err, stdout, exitcode) => {
49+
expect(err).to.not.exist
50+
expect(exitcode).to.equal(0)
51+
expect(updatedConfig().foo).to.equal(false)
52+
done()
53+
})
54+
})
55+
56+
it('set a config key with json', done => {
57+
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'config', 'foo', '{"bar": 0}', '--json'])
58+
.run((err, stdout, exitcode) => {
59+
expect(err).to.not.exist
60+
expect(exitcode).to.equal(0)
61+
expect(updatedConfig().foo).to.deep.equal({ bar: 0 })
62+
done()
63+
})
64+
})
65+
66+
it('set a config key with invalid json', done => {
67+
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'config', 'foo', '{"bar: 0}', '--json'])
68+
.run((err, stdout, exitcode) => {
69+
const expected = 'error invalid JSON provided'
70+
expect(stdout[0]).to.equal(expected)
71+
expect(err).to.not.exist
72+
expect(exitcode).to.equal(1)
73+
done()
74+
})
75+
})
76+
77+
it('call config with no arguments', done => {
78+
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'config'])
79+
.run((err, stdout, exitcode) => {
80+
const expected = 'error argument \'key\' is required'
81+
expect(stdout[0]).to.equal(expected)
82+
expect(err).to.not.exist
83+
expect(exitcode).to.equal(1)
84+
done()
85+
})
86+
})
87+
})

0 commit comments

Comments
 (0)