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

feat: add config.getAll #3071

Merged
merged 3 commits into from
Jun 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 36 additions & 3 deletions docs/core-api/CONFIG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
# Config API <!-- omit in toc -->

- [`ipfs.config.get([key,] [options])`](#ipfsconfiggetkey-options)
- [`ipfs.config.get(key, [options])`](#ipfsconfiggetkey-options)
- [Parameters](#parameters)
- [Options](#options)
- [Returns](#returns)
- [Example](#example)
- [`ipfs.config.getAll([options])`](#ipfsconfiggetkey-options)
- [Options](#options)
- [Returns](#returns)
- [Example](#example)
- [`ipfs.config.set(key, value, [options])`](#ipfsconfigsetkey-value-options)
- [Parameters](#parameters-1)
- [Options](#options-1)
Expand All @@ -26,15 +30,15 @@
- [Returns](#returns-4)
- [Example](#example-4)

## `ipfs.config.get([key,] [options])`
## `ipfs.config.get(key, [options])`

> Returns the currently being used config. If the daemon is off, it returns the stored config.

### Parameters

| Name | Type | Description |
| ---- | ---- | ----------- |
| 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. |
| key | `String` | The key of the value that should be fetched from the config file. |

### Options

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

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

## `ipfs.config.getAll([options])`

> Returns the full config been used. If the daemon is off, it returns the stored config.

### Options

An optional object which may have the following keys:

| Name | Type | Default | Description |
| ---- | ---- | ------- | ----------- |
| timeout | `Number` | `undefined` | A timeout in ms |
| signal | [AbortSignal][] | `undefined` | Can be used to cancel any long running requests started as a result of this call |

### Returns

| Type | Description |
| -------- | -------- |
| `Promise<Object>` | An object containing the configuration of the IPFS node |

### Example

```JavaScript
const config = await ipfs.config.getAll()
console.log(config)
```

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


## `ipfs.config.set(key, value, [options])`

> Adds or replaces a config value.
Expand Down
33 changes: 29 additions & 4 deletions packages/interface-ipfs-core/src/config/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@ module.exports = (common, options) => {
}))
})

it('should retrieve the whole config', async () => {
const config = await ipfs.config.get()

expect(config).to.be.an('object')
it('should fail with error', async () => {
await expect(ipfs.config.get()).to.eventually.rejectedWith('key argument is required')
})

it('should retrieve a value through a key', async () => {
Expand All @@ -51,4 +49,31 @@ module.exports = (common, options) => {
return expect(ipfs.config.get('Bananas')).to.eventually.be.rejected()
})
})

describe('.config.getAll', function () {
this.timeout(30 * 1000)
let ipfs

before(async () => { ipfs = (await common.spawn()).api })

after(() => common.clean())

it('should respect timeout option when getting config values', () => {
return testTimeout(() => ipfs.config.getAll({
timeout: 1
}))
})

it('should retrieve the whole config', async () => {
const config = await ipfs.config.getAll()

expect(config).to.be.an('object')
})

it('should retrieve the whole config with options', async () => {
const config = await ipfs.config.getAll({ signal: undefined })

expect(config).to.be.an('object')
})
})
}
8 changes: 4 additions & 4 deletions packages/interface-ipfs-core/src/config/profiles/apply.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ module.exports = (common, options) => {
const diff = await ipfs.config.profiles.apply('lowpower')
expect(diff.original.Swarm.ConnMgr.LowWater).to.not.equal(diff.updated.Swarm.ConnMgr.LowWater)

const newConfig = await ipfs.config.get()
const newConfig = await ipfs.config.getAll()
expect(newConfig.Swarm.ConnMgr.LowWater).to.equal(diff.updated.Swarm.ConnMgr.LowWater)
})

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

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

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

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

const updatedConfig = await ipfs.config.get()
const updatedConfig = await ipfs.config.getAll()

expect(updatedConfig).to.deep.equal(originalConfig)
})
Expand Down
4 changes: 2 additions & 2 deletions packages/interface-ipfs-core/src/config/replace.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ module.exports = (common, options) => {
it('should replace the whole config', async () => {
await ipfs.config.replace(config)

const _config = await ipfs.config.get()
const _config = await ipfs.config.getAll()
expect(_config).to.deep.equal(config)
})

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

const _config = await ipfs.config.get()
const _config = await ipfs.config.getAll()
expect(_config).to.deep.equal({})
})
})
Expand Down
10 changes: 4 additions & 6 deletions packages/ipfs-http-client/src/config/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ const toUrlSearchParams = require('../lib/to-url-search-params')

module.exports = configure(api => {
return async (key, options = {}) => {
if (key && typeof key === 'object') {
options = key
key = null
if (!key) {
throw new Error('key argument is required')
}

const url = key ? 'config' : 'config/show'
const res = await api.post(url, {
const res = await api.post('config', {
timeout: options.timeout,
signal: options.signal,
searchParams: toUrlSearchParams({
Expand All @@ -22,6 +20,6 @@ module.exports = configure(api => {
})
const data = await res.json()

return key ? data.Value : data
return data.Value
}
})
20 changes: 20 additions & 0 deletions packages/ipfs-http-client/src/config/getAll.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict'

const configure = require('../lib/configure')
const toUrlSearchParams = require('../lib/to-url-search-params')

module.exports = configure(api => {
return async (options = {}) => {
const res = await api.post('config/show', {
timeout: options.timeout,
signal: options.signal,
searchParams: toUrlSearchParams({
...options
}),
headers: options.headers
})
const data = await res.json()

return data
}
})
1 change: 1 addition & 0 deletions packages/ipfs-http-client/src/config/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

module.exports = config => ({
getAll: require('./getAll')(config),
get: require('./get')(config),
set: require('./set')(config),
replace: require('./replace')(config),
Expand Down
2 changes: 1 addition & 1 deletion packages/ipfs/src/cli/commands/config/show.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module.exports = {
},

async handler ({ ctx: { ipfs, print }, timeout }) {
const config = await ipfs.config.get({
const config = await ipfs.config.getAll({
timeout
})
print(JSON.stringify(config, null, 4))
Expand Down
2 changes: 1 addition & 1 deletion packages/ipfs/src/core/components/bootstrap/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module.exports = ({ repo }) => {
throw new Error(`${multiaddr} is not a valid Multiaddr`)
}

const config = await repo.config.get()
const config = await repo.config.getAll()
if (options.default) {
config.Bootstrap = defaultConfig().Bootstrap
} else if (multiaddr && config.Bootstrap.indexOf(multiaddr) === -1) {
Expand Down
2 changes: 1 addition & 1 deletion packages/ipfs/src/core/components/bootstrap/rm.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module.exports = ({ repo }) => {
}

let res = []
const config = await repo.config.get()
const config = await repo.config.getAll()

if (options.all) {
res = config.Bootstrap || []
Expand Down
6 changes: 3 additions & 3 deletions packages/ipfs/src/core/components/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ const log = require('debug')('ipfs:core:config')

module.exports = ({ repo }) => {
return {
getAll: withTimeoutOption(repo.config.getAll),
get: withTimeoutOption((key, options) => {
if (!options && key && typeof key === 'object') {
options = key
key = undefined
if (!key) {
return Promise.reject(new Error('key argument is required'))
}

return repo.config.get(key, options)
Expand Down
2 changes: 1 addition & 1 deletion packages/ipfs/src/core/components/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ async function initNewRepo (repo, { privateKey, emptyRepo, bits, profiles, confi
}

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

if (newConfig || profiles) {
if (profiles) {
Expand Down
2 changes: 1 addition & 1 deletion packages/ipfs/src/core/components/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ module.exports = ({
await repo.open()
}

const config = await repo.config.get()
const config = await repo.config.getAll()
const addrs = []

if (config.Addresses && config.Addresses.Swarm) {
Expand Down
6 changes: 3 additions & 3 deletions packages/ipfs/src/http/api/resources/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ exports.getOrSet = {

let originalConfig
try {
originalConfig = await ipfs.config.get(undefined, {
originalConfig = await ipfs.config.getAll({
signal,
timeout
})
Expand Down Expand Up @@ -172,7 +172,7 @@ exports.get = {

let config
try {
config = await ipfs.config.get(undefined, {
config = await ipfs.config.getAll({
signal,
timeout
})
Expand Down Expand Up @@ -215,7 +215,7 @@ exports.show = {

let config
try {
config = await ipfs.config.get(undefined, {
config = await ipfs.config.getAll({
signal,
timeout
})
Expand Down
2 changes: 1 addition & 1 deletion packages/ipfs/src/http/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class HttpApi {

const ipfs = this._ipfs

const config = await ipfs.config.get()
const config = await ipfs.config.getAll()
config.Addresses = config.Addresses || {}

const apiAddrs = config.Addresses.API
Expand Down
5 changes: 3 additions & 2 deletions packages/ipfs/test/cli/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ describe('config', () => {
ipfs = {
config: {
set: sinon.stub(),
getAll: sinon.stub(),
get: sinon.stub(),
replace: sinon.stub(),
profiles: {
Expand Down Expand Up @@ -87,15 +88,15 @@ describe('config', () => {

describe('show', function () {
it('returns the full config', async () => {
ipfs.config.get.withArgs({
ipfs.config.getAll.withArgs({
timeout: undefined
}).returns({ foo: 'bar' })
const out = await cli('config show', { ipfs })
expect(JSON.parse(out)).to.be.eql({ foo: 'bar' })
})

it('returns the full config with a timeout', async () => {
ipfs.config.get.withArgs({
ipfs.config.getAll.withArgs({
timeout: 1000
}).returns({ foo: 'bar' })
const out = await cli('config show --timeout=1s', { ipfs })
Expand Down
8 changes: 4 additions & 4 deletions packages/ipfs/test/core/create-node.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('create node', function () {
preload: { enabled: false }
})

const config = await node.config.get()
const config = await node.config.getAll()
expect(config.Identity).to.exist()
await node.stop()
})
Expand All @@ -53,7 +53,7 @@ describe('create node', function () {
preload: { enabled: false }
})

const config = await node.config.get()
const config = await node.config.getAll()
expect(config.Identity).to.exist()
await node.stop()
})
Expand Down Expand Up @@ -104,7 +104,7 @@ describe('create node', function () {
preload: { enabled: false }
})

const config = await node.config.get()
const config = await node.config.getAll()
expect(config.Identity).to.exist()
expect(config.Identity.PrivKey.length).is.below(1024)
await node.stop()
Expand Down Expand Up @@ -152,7 +152,7 @@ describe('create node', function () {
preload: { enabled: false }
})

const config = await node.config.get()
const config = await node.config.getAll()
expect(config.Addresses.Swarm).to.eql(['/ip4/127.0.0.1/tcp/9977'])
expect(config.Bootstrap).to.eql([])
await node.stop()
Expand Down
Loading