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

Commit 2beac9c

Browse files
fix(config): better http-api and interface-ipfs-core compliant
1 parent 0e99029 commit 2beac9c

File tree

5 files changed

+54
-62
lines changed

5 files changed

+54
-62
lines changed

src/cli/commands/config.js

+7-35
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
'use strict'
22

33
const debug = require('debug')
4-
const get = require('lodash.get')
5-
const set = require('lodash.set')
64
const log = debug('cli:config')
75
log.error = debug('cli:config:error')
86
const utils = require('../utils')
@@ -43,26 +41,17 @@ module.exports = {
4341

4442
if (!value) {
4543
// Get the value of a given key
46-
47-
if (utils.isDaemonOn()) {
48-
return ipfs.config.get(key, (err, config) => {
49-
if (err) {
50-
log.error(err)
51-
throw new Error('failed to read the config')
52-
}
53-
54-
console.log(config.Value)
55-
})
56-
}
57-
58-
ipfs.config.get((err, config) => {
44+
ipfs.config.get(key, (err, value) => {
5945
if (err) {
6046
log.error(err)
6147
throw new Error('failed to read the config')
6248
}
6349

64-
const value = get(config, key)
65-
console.log(value)
50+
if (typeof value === 'object') {
51+
console.log(JSON.stringify(value, null, 2))
52+
} else {
53+
console.log(value)
54+
}
6655
})
6756
} else {
6857
// Set the new value of a given key
@@ -78,28 +67,11 @@ module.exports = {
7867
}
7968
}
8069

81-
if (utils.isDaemonOn()) {
82-
return ipfs.config.set(key, value, (err) => {
83-
if (err) {
84-
log.error(err)
85-
throw new Error('failed to save the config')
86-
}
87-
})
88-
}
89-
90-
ipfs.config.get((err, originalConfig) => {
70+
ipfs.config.set(key, value, (err) => {
9171
if (err) {
9272
log.error(err)
9373
throw new Error('failed to read the config')
9474
}
95-
96-
const updatedConfig = set(originalConfig, key, value)
97-
ipfs.config.replace(updatedConfig, (err) => {
98-
if (err) {
99-
log.error(err)
100-
throw new Error('failed to save the config')
101-
}
102-
})
10375
})
10476
}
10577
})

src/http-api/resources/config.js

+43-21
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,10 @@ exports.getOrSet = {
5757
handler: (request, reply) => {
5858
const key = request.pre.args.key
5959
const value = request.pre.args.value
60+
const ipfs = request.server.app.ipfs
6061

61-
if (typeof value === 'object' && value.type === 'Buffer') {
62+
if (typeof value === 'object' &&
63+
value.type === 'Buffer') {
6264
return reply({
6365
Message: 'Invalid value type',
6466
Code: 0
@@ -67,7 +69,7 @@ exports.getOrSet = {
6769

6870
if (value === undefined) {
6971
// Get the value of a given key
70-
return request.server.app.ipfs.config.get((err, config) => {
72+
return ipfs.config.get((err, config) => {
7173
if (err) {
7274
log.error(err)
7375
return reply({
@@ -89,9 +91,20 @@ exports.getOrSet = {
8991
Value: value
9092
})
9193
})
92-
} else {
93-
// Set the new value of a given key
94-
request.server.app.ipfs.config.get((err, originalConfig) => {
94+
}
95+
96+
// Set the new value of a given key
97+
ipfs.config.get((err, originalConfig) => {
98+
if (err) {
99+
log.error(err)
100+
return reply({
101+
Message: 'Failed to get config value: ' + err,
102+
Code: 0
103+
}).code(500)
104+
}
105+
106+
const updatedConfig = set(originalConfig, key, value)
107+
ipfs.config.replace(updatedConfig, (err) => {
95108
if (err) {
96109
log.error(err)
97110
return reply({
@@ -100,28 +113,37 @@ exports.getOrSet = {
100113
}).code(500)
101114
}
102115

103-
const updatedConfig = set(originalConfig, key, value)
104-
request.server.app.ipfs.config.replace(updatedConfig, (err) => {
105-
if (err) {
106-
log.error(err)
107-
return reply({
108-
Message: 'Failed to get config value: ' + err,
109-
Code: 0
110-
}).code(500)
111-
}
112-
113-
return reply({
114-
Key: key,
115-
Value: value
116-
})
116+
return reply({
117+
Key: key,
118+
Value: value
117119
})
118120
})
119-
}
121+
})
120122
}
121123
}
122124

123125
exports.get = (request, reply) => {
124-
return request.server.app.ipfs.config.get((err, config) => {
126+
const ipfs = request.server.app.ipfs
127+
128+
ipfs.config.get((err, config) => {
129+
if (err) {
130+
log.error(err)
131+
return reply({
132+
Message: 'Failed to get config value: ' + err,
133+
Code: 0
134+
}).code(500)
135+
}
136+
137+
return reply({
138+
Value: config
139+
})
140+
})
141+
}
142+
143+
exports.show = (request, reply) => {
144+
const ipfs = request.server.app.ipfs
145+
146+
ipfs.config.get((err, config) => {
125147
if (err) {
126148
log.error(err)
127149
return reply({

src/http-api/routes/config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module.exports = (server) => {
1919
api.route({
2020
method: '*',
2121
path: '/api/v0/config/show',
22-
handler: resources.config.get
22+
handler: resources.config.show
2323
})
2424

2525
api.route({

test/cli/test-config.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -123,16 +123,14 @@ describe('config', () => {
123123
})
124124

125125
after((done) => {
126-
console.log('stopping')
127126
httpAPI.stop((err) => {
128-
console.log('stopped')
129127
expect(err).to.not.exist
130128
done()
131129
})
132130
})
133131

134132
describe('get/set', () => {
135-
it.skip('get a config key value', (done) => {
133+
it('get a config key value', (done) => {
136134
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'config', 'Identity.PeerID'], {env})
137135
.run((err, stdout, exitcode) => {
138136
const expected = 'QmQ2zigjQikYnyYUSXZydNXrDRhBut2mubwJBaLXobMt3A'

test/http-api/ipfs-api/test-config.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,14 @@ module.exports = (ctl) => {
6060
it('.get updatedConfig', (done) => {
6161
ctl.config.get((err, config) => {
6262
expect(err).not.to.exist
63-
expect(config).to.deep.equal(updatedConfig())
63+
expect(config).to.be.eql(updatedConfig())
6464
done()
6565
})
6666
})
6767

6868
// This one is one stale mode till go-ipfs decides
6969
// what to do
70-
describe.skip('.replace', () => {
70+
describe('.replace', () => {
7171
it('returns error if the config is invalid', (done) => {
7272
const filePath = 'test/test-data/badconfig'
7373

0 commit comments

Comments
 (0)