From ebf25ef27b10593858f395bd6a17264558f4836f Mon Sep 17 00:00:00 2001 From: Francisco Baio Dias Date: Fri, 15 Jan 2016 21:38:04 +0000 Subject: [PATCH] Add config command to cli --- package.json | 2 + src/cli/commands/config.js | 74 +++++++++++++++++++++++++++++ tests/test-cli/index.js | 2 +- tests/test-cli/test-config.js | 87 +++++++++++++++++++++++++++++++++++ 4 files changed, 164 insertions(+), 1 deletion(-) create mode 100644 src/cli/commands/config.js create mode 100644 tests/test-cli/test-config.js diff --git a/package.json b/package.json index c8db791219..d0a103ff19 100644 --- a/package.json +++ b/package.json @@ -46,6 +46,8 @@ "debug": "^2.2.0", "hapi": "^12.0.0", "ipfs-repo": "^0.2.2", + "lodash.get": "^4.0.0", + "lodash.set": "^4.0.0", "ronin": "^0.3.11" } } diff --git a/src/cli/commands/config.js b/src/cli/commands/config.js new file mode 100644 index 0000000000..196eb0c388 --- /dev/null +++ b/src/cli/commands/config.js @@ -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') + } + }) + }) + } + } +}) diff --git a/tests/test-cli/index.js b/tests/test-cli/index.js index 2e8c768446..bf15631cac 100644 --- a/tests/test-cli/index.js +++ b/tests/test-cli/index.js @@ -9,7 +9,7 @@ const expect = require('chai').expect describe('cli', () => { const repoExample = process.cwd() + '/tests/repo-example' - const repoTests = process.cwd() + '/tests/repo-tests' + Date.now() + const repoTests = exports.repoTests = process.cwd() + '/tests/repo-tests' + Date.now() before(done => { ncp(repoExample, repoTests, err => { diff --git a/tests/test-cli/test-config.js b/tests/test-cli/test-config.js new file mode 100644 index 0000000000..9a3eeca710 --- /dev/null +++ b/tests/test-cli/test-config.js @@ -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() + }) + }) +})