Skip to content

Commit 9ae1e69

Browse files
authored
Merge pull request ipfs#536 from ipfs/add-listing-config-profiles
feat: add test and docs for listing config profiles
2 parents b91c0c8 + fe89fe4 commit 9ae1e69

File tree

6 files changed

+168
-70
lines changed

6 files changed

+168
-70
lines changed

SPEC/CONFIG.md

+59
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
* [config.get](#configget)
44
* [config.set](#configset)
55
* [config.replace](#configreplace)
6+
* [config.profiles.list](#configprofileslist)
7+
* [config.profiles.apply](#configprofilesapply)
68

79
#### `config.get`
810

@@ -85,5 +87,62 @@ ipfs.config.replace(newConfig, (err) => {
8587

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

90+
#### `config.profiles.list`
91+
92+
> List available config profiles
93+
94+
##### `ipfs.config.profiles.list([options], [callback])`
95+
96+
`options` is a object.
97+
`callback` must follow `function (err, result) {}` signature, where `err` is an error if the operation was not successful.
98+
99+
If no callback is passed, a [promise][] is returned
100+
101+
**Example:**
102+
103+
```JavaScript
104+
ipfs.config.profiles.list(newConfig, (err, profiles) => {
105+
if (err) {
106+
throw err
107+
}
108+
109+
profiles.forEach(profile => {
110+
console.info(profile.name, profile.description)
111+
})
112+
})
113+
```
114+
115+
A great source of [examples][] can be found in the tests for this API.
116+
117+
#### `config.profiles.apply`
118+
119+
> Apply a config profile
120+
121+
##### `ipfs.config.profiles.apply(name, [options], [callback])`
122+
123+
`name` is a string. Call `config.profiles.list()` for a list of valid profile names.
124+
`options` an object that might contain the following values:
125+
- `dryRun` is a boolean which if true does not apply the profile
126+
`callback` must follow the `function (err, result) {}` signature, where `err` is an error if the operation was not successful.
127+
128+
If no callback is passed, a [promise][] is returned
129+
130+
**Example:**
131+
132+
```JavaScript
133+
ipfs.config.profiles.apply('lowpower', (err, diff) => {
134+
if (err) {
135+
throw err
136+
}
137+
138+
console.info(diff.old)
139+
console.info(diff.new)
140+
})
141+
```
142+
143+
Note that you will need to restart your node for config changes to take effect.
144+
145+
A great source of [examples][] can be found in the tests for this API.
146+
88147
[promise]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
89148
[examples]: https://github.com./ipfs/interface-ipfs-core/blob/master/src/config

src/config/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const tests = {
55
get: require('./get'),
66
set: require('./set'),
77
replace: require('./replace'),
8-
profile: require('./profile')
8+
profiles: require('./profiles')
99
}
1010

1111
module.exports = createSuite(tests)

src/config/profile.js

-69
This file was deleted.

src/config/profiles/apply.js

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const { getDescribe, getIt, expect } = require('../../utils/mocha')
5+
const waterfall = require('async/waterfall')
6+
7+
module.exports = (createCommon, options) => {
8+
const describe = getDescribe(options)
9+
const it = getIt(options)
10+
const common = createCommon()
11+
12+
describe('.config.profiles.apply', function () {
13+
this.timeout(30 * 1000)
14+
let ipfs
15+
16+
before(function (done) {
17+
// CI takes longer to instantiate the daemon, so we need to increase the
18+
// timeout for the before step
19+
this.timeout(60 * 1000)
20+
21+
common.setup((err, factory) => {
22+
expect(err).to.not.exist()
23+
factory.spawnNode((err, node) => {
24+
expect(err).to.not.exist()
25+
ipfs = node
26+
done()
27+
})
28+
})
29+
})
30+
31+
after((done) => common.teardown(done))
32+
33+
it('should apply a config profile', (done) => {
34+
let diff
35+
waterfall([
36+
(cb) => ipfs.config.profiles.apply('lowpower', cb),
37+
(_diff, cb) => {
38+
diff = _diff
39+
expect(diff.old.Swarm.ConnMgr.LowWater).to.not.equal(diff.new.Swarm.ConnMgr.LowWater)
40+
ipfs.config.get(cb)
41+
},
42+
(newConfig, cb) => {
43+
expect(newConfig.Swarm.ConnMgr.LowWater).to.equal(diff.new.Swarm.ConnMgr.LowWater)
44+
cb()
45+
}
46+
], done)
47+
})
48+
})
49+
}

src/config/profiles/index.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict'
2+
const { createSuite } = require('../../utils/suite')
3+
4+
const tests = {
5+
list: require('./list'),
6+
apply: require('./apply')
7+
}
8+
9+
module.exports = createSuite(tests)

src/config/profiles/list.js

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const { getDescribe, getIt, expect } = require('../../utils/mocha')
5+
const waterfall = require('async/waterfall')
6+
7+
module.exports = (createCommon, options) => {
8+
const describe = getDescribe(options)
9+
const it = getIt(options)
10+
const common = createCommon()
11+
12+
describe('.config.profiles.list', function () {
13+
this.timeout(30 * 1000)
14+
let ipfs
15+
16+
before(function (done) {
17+
// CI takes longer to instantiate the daemon, so we need to increase the
18+
// timeout for the before step
19+
this.timeout(60 * 1000)
20+
21+
common.setup((err, factory) => {
22+
expect(err).to.not.exist()
23+
factory.spawnNode((err, node) => {
24+
expect(err).to.not.exist()
25+
ipfs = node
26+
done()
27+
})
28+
})
29+
})
30+
31+
after((done) => common.teardown(done))
32+
33+
it('should list config profiles', (done) => {
34+
waterfall([
35+
(cb) => ipfs.config.profiles.list(cb),
36+
(profiles, cb) => {
37+
expect(profiles).to.be.an('array')
38+
expect(profiles).not.to.be.empty()
39+
40+
profiles.forEach(profile => {
41+
expect(profile.name).to.be.a('string')
42+
expect(profile.description).to.be.a('string')
43+
})
44+
45+
cb()
46+
}
47+
], done)
48+
})
49+
})
50+
}

0 commit comments

Comments
 (0)