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
Add config command to cli #53
Merged
+164
−1
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() | ||
}) | ||
}) | ||
}) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
presentThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, just added it here: fbaiodias@ebf25ef#diff-1a445abf8db16914970d37a823fd6e7eR77