Skip to content
This repository has been archived by the owner on Mar 10, 2020. It is now read-only.

config API #39

Merged
merged 1 commit into from
Jul 2, 2016
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
52 changes: 52 additions & 0 deletions API/config/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
config API
==========

#### `config.get`

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

##### `Go` **WIP**

##### `JavaScript` - ipfs.config.get([key, callback])

`key` is the key of the value that should be fetched from the config file. If no key is passed, then the whole config should be returned. `key` should be of type String.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can key be in the form of my.config.value? as far as I know that is available in go

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if I understand what you mean

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There ia no documentation about the fact that you can set nested valuea

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setting nested values is on set


`callback` must follow `function (err, config) {}` signature, where `err` is an error if the operation was not successful and `config` is a JSON object containing the configuration of the IPFS node.

If no callback is passed, a [promise][] is returned

#### `config.set`

> Adds or replaces a config value.

##### `Go` **WIP**

##### `JavaScript` - ipfs.config.set(key, value, [callback])

`key` is the key value that will be added or replaced (in case of the value already). `key` should be of type String.

`value` value to be set.

`callback` must follow `function (err) {}` signature, where `err` is an error if the operation was not successful.

If no callback is passed, a [promise][] is returned

Note that this operation will **not** spark the restart of any service, i.e: if a config.replace changes the multiaddrs of the Swarm, Swarm will have to be restarted manually for the changes to take difference.

#### `config.replace`

> Adds or replaces a config value.

##### `Go` **WIP**

##### `JavaScript` - ipfs.config.replace(config, [callback])

`config` is a JSON object that contains the new config.

`callback` must follow `function (err) {}` signature, where `err` is an error if the operation was not successful.

If no callback is passed, a [promise][] is returned

Note that this operation will **not** spark the restart of any service, i.e: if a config.replace changes the multiaddrs of the Swarm, Swarm will have to be restarted manually for the changes to take difference.

[promise]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
179 changes: 179 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
/* eslint-env mocha */
'use strict'

const expect = require('chai').expect

module.exports = (common) => {
describe('.config', () => {
let ipfs

before((done) => {
common.setup((err, _ipfs) => {
expect(err).to.not.exist
ipfs = _ipfs
done()
})
})

after((done) => {
common.teardown(done)
})

describe('callback API', () => {
describe('.get', () => {
it('retrieve the whole config', (done) => {
ipfs.config.get((err, config) => {
expect(err).to.not.exist
expect(config).to.exist
done()
})
})

it('retrieve a value through a key', (done) => {
ipfs.config.get('Identity', (err, identity) => {
expect(err).to.not.exist
expect(identity).to.exist
done()
})
})

it('retrieve a value through a nested key', (done) => {
ipfs.config.get('Addresses.Swarm', (err, swarmAddrs) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about nested arrays?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that is required as in: will ever be used, since a value used by a component of the protocol will always look for a key that belongs to it and not to a partial set of the value of a key.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I disagree. For example it makes sense to only retrieve or set a specific address via the cli, and those are in arrays

Copy link
Contributor Author

@daviddias daviddias Jul 2, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And how will you filter those as a User? I would have to get the full thing first to know the order.

expect(err).to.not.exist
expect(swarmAddrs).to.exist
done()
})
})

it('fail on non valid key', (done) => {
ipfs.config.get(1234, (err, peerId) => {
expect(err).to.exist
done()
})
})

it('fail on non existent key', (done) => {
ipfs.config.get('Bananas', (err, peerId) => {
expect(err).to.exist
done()
})
})
})
describe('.set', () => {
it('set a new key', (done) => {
ipfs.config.set('Fruit', 'banana', (err) => {
expect(err).to.not.exist
ipfs.config.get('Fruit', (err, fruit) => {
expect(err).to.not.exist
expect(fruit).to.equal('banana')
done()
})
})
})

it('set an already existing key', (done) => {
ipfs.config.set('Fruit', 'morango', (err) => {
expect(err).to.not.exist
ipfs.config.get('Fruit', (err, fruit) => {
expect(err).to.not.exist
expect(fruit).to.equal('morango')
done()
})
})
})

it('set a JSON object', (done) => {
const key = 'API.HTTPHeaders.Access-Control-Allow-Origin'
const val = ['http://example.io']
ipfs.config.set(key, val, function (err) {
expect(err).to.not.exist
ipfs.config.get(key, function (err, result) {
expect(err).to.not.exist
expect(result).to.deep.equal(val)
done()
})
})
})

it('fail on non valid key', (done) => {
ipfs.config.set(new Buffer('heeey'), '', (err) => {
expect(err).to.exist
done()
})
})

it('fail on non valid value', (done) => {
ipfs.config.set('Fruit', new Buffer('abc'), (err) => {
expect(err).to.exist
done()
})
})
})

// Waiting for fix on go-ipfs
// - https://github.com/ipfs/js-ipfs-api/pull/307#discussion_r69281789
// - https://github.com/ipfs/go-ipfs/issues/2927
describe.skip('.replace', () => {
const config = {
Fruit: 'Bananas'
}

it('replace the whole config', (done) => {
ipfs.config.replace(config, (err) => {
expect(err).to.not.exist
ipfs.config.get((err, _config) => {
expect(err).to.not.exist
expect(_config).to.deep.equal(config)
})
})
})

it('replace to empty config', (done) => {
ipfs.config.replace({}, (err) => {
expect(err).to.not.exist
ipfs.config.get((err, _config) => {
expect(err).to.not.exist
expect(_config).to.deep.equal(config)
})
})
})
})
})

describe('promise API', () => {
describe('.get', () => {
it('retrieve the whole config', (done) => {
ipfs.config.get()
.then((config) => {
expect(config).to.exist
done()
})
.catch((err) => {
expect(err).to.not.exist
})
})
})

describe('.set', () => {
it('set a new key', (done) => {
ipfs.config.set('Fruit', 'banana')
.then(() => {
ipfs.config.get('Fruit', (err, fruit) => {
expect(err).to.not.exist
expect(fruit).to.equal('banana')
done()
})
})
.catch((err) => {
expect(err).to.not.exist
})
})
})

// Waiting for fix on go-ipfs
// - https://github.com/ipfs/js-ipfs-api/pull/307#discussion_r69281789
// - https://github.com/ipfs/go-ipfs/issues/2927
describe.skip('.replace', () => {})
})
})
}
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict'

exports.all = () => {}
exports.object = require('./object')
exports.files = require('./files')
exports.config = require('./config')