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

Commit 16587f1

Browse files
authored
feat: add config.getAll (#3071)
This PR adds `ipfs.config.getAll(options?: Object)` and makes `key` in `ipfs.config.get(key: string, options?: object)` required. BREAKING CHANGES * key argument to `ipfs.config.key` is now required
1 parent b38c38f commit 16587f1

File tree

19 files changed

+132
-53
lines changed

19 files changed

+132
-53
lines changed

docs/core-api/CONFIG.md

+36-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
# Config API <!-- omit in toc -->
22

3-
- [`ipfs.config.get([key,] [options])`](#ipfsconfiggetkey-options)
3+
- [`ipfs.config.get(key, [options])`](#ipfsconfiggetkey-options)
44
- [Parameters](#parameters)
55
- [Options](#options)
66
- [Returns](#returns)
77
- [Example](#example)
8+
- [`ipfs.config.getAll([options])`](#ipfsconfiggetkey-options)
9+
- [Options](#options)
10+
- [Returns](#returns)
11+
- [Example](#example)
812
- [`ipfs.config.set(key, value, [options])`](#ipfsconfigsetkey-value-options)
913
- [Parameters](#parameters-1)
1014
- [Options](#options-1)
@@ -26,15 +30,15 @@
2630
- [Returns](#returns-4)
2731
- [Example](#example-4)
2832

29-
## `ipfs.config.get([key,] [options])`
33+
## `ipfs.config.get(key, [options])`
3034

3135
> Returns the currently being used config. If the daemon is off, it returns the stored config.
3236
3337
### Parameters
3438

3539
| Name | Type | Description |
3640
| ---- | ---- | ----------- |
37-
| key | `String` | The key of the value that should be fetched from the config file. If no key is passed, then the whole config will be returned. |
41+
| key | `String` | The key of the value that should be fetched from the config file. |
3842

3943
### Options
4044

@@ -60,6 +64,35 @@ console.log(config)
6064

6165
A great source of [examples][] can be found in the tests for this API.
6266

67+
## `ipfs.config.getAll([options])`
68+
69+
> Returns the full config been used. If the daemon is off, it returns the stored config.
70+
71+
### Options
72+
73+
An optional object which may have the following keys:
74+
75+
| Name | Type | Default | Description |
76+
| ---- | ---- | ------- | ----------- |
77+
| timeout | `Number` | `undefined` | A timeout in ms |
78+
| signal | [AbortSignal][] | `undefined` | Can be used to cancel any long running requests started as a result of this call |
79+
80+
### Returns
81+
82+
| Type | Description |
83+
| -------- | -------- |
84+
| `Promise<Object>` | An object containing the configuration of the IPFS node |
85+
86+
### Example
87+
88+
```JavaScript
89+
const config = await ipfs.config.getAll()
90+
console.log(config)
91+
```
92+
93+
A great source of [examples][] can be found in the tests for this API.
94+
95+
6396
## `ipfs.config.set(key, value, [options])`
6497

6598
> Adds or replaces a config value.

packages/interface-ipfs-core/src/config/get.js

+29-4
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,8 @@ module.exports = (common, options) => {
2727
}))
2828
})
2929

30-
it('should retrieve the whole config', async () => {
31-
const config = await ipfs.config.get()
32-
33-
expect(config).to.be.an('object')
30+
it('should fail with error', async () => {
31+
await expect(ipfs.config.get()).to.eventually.rejectedWith('key argument is required')
3432
})
3533

3634
it('should retrieve a value through a key', async () => {
@@ -51,4 +49,31 @@ module.exports = (common, options) => {
5149
return expect(ipfs.config.get('Bananas')).to.eventually.be.rejected()
5250
})
5351
})
52+
53+
describe('.config.getAll', function () {
54+
this.timeout(30 * 1000)
55+
let ipfs
56+
57+
before(async () => { ipfs = (await common.spawn()).api })
58+
59+
after(() => common.clean())
60+
61+
it('should respect timeout option when getting config values', () => {
62+
return testTimeout(() => ipfs.config.getAll({
63+
timeout: 1
64+
}))
65+
})
66+
67+
it('should retrieve the whole config', async () => {
68+
const config = await ipfs.config.getAll()
69+
70+
expect(config).to.be.an('object')
71+
})
72+
73+
it('should retrieve the whole config with options', async () => {
74+
const config = await ipfs.config.getAll({ signal: undefined })
75+
76+
expect(config).to.be.an('object')
77+
})
78+
})
5479
}

packages/interface-ipfs-core/src/config/profiles/apply.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ module.exports = (common, options) => {
3333
const diff = await ipfs.config.profiles.apply('lowpower')
3434
expect(diff.original.Swarm.ConnMgr.LowWater).to.not.equal(diff.updated.Swarm.ConnMgr.LowWater)
3535

36-
const newConfig = await ipfs.config.get()
36+
const newConfig = await ipfs.config.getAll()
3737
expect(newConfig.Swarm.ConnMgr.LowWater).to.equal(diff.updated.Swarm.ConnMgr.LowWater)
3838
})
3939

4040
it('should strip private key from diff output', async () => {
41-
const originalConfig = await ipfs.config.get()
41+
const originalConfig = await ipfs.config.getAll()
4242
const diff = await ipfs.config.profiles.apply('default-networking', { dryRun: true })
4343

4444
// should have stripped private key from diff output
@@ -48,11 +48,11 @@ module.exports = (common, options) => {
4848
})
4949

5050
it('should not apply a config profile in dry-run mode', async () => {
51-
const originalConfig = await ipfs.config.get()
51+
const originalConfig = await ipfs.config.getAll()
5252

5353
await ipfs.config.profiles.apply('server', { dryRun: true })
5454

55-
const updatedConfig = await ipfs.config.get()
55+
const updatedConfig = await ipfs.config.getAll()
5656

5757
expect(updatedConfig).to.deep.equal(originalConfig)
5858
})

packages/interface-ipfs-core/src/config/replace.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ module.exports = (common, options) => {
3636
it('should replace the whole config', async () => {
3737
await ipfs.config.replace(config)
3838

39-
const _config = await ipfs.config.get()
39+
const _config = await ipfs.config.getAll()
4040
expect(_config).to.deep.equal(config)
4141
})
4242

4343
it('should replace to empty config', async () => {
4444
await ipfs.config.replace({})
4545

46-
const _config = await ipfs.config.get()
46+
const _config = await ipfs.config.getAll()
4747
expect(_config).to.deep.equal({})
4848
})
4949
})

packages/ipfs-http-client/src/config/get.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@ const toUrlSearchParams = require('../lib/to-url-search-params')
55

66
module.exports = configure(api => {
77
return async (key, options = {}) => {
8-
if (key && typeof key === 'object') {
9-
options = key
10-
key = null
8+
if (!key) {
9+
throw new Error('key argument is required')
1110
}
1211

13-
const url = key ? 'config' : 'config/show'
14-
const res = await api.post(url, {
12+
const res = await api.post('config', {
1513
timeout: options.timeout,
1614
signal: options.signal,
1715
searchParams: toUrlSearchParams({
@@ -22,6 +20,6 @@ module.exports = configure(api => {
2220
})
2321
const data = await res.json()
2422

25-
return key ? data.Value : data
23+
return data.Value
2624
}
2725
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict'
2+
3+
const configure = require('../lib/configure')
4+
const toUrlSearchParams = require('../lib/to-url-search-params')
5+
6+
module.exports = configure(api => {
7+
return async (options = {}) => {
8+
const res = await api.post('config/show', {
9+
timeout: options.timeout,
10+
signal: options.signal,
11+
searchParams: toUrlSearchParams({
12+
...options
13+
}),
14+
headers: options.headers
15+
})
16+
const data = await res.json()
17+
18+
return data
19+
}
20+
})

packages/ipfs-http-client/src/config/index.js

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

33
module.exports = config => ({
4+
getAll: require('./getAll')(config),
45
get: require('./get')(config),
56
set: require('./set')(config),
67
replace: require('./replace')(config),

packages/ipfs/src/cli/commands/config/show.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ module.exports = {
1818
},
1919

2020
async handler ({ ctx: { ipfs, print }, timeout }) {
21-
const config = await ipfs.config.get({
21+
const config = await ipfs.config.getAll({
2222
timeout
2323
})
2424
print(JSON.stringify(config, null, 4))

packages/ipfs/src/core/components/bootstrap/add.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module.exports = ({ repo }) => {
1212
throw new Error(`${multiaddr} is not a valid Multiaddr`)
1313
}
1414

15-
const config = await repo.config.get()
15+
const config = await repo.config.getAll()
1616
if (options.default) {
1717
config.Bootstrap = defaultConfig().Bootstrap
1818
} else if (multiaddr && config.Bootstrap.indexOf(multiaddr) === -1) {

packages/ipfs/src/core/components/bootstrap/rm.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module.exports = ({ repo }) => {
1212
}
1313

1414
let res = []
15-
const config = await repo.config.get()
15+
const config = await repo.config.getAll()
1616

1717
if (options.all) {
1818
res = config.Bootstrap || []

packages/ipfs/src/core/components/config.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ const log = require('debug')('ipfs:core:config')
66

77
module.exports = ({ repo }) => {
88
return {
9+
getAll: withTimeoutOption(repo.config.getAll),
910
get: withTimeoutOption((key, options) => {
10-
if (!options && key && typeof key === 'object') {
11-
options = key
12-
key = undefined
11+
if (!key) {
12+
return Promise.reject(new Error('key argument is required'))
1313
}
1414

1515
return repo.config.get(key, options)

packages/ipfs/src/core/components/init.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ async function initNewRepo (repo, { privateKey, emptyRepo, bits, profiles, confi
227227
}
228228

229229
async function initExistingRepo (repo, { config: newConfig, profiles, pass }) {
230-
let config = await repo.config.get()
230+
let config = await repo.config.getAll()
231231

232232
if (newConfig || profiles) {
233233
if (profiles) {

packages/ipfs/src/core/components/start.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ module.exports = ({
3434
await repo.open()
3535
}
3636

37-
const config = await repo.config.get()
37+
const config = await repo.config.getAll()
3838
const addrs = []
3939

4040
if (config.Addresses && config.Addresses.Swarm) {

packages/ipfs/src/http/api/resources/config.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ exports.getOrSet = {
9999

100100
let originalConfig
101101
try {
102-
originalConfig = await ipfs.config.get(undefined, {
102+
originalConfig = await ipfs.config.getAll({
103103
signal,
104104
timeout
105105
})
@@ -172,7 +172,7 @@ exports.get = {
172172

173173
let config
174174
try {
175-
config = await ipfs.config.get(undefined, {
175+
config = await ipfs.config.getAll({
176176
signal,
177177
timeout
178178
})
@@ -215,7 +215,7 @@ exports.show = {
215215

216216
let config
217217
try {
218-
config = await ipfs.config.get(undefined, {
218+
config = await ipfs.config.getAll({
219219
signal,
220220
timeout
221221
})

packages/ipfs/src/http/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class HttpApi {
5454

5555
const ipfs = this._ipfs
5656

57-
const config = await ipfs.config.get()
57+
const config = await ipfs.config.getAll()
5858
config.Addresses = config.Addresses || {}
5959

6060
const apiAddrs = config.Addresses.API

packages/ipfs/test/cli/config.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ describe('config', () => {
1414
ipfs = {
1515
config: {
1616
set: sinon.stub(),
17+
getAll: sinon.stub(),
1718
get: sinon.stub(),
1819
replace: sinon.stub(),
1920
profiles: {
@@ -87,15 +88,15 @@ describe('config', () => {
8788

8889
describe('show', function () {
8990
it('returns the full config', async () => {
90-
ipfs.config.get.withArgs({
91+
ipfs.config.getAll.withArgs({
9192
timeout: undefined
9293
}).returns({ foo: 'bar' })
9394
const out = await cli('config show', { ipfs })
9495
expect(JSON.parse(out)).to.be.eql({ foo: 'bar' })
9596
})
9697

9798
it('returns the full config with a timeout', async () => {
98-
ipfs.config.get.withArgs({
99+
ipfs.config.getAll.withArgs({
99100
timeout: 1000
100101
}).returns({ foo: 'bar' })
101102
const out = await cli('config show --timeout=1s', { ipfs })

packages/ipfs/test/core/create-node.spec.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ describe('create node', function () {
3434
preload: { enabled: false }
3535
})
3636

37-
const config = await node.config.get()
37+
const config = await node.config.getAll()
3838
expect(config.Identity).to.exist()
3939
await node.stop()
4040
})
@@ -53,7 +53,7 @@ describe('create node', function () {
5353
preload: { enabled: false }
5454
})
5555

56-
const config = await node.config.get()
56+
const config = await node.config.getAll()
5757
expect(config.Identity).to.exist()
5858
await node.stop()
5959
})
@@ -104,7 +104,7 @@ describe('create node', function () {
104104
preload: { enabled: false }
105105
})
106106

107-
const config = await node.config.get()
107+
const config = await node.config.getAll()
108108
expect(config.Identity).to.exist()
109109
expect(config.Identity.PrivKey.length).is.below(1024)
110110
await node.stop()
@@ -152,7 +152,7 @@ describe('create node', function () {
152152
preload: { enabled: false }
153153
})
154154

155-
const config = await node.config.get()
155+
const config = await node.config.getAll()
156156
expect(config.Addresses.Swarm).to.eql(['/ip4/127.0.0.1/tcp/9977'])
157157
expect(config.Bootstrap).to.eql([])
158158
await node.stop()

0 commit comments

Comments
 (0)