This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
164 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
'use strict' | ||
|
||
const Command = require('ronin').Command | ||
const IPFS = require('../../ipfs-core') | ||
const debug = require('debug') | ||
const get = require('lodash.get') | ||
const set = require('lodash.set') | ||
const log = debug('cli:config') | ||
log.error = debug('cli:config:error') | ||
|
||
module.exports = Command.extend({ | ||
desc: 'Controls configuration variables.', | ||
|
||
options: { | ||
bool: { | ||
type: 'boolean', | ||
default: false | ||
}, | ||
json: { | ||
type: 'boolean', | ||
default: false | ||
} | ||
}, | ||
|
||
run: (bool, json, key, value) => { | ||
if (!key) { | ||
throw new Error('argument \'key\' is required') | ||
} | ||
|
||
var node = new IPFS() | ||
|
||
if (!value) { | ||
// Get the value of a given key | ||
|
||
node.config.show((err, config) => { | ||
if (err) { | ||
log.error(err) | ||
throw new Error('failed to read the config') | ||
} | ||
|
||
const value = get(config, key) | ||
console.log(value) | ||
}) | ||
} else { | ||
// Set the new value of a given key | ||
|
||
if (bool) { | ||
value = (value === 'true') | ||
} else if (json) { | ||
try { | ||
value = JSON.parse(value) | ||
} catch (err) { | ||
log.error(err) | ||
throw new Error('invalid JSON provided') | ||
} | ||
} | ||
|
||
node.config.show((err, originalConfig) => { | ||
if (err) { | ||
log.error(err) | ||
throw new Error('failed to read the config') | ||
} | ||
|
||
const updatedConfig = set(originalConfig, key, value) | ||
node.config.replace(updatedConfig, (err) => { | ||
if (err) { | ||
log.error(err) | ||
throw new Error('failed to save the config') | ||
} | ||
}) | ||
}) | ||
} | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* globals describe, it */ | ||
|
||
'use strict' | ||
|
||
const expect = require('chai').expect | ||
const nexpect = require('nexpect') | ||
const fs = require('fs') | ||
|
||
describe('config', () => { | ||
const repoTests = require('./index').repoTests | ||
const configPath = repoTests + '/config' | ||
|
||
const updatedConfig = () => JSON.parse(fs.readFileSync(configPath, 'utf8')) | ||
|
||
it('get a config key value', done => { | ||
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'config', 'Identity.PeerID']) | ||
.run((err, stdout, exitcode) => { | ||
const expected = 'QmQ2zigjQikYnyYUSXZydNXrDRhBut2mubwJBaLXobMt3A' | ||
expect(stdout[0]).to.equal(expected) | ||
expect(err).to.not.exist | ||
expect(exitcode).to.equal(0) | ||
done() | ||
}) | ||
}) | ||
|
||
it('set a config key with a string value', done => { | ||
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'config', 'foo', 'bar']) | ||
.run((err, stdout, exitcode) => { | ||
expect(err).to.not.exist | ||
expect(exitcode).to.equal(0) | ||
expect(updatedConfig().foo).to.equal('bar') | ||
done() | ||
}) | ||
}) | ||
|
||
it('set a config key with true', done => { | ||
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'config', 'foo', true, '--bool']) | ||
.run((err, stdout, exitcode) => { | ||
expect(err).to.not.exist | ||
expect(exitcode).to.equal(0) | ||
expect(updatedConfig().foo).to.equal(true) | ||
done() | ||
}) | ||
}) | ||
|
||
it('set a config key with false', done => { | ||
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'config', 'foo', false, '--bool']) | ||
.run((err, stdout, exitcode) => { | ||
expect(err).to.not.exist | ||
expect(exitcode).to.equal(0) | ||
expect(updatedConfig().foo).to.equal(false) | ||
done() | ||
}) | ||
}) | ||
|
||
it('set a config key with json', done => { | ||
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'config', 'foo', '{"bar": 0}', '--json']) | ||
.run((err, stdout, exitcode) => { | ||
expect(err).to.not.exist | ||
expect(exitcode).to.equal(0) | ||
expect(updatedConfig().foo).to.deep.equal({ bar: 0 }) | ||
done() | ||
}) | ||
}) | ||
|
||
it('set a config key with invalid json', done => { | ||
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'config', 'foo', '{"bar: 0}', '--json']) | ||
.run((err, stdout, exitcode) => { | ||
const expected = 'error invalid JSON provided' | ||
expect(stdout[0]).to.equal(expected) | ||
expect(err).to.not.exist | ||
expect(exitcode).to.equal(1) | ||
done() | ||
}) | ||
}) | ||
|
||
it('call config with no arguments', done => { | ||
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'config']) | ||
.run((err, stdout, exitcode) => { | ||
const expected = 'error argument \'key\' is required' | ||
expect(stdout[0]).to.equal(expected) | ||
expect(err).to.not.exist | ||
expect(exitcode).to.equal(1) | ||
done() | ||
}) | ||
}) | ||
}) |