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

Commit

Permalink
fix(config): better http-api and interface-ipfs-core compliant
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed Sep 15, 2016
1 parent 0e99029 commit 2beac9c
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 62 deletions.
42 changes: 7 additions & 35 deletions src/cli/commands/config.js
Original file line number Diff line number Diff line change
@@ -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')
Expand Down Expand Up @@ -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
Expand All @@ -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')
}
})
})
}
})
Expand Down
64 changes: 43 additions & 21 deletions src/http-api/resources/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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({
Expand All @@ -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({
Expand All @@ -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({
Expand Down
2 changes: 1 addition & 1 deletion src/http-api/routes/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
4 changes: 1 addition & 3 deletions test/cli/test-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
4 changes: 2 additions & 2 deletions test/http-api/ipfs-api/test-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down

0 comments on commit 2beac9c

Please sign in to comment.