diff --git a/package.json b/package.json index 49f4665ce0..4e5ed05c74 100644 --- a/package.json +++ b/package.json @@ -80,7 +80,7 @@ "debug": "^2.2.0", "fs-blob-store": "^5.2.1", "hapi": "^13.3.0", - "ipfs-api": "github:ipfs/js-ipfs-api#1fd9749", + "ipfs-api": "github:ipfs/js-ipfs-api#f3e2d42", "ipfs-blocks": "^0.1.0", "ipfs-data-importing": "^0.3.3", "ipfs-merkle-dag": "^0.3.0", diff --git a/src/cli/commands/config.js b/src/cli/commands/config.js index 86c4527aea..e328fcf14f 100644 --- a/src/cli/commands/config.js +++ b/src/cli/commands/config.js @@ -33,6 +33,17 @@ module.exports = Command.extend({ if (!value) { // Get the value of a given key + if (utils.isDaemonOn()) { + return ipfs.config.get(key, (err, config) => { + if (err) { + log.error(err) + throw new Error('failed to read the config') + } + + console.log(config.Value) + }) + } + ipfs.config.show((err, config) => { if (err) { log.error(err) @@ -56,6 +67,15 @@ module.exports = Command.extend({ } } + if (utils.isDaemonOn()) { + return ipfs.config.set(key, value, (err) => { + if (err) { + log.error(err) + throw new Error('failed to save the config') + } + }) + } + ipfs.config.show((err, originalConfig) => { if (err) { log.error(err) diff --git a/src/cli/commands/config/edit.js b/src/cli/commands/config/edit.js index 2770cecdc5..5bcfde40cc 100644 --- a/src/cli/commands/config/edit.js +++ b/src/cli/commands/config/edit.js @@ -86,6 +86,9 @@ module.exports = Command.extend({ } function saveConfig (config, next) { + config = utils.isDaemonOn() + ? new Buffer(JSON.stringify(config)) : config + ipfs.config.replace(config, (err) => { if (err) { log.error(err) diff --git a/src/cli/commands/config/replace.js b/src/cli/commands/config/replace.js index 258000d6a1..c9d5672d32 100644 --- a/src/cli/commands/config/replace.js +++ b/src/cli/commands/config/replace.js @@ -4,6 +4,7 @@ const path = require('path') const log = debug('cli:config') log.error = debug('cli:config:error') const utils = require('../../utils') +const fs = require('fs') module.exports = Command.extend({ desc: 'Replaces the config with ', @@ -15,14 +16,16 @@ module.exports = Command.extend({ if (err) { throw err } - const config = require(path.resolve(process.cwd(), configPath)) - ipfs.config.replace(config, (err, version) => { + const filePath = path.resolve(process.cwd(), configPath) + + const config = utils.isDaemonOn() + ? filePath : JSON.parse(fs.readFileSync(filePath, 'utf8')) + + ipfs.config.replace(config, (err) => { if (err) { throw err } - - console.log(version) }) }) } diff --git a/test/cli-tests/test-config.js b/test/cli-tests/test-config.js index f7a8badd42..41fe69300d 100644 --- a/test/cli-tests/test-config.js +++ b/test/cli-tests/test-config.js @@ -3,87 +3,220 @@ const expect = require('chai').expect const nexpect = require('nexpect') const fs = require('fs') +const httpAPI = require('../../src/http-api') describe('config', () => { + const repoTests = require('./index').repoTests + const configPath = repoTests + '/config' + const originalConfigPath = process.cwd() + '/test/go-ipfs-repo/config' + const updatedConfig = () => JSON.parse(fs.readFileSync(configPath, 'utf8')) + const restoreConfig = () => fs.writeFileSync(configPath, fs.readFileSync(originalConfigPath, 'utf8'), 'utf8') + describe('api offline', () => { - 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() - }) - }) + describe('get/set', () => { + 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 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 invalid json', (done) => { + nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'config', 'foo', '{"bar: 0}', '--json']) + .run((err, stdout, exitcode) => { + const expected = 'error\tinvalid 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\targument 'key' is required" + expect(stdout[0]).to.equal(expected) + expect(err).to.not.exist + expect(exitcode).to.equal(1) + 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() - }) + describe('replace', () => { + it('replace config with file', (done) => { + const filePath = 'test/test-data/otherconfig' + const expectedConfig = JSON.parse(fs.readFileSync(filePath, 'utf8')) + + nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'config', 'replace', filePath]) + .run((err, stdout, exitcode) => { + expect(err).to.not.exist + expect(exitcode).to.equal(0) + + expect(updatedConfig()).to.deep.equal(expectedConfig) + done() + }) + }) + + after(() => { + restoreConfig() + }) }) + }) - 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() - }) + describe('api running', () => { + before((done) => { + httpAPI.start((err) => { + expect(err).to.not.exist + 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() - }) + after((done) => { + httpAPI.stop((err) => { + expect(err).to.not.exist + 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\tinvalid JSON provided' - expect(stdout[0]).to.equal(expected) - expect(err).to.not.exist - expect(exitcode).to.equal(1) - done() - }) + describe('get/set', () => { + 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\tinvalid 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\targument 'key' is required" + 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\targument 'key' is required" - expect(stdout[0]).to.equal(expected) - expect(err).to.not.exist - expect(exitcode).to.equal(1) - done() - }) + describe('replace', () => { + it('replace config with file', (done) => { + const filePath = 'test/test-data/otherconfig' + const expectedConfig = JSON.parse(fs.readFileSync(filePath, 'utf8')) + + nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'config', 'replace', filePath]) + .run((err, stdout, exitcode) => { + expect(err).to.not.exist + expect(exitcode).to.equal(0) + + expect(updatedConfig()).to.deep.equal(expectedConfig) + done() + }) + }) + + after(() => { + restoreConfig() + }) }) }) - - describe('api running', () => {}) })