From 2beac9cd49275e58b9e122d624aa65780341546e Mon Sep 17 00:00:00 2001 From: Friedel Ziegelmayer Date: Thu, 15 Sep 2016 14:51:40 +0200 Subject: [PATCH] fix(config): better http-api and interface-ipfs-core compliant --- src/cli/commands/config.js | 42 +++--------------- src/http-api/resources/config.js | 64 ++++++++++++++++++--------- src/http-api/routes/config.js | 2 +- test/cli/test-config.js | 4 +- test/http-api/ipfs-api/test-config.js | 4 +- 5 files changed, 54 insertions(+), 62 deletions(-) diff --git a/src/cli/commands/config.js b/src/cli/commands/config.js index a15674fde3..4f9b0b993f 100644 --- a/src/cli/commands/config.js +++ b/src/cli/commands/config.js @@ -1,8 +1,6 @@ 'use strict' 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') const utils = require('../utils') @@ -43,26 +41,17 @@ module.exports = { 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.get((err, config) => { + ipfs.config.get(key, (err, value) => { if (err) { log.error(err) throw new Error('failed to read the config') } - const value = get(config, key) - console.log(value) + if (typeof value === 'object') { + console.log(JSON.stringify(value, null, 2)) + } else { + console.log(value) + } }) } else { // Set the new value of a given key @@ -78,28 +67,11 @@ module.exports = { } } - 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.get((err, originalConfig) => { + ipfs.config.set(key, value, (err) => { if (err) { log.error(err) throw new Error('failed to read the config') } - - const updatedConfig = set(originalConfig, key, value) - ipfs.config.replace(updatedConfig, (err) => { - if (err) { - log.error(err) - throw new Error('failed to save the config') - } - }) }) } }) diff --git a/src/http-api/resources/config.js b/src/http-api/resources/config.js index 214fdeb459..7344394a08 100644 --- a/src/http-api/resources/config.js +++ b/src/http-api/resources/config.js @@ -57,8 +57,10 @@ exports.getOrSet = { handler: (request, reply) => { const key = request.pre.args.key const value = request.pre.args.value + const ipfs = request.server.app.ipfs - if (typeof value === 'object' && value.type === 'Buffer') { + if (typeof value === 'object' && + value.type === 'Buffer') { return reply({ Message: 'Invalid value type', Code: 0 @@ -67,7 +69,7 @@ exports.getOrSet = { if (value === undefined) { // Get the value of a given key - return request.server.app.ipfs.config.get((err, config) => { + return ipfs.config.get((err, config) => { if (err) { log.error(err) return reply({ @@ -89,9 +91,20 @@ exports.getOrSet = { Value: value }) }) - } else { - // Set the new value of a given key - request.server.app.ipfs.config.get((err, originalConfig) => { + } + + // Set the new value of a given key + ipfs.config.get((err, originalConfig) => { + if (err) { + log.error(err) + return reply({ + Message: 'Failed to get config value: ' + err, + Code: 0 + }).code(500) + } + + const updatedConfig = set(originalConfig, key, value) + ipfs.config.replace(updatedConfig, (err) => { if (err) { log.error(err) return reply({ @@ -100,28 +113,37 @@ exports.getOrSet = { }).code(500) } - const updatedConfig = set(originalConfig, key, value) - request.server.app.ipfs.config.replace(updatedConfig, (err) => { - if (err) { - log.error(err) - return reply({ - Message: 'Failed to get config value: ' + err, - Code: 0 - }).code(500) - } - - return reply({ - Key: key, - Value: value - }) + return reply({ + Key: key, + Value: value }) }) - } + }) } } exports.get = (request, reply) => { - return request.server.app.ipfs.config.get((err, config) => { + const ipfs = request.server.app.ipfs + + ipfs.config.get((err, config) => { + if (err) { + log.error(err) + return reply({ + Message: 'Failed to get config value: ' + err, + Code: 0 + }).code(500) + } + + return reply({ + Value: config + }) + }) +} + +exports.show = (request, reply) => { + const ipfs = request.server.app.ipfs + + ipfs.config.get((err, config) => { if (err) { log.error(err) return reply({ diff --git a/src/http-api/routes/config.js b/src/http-api/routes/config.js index ae281e2a4c..452d84ed0b 100644 --- a/src/http-api/routes/config.js +++ b/src/http-api/routes/config.js @@ -19,7 +19,7 @@ module.exports = (server) => { api.route({ method: '*', path: '/api/v0/config/show', - handler: resources.config.get + handler: resources.config.show }) api.route({ diff --git a/test/cli/test-config.js b/test/cli/test-config.js index 010bab8153..0490946d29 100644 --- a/test/cli/test-config.js +++ b/test/cli/test-config.js @@ -123,16 +123,14 @@ describe('config', () => { }) after((done) => { - console.log('stopping') httpAPI.stop((err) => { - console.log('stopped') expect(err).to.not.exist done() }) }) describe('get/set', () => { - it.skip('get a config key value', (done) => { + it('get a config key value', (done) => { nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'config', 'Identity.PeerID'], {env}) .run((err, stdout, exitcode) => { const expected = 'QmQ2zigjQikYnyYUSXZydNXrDRhBut2mubwJBaLXobMt3A' diff --git a/test/http-api/ipfs-api/test-config.js b/test/http-api/ipfs-api/test-config.js index 601a24454c..e0c5012535 100644 --- a/test/http-api/ipfs-api/test-config.js +++ b/test/http-api/ipfs-api/test-config.js @@ -60,14 +60,14 @@ module.exports = (ctl) => { it('.get updatedConfig', (done) => { ctl.config.get((err, config) => { expect(err).not.to.exist - expect(config).to.deep.equal(updatedConfig()) + expect(config).to.be.eql(updatedConfig()) done() }) }) // This one is one stale mode till go-ipfs decides // what to do - describe.skip('.replace', () => { + describe('.replace', () => { it('returns error if the config is invalid', (done) => { const filePath = 'test/test-data/badconfig'