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

Add config command to cli #53

Merged
merged 1 commit into from
Jan 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
})
})
})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing a test for failure, i.e. no key present

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.