Skip to content

Commit

Permalink
Add config command to cli
Browse files Browse the repository at this point in the history
  • Loading branch information
fbaiodias committed Jan 15, 2016
1 parent de3aa7d commit ebf25ef
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 1 deletion.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
74 changes: 74 additions & 0 deletions src/cli/commands/config.js
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')
}
})
})
}
}
})
2 changes: 1 addition & 1 deletion tests/test-cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
87 changes: 87 additions & 0 deletions tests/test-cli/test-config.js
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()
})
})
})

0 comments on commit ebf25ef

Please sign in to comment.