diff --git a/SPEC/CONFIG.md b/SPEC/CONFIG.md index 93411fdd0..a28a8e137 100644 --- a/SPEC/CONFIG.md +++ b/SPEC/CONFIG.md @@ -3,6 +3,8 @@ * [config.get](#configget) * [config.set](#configset) * [config.replace](#configreplace) +* [config.profiles.list](#configprofileslist) +* [config.profiles.apply](#configprofilesapply) #### `config.get` @@ -85,5 +87,62 @@ ipfs.config.replace(newConfig, (err) => { A great source of [examples][] can be found in the tests for this API. +#### `config.profiles.list` + +> List available config profiles + +##### `ipfs.config.profiles.list([options], [callback])` + +`options` is a object. +`callback` must follow `function (err, result) {}` signature, where `err` is an error if the operation was not successful. + +If no callback is passed, a [promise][] is returned + +**Example:** + +```JavaScript +ipfs.config.profiles.list(newConfig, (err, profiles) => { + if (err) { + throw err + } + + profiles.forEach(profile => { + console.info(profile.name, profile.description) + }) +}) +``` + +A great source of [examples][] can be found in the tests for this API. + +#### `config.profiles.apply` + +> Apply a config profile + +##### `ipfs.config.profiles.apply(name, [options], [callback])` + +`name` is a string. Call `config.profiles.list()` for a list of valid profile names. +`options` an object that might contain the following values: + - `dryRun` is a boolean which if true does not apply the profile +`callback` must follow the `function (err, result) {}` signature, where `err` is an error if the operation was not successful. + +If no callback is passed, a [promise][] is returned + +**Example:** + +```JavaScript +ipfs.config.profiles.apply('lowpower', (err, diff) => { + if (err) { + throw err + } + + console.info(diff.old) + console.info(diff.new) +}) +``` + +Note that you will need to restart your node for config changes to take effect. + +A great source of [examples][] can be found in the tests for this API. + [promise]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise [examples]: https://github.com/ipfs/interface-ipfs-core/blob/master/src/config diff --git a/src/config/index.js b/src/config/index.js index a9c31ec12..783dacdc1 100644 --- a/src/config/index.js +++ b/src/config/index.js @@ -5,7 +5,7 @@ const tests = { get: require('./get'), set: require('./set'), replace: require('./replace'), - profile: require('./profile') + profiles: require('./profiles') } module.exports = createSuite(tests) diff --git a/src/config/profile.js b/src/config/profile.js deleted file mode 100644 index 9722d06c8..000000000 --- a/src/config/profile.js +++ /dev/null @@ -1,69 +0,0 @@ -/* eslint-env mocha */ -'use strict' - -const { getDescribe, getIt, expect } = require('../utils/mocha') -const waterfall = require('async/waterfall') - -module.exports = (createCommon, options) => { - const describe = getDescribe(options) - const it = getIt(options) - const common = createCommon() - - describe('.config.profile', function () { - this.timeout(30 * 1000) - let ipfs - - before(function (done) { - // CI takes longer to instantiate the daemon, so we need to increase the - // timeout for the before step - this.timeout(60 * 1000) - - common.setup((err, factory) => { - expect(err).to.not.exist() - factory.spawnNode((err, node) => { - expect(err).to.not.exist() - ipfs = node - done() - }) - }) - }) - - after((done) => common.teardown(done)) - - it('should output changes but not save them for dry run', (done) => { - let config - waterfall([ - (cb) => ipfs.config.get(cb), - (_config, cb) => { - config = _config - ipfs.config.profile('lowpower', { dryRun: true }, cb) - }, - (diff, cb) => { - expect(diff.oldCfg.Swarm.ConnMgr.LowWater).to.not.equal(diff.newCfg.Swarm.ConnMgr.LowWater) - ipfs.config.get(cb) - }, - (newConfig, cb) => { - expect(newConfig.Swarm.ConnMgr.LowWater).to.equal(config.Swarm.ConnMgr.LowWater) - cb() - } - ], done) - }) - - it('should set a config profile', (done) => { - let diff - waterfall([ - (cb) => ipfs.config.get(cb), - (config, cb) => ipfs.config.profile('lowpower', cb), - (_diff, cb) => { - diff = _diff - expect(diff.oldCfg.Swarm.ConnMgr.LowWater).to.not.equal(diff.newCfg.Swarm.ConnMgr.LowWater) - ipfs.config.get(cb) - }, - (newConfig, cb) => { - expect(newConfig.Swarm.ConnMgr.LowWater).to.equal(diff.newCfg.Swarm.ConnMgr.LowWater) - cb() - } - ], done) - }) - }) -} diff --git a/src/config/profiles/apply.js b/src/config/profiles/apply.js new file mode 100644 index 000000000..87b55298a --- /dev/null +++ b/src/config/profiles/apply.js @@ -0,0 +1,49 @@ +/* eslint-env mocha */ +'use strict' + +const { getDescribe, getIt, expect } = require('../../utils/mocha') +const waterfall = require('async/waterfall') + +module.exports = (createCommon, options) => { + const describe = getDescribe(options) + const it = getIt(options) + const common = createCommon() + + describe('.config.profiles.apply', function () { + this.timeout(30 * 1000) + let ipfs + + before(function (done) { + // CI takes longer to instantiate the daemon, so we need to increase the + // timeout for the before step + this.timeout(60 * 1000) + + common.setup((err, factory) => { + expect(err).to.not.exist() + factory.spawnNode((err, node) => { + expect(err).to.not.exist() + ipfs = node + done() + }) + }) + }) + + after((done) => common.teardown(done)) + + it('should apply a config profile', (done) => { + let diff + waterfall([ + (cb) => ipfs.config.profiles.apply('lowpower', cb), + (_diff, cb) => { + diff = _diff + expect(diff.old.Swarm.ConnMgr.LowWater).to.not.equal(diff.new.Swarm.ConnMgr.LowWater) + ipfs.config.get(cb) + }, + (newConfig, cb) => { + expect(newConfig.Swarm.ConnMgr.LowWater).to.equal(diff.new.Swarm.ConnMgr.LowWater) + cb() + } + ], done) + }) + }) +} diff --git a/src/config/profiles/index.js b/src/config/profiles/index.js new file mode 100644 index 000000000..0a70eb4c1 --- /dev/null +++ b/src/config/profiles/index.js @@ -0,0 +1,9 @@ +'use strict' +const { createSuite } = require('../../utils/suite') + +const tests = { + list: require('./list'), + apply: require('./apply') +} + +module.exports = createSuite(tests) diff --git a/src/config/profiles/list.js b/src/config/profiles/list.js new file mode 100644 index 000000000..0b080714d --- /dev/null +++ b/src/config/profiles/list.js @@ -0,0 +1,50 @@ +/* eslint-env mocha */ +'use strict' + +const { getDescribe, getIt, expect } = require('../../utils/mocha') +const waterfall = require('async/waterfall') + +module.exports = (createCommon, options) => { + const describe = getDescribe(options) + const it = getIt(options) + const common = createCommon() + + describe('.config.profiles.list', function () { + this.timeout(30 * 1000) + let ipfs + + before(function (done) { + // CI takes longer to instantiate the daemon, so we need to increase the + // timeout for the before step + this.timeout(60 * 1000) + + common.setup((err, factory) => { + expect(err).to.not.exist() + factory.spawnNode((err, node) => { + expect(err).to.not.exist() + ipfs = node + done() + }) + }) + }) + + after((done) => common.teardown(done)) + + it('should list config profiles', (done) => { + waterfall([ + (cb) => ipfs.config.profiles.list(cb), + (profiles, cb) => { + expect(profiles).to.be.an('array') + expect(profiles).not.to.be.empty() + + profiles.forEach(profile => { + expect(profile.name).to.be.a('string') + expect(profile.description).to.be.a('string') + }) + + cb() + } + ], done) + }) + }) +}