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

Commit

Permalink
Merge pull request #109 from ipfs/fix/config
Browse files Browse the repository at this point in the history
Make ipfs config commands work with daemon running
  • Loading branch information
daviddias committed Apr 6, 2016
2 parents 42016ec + 14d35a6 commit fdcc5af
Show file tree
Hide file tree
Showing 5 changed files with 231 additions and 72 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
"debug": "^2.2.0",
"fs-blob-store": "^5.2.1",
"hapi": "^13.3.0",
"ipfs-api": "github:ipfs/js-ipfs-api#1fd9749",
"ipfs-api": "github:ipfs/js-ipfs-api#f3e2d42",
"ipfs-blocks": "^0.1.0",
"ipfs-data-importing": "^0.3.3",
"ipfs-merkle-dag": "^0.3.0",
Expand Down
20 changes: 20 additions & 0 deletions src/cli/commands/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ module.exports = Command.extend({
if (!value) {
// Get the value of a given key

if (utils.isDaemonOn()) {
return ipfs.config.get(key, (err, config) => {
if (err) {
log.error(err)
throw new Error('failed to read the config')
}

console.log(config.Value)
})
}

ipfs.config.show((err, config) => {
if (err) {
log.error(err)
Expand All @@ -56,6 +67,15 @@ module.exports = Command.extend({
}
}

if (utils.isDaemonOn()) {
return ipfs.config.set(key, value, (err) => {
if (err) {
log.error(err)
throw new Error('failed to save the config')
}
})
}

ipfs.config.show((err, originalConfig) => {
if (err) {
log.error(err)
Expand Down
3 changes: 3 additions & 0 deletions src/cli/commands/config/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ module.exports = Command.extend({
}

function saveConfig (config, next) {
config = utils.isDaemonOn()
? new Buffer(JSON.stringify(config)) : config

ipfs.config.replace(config, (err) => {
if (err) {
log.error(err)
Expand Down
11 changes: 7 additions & 4 deletions src/cli/commands/config/replace.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const path = require('path')
const log = debug('cli:config')
log.error = debug('cli:config:error')
const utils = require('../../utils')
const fs = require('fs')

module.exports = Command.extend({
desc: 'Replaces the config with <file>',
Expand All @@ -15,14 +16,16 @@ module.exports = Command.extend({
if (err) {
throw err
}
const config = require(path.resolve(process.cwd(), configPath))

ipfs.config.replace(config, (err, version) => {
const filePath = path.resolve(process.cwd(), configPath)

const config = utils.isDaemonOn()
? filePath : JSON.parse(fs.readFileSync(filePath, 'utf8'))

ipfs.config.replace(config, (err) => {
if (err) {
throw err
}

console.log(version)
})
})
}
Expand Down
267 changes: 200 additions & 67 deletions test/cli-tests/test-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,87 +3,220 @@
const expect = require('chai').expect
const nexpect = require('nexpect')
const fs = require('fs')
const httpAPI = require('../../src/http-api')

describe('config', () => {
const repoTests = require('./index').repoTests
const configPath = repoTests + '/config'
const originalConfigPath = process.cwd() + '/test/go-ipfs-repo/config'
const updatedConfig = () => JSON.parse(fs.readFileSync(configPath, 'utf8'))
const restoreConfig = () => fs.writeFileSync(configPath, fs.readFileSync(originalConfigPath, 'utf8'), 'utf8')

describe('api offline', () => {
const repoTests = require('./index').repoTests
const configPath = repoTests + '/config'

const updatedConfig = () => JSON.parse(fs.readFileSync(configPath, 'utf8'))

it('get a config key value', (done) => {
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'config', 'Identity.PeerID'])
.run((err, stdout, exitcode) => {
const expected = 'QmQ2zigjQikYnyYUSXZydNXrDRhBut2mubwJBaLXobMt3A'
expect(stdout[0]).to.equal(expected)
expect(err).to.not.exist
expect(exitcode).to.equal(0)
done()
})
})
describe('get/set', () => {
it('get a config key value', (done) => {
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'config', 'Identity.PeerID'])
.run((err, stdout, exitcode) => {
const expected = 'QmQ2zigjQikYnyYUSXZydNXrDRhBut2mubwJBaLXobMt3A'
expect(stdout[0]).to.equal(expected)
expect(err).to.not.exist
expect(exitcode).to.equal(0)
done()
})
})

it('set a config key with a string value', (done) => {
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'config', 'foo', 'bar'])
.run((err, stdout, exitcode) => {
expect(err).to.not.exist
expect(exitcode).to.equal(0)
expect(updatedConfig().foo).to.equal('bar')
done()
})
})

it('set a config key with true', (done) => {
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'config', 'foo', true, '--bool'])
.run((err, stdout, exitcode) => {
expect(err).to.not.exist
expect(exitcode).to.equal(0)
expect(updatedConfig().foo).to.equal(true)
done()
})
})

it('set a config key with false', (done) => {
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'config', 'foo', false, '--bool'])
.run((err, stdout, exitcode) => {
expect(err).to.not.exist
expect(exitcode).to.equal(0)
expect(updatedConfig().foo).to.equal(false)
done()
})
})

it('set a config key with json', (done) => {
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'config', 'foo', '{"bar": 0}', '--json'])
.run((err, stdout, exitcode) => {
expect(err).to.not.exist
expect(exitcode).to.equal(0)
expect(updatedConfig().foo).to.deep.equal({ bar: 0 })
done()
})
})

it('set a config key with a string value', (done) => {
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'config', 'foo', 'bar'])
.run((err, stdout, exitcode) => {
expect(err).to.not.exist
expect(exitcode).to.equal(0)
expect(updatedConfig().foo).to.equal('bar')
done()
})
it('set a config key with invalid json', (done) => {
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'config', 'foo', '{"bar: 0}', '--json'])
.run((err, stdout, exitcode) => {
const expected = 'error\tinvalid JSON provided'
expect(stdout[0]).to.equal(expected)
expect(err).to.not.exist
expect(exitcode).to.equal(1)
done()
})
})

it('call config with no arguments', (done) => {
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'config'])
.run((err, stdout, exitcode) => {
const expected = "error\targument 'key' is required"
expect(stdout[0]).to.equal(expected)
expect(err).to.not.exist
expect(exitcode).to.equal(1)
done()
})
})
})

it('set a config key with true', (done) => {
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'config', 'foo', true, '--bool'])
.run((err, stdout, exitcode) => {
expect(err).to.not.exist
expect(exitcode).to.equal(0)
expect(updatedConfig().foo).to.equal(true)
done()
})
describe('replace', () => {
it('replace config with file', (done) => {
const filePath = 'test/test-data/otherconfig'
const expectedConfig = JSON.parse(fs.readFileSync(filePath, 'utf8'))

nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'config', 'replace', filePath])
.run((err, stdout, exitcode) => {
expect(err).to.not.exist
expect(exitcode).to.equal(0)

expect(updatedConfig()).to.deep.equal(expectedConfig)
done()
})
})

after(() => {
restoreConfig()
})
})
})

it('set a config key with false', (done) => {
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'config', 'foo', false, '--bool'])
.run((err, stdout, exitcode) => {
expect(err).to.not.exist
expect(exitcode).to.equal(0)
expect(updatedConfig().foo).to.equal(false)
done()
})
describe('api running', () => {
before((done) => {
httpAPI.start((err) => {
expect(err).to.not.exist
done()
})
})

it('set a config key with json', (done) => {
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'config', 'foo', '{"bar": 0}', '--json'])
.run((err, stdout, exitcode) => {
expect(err).to.not.exist
expect(exitcode).to.equal(0)
expect(updatedConfig().foo).to.deep.equal({ bar: 0 })
done()
})
after((done) => {
httpAPI.stop((err) => {
expect(err).to.not.exist
done()
})
})

it('set a config key with invalid json', (done) => {
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'config', 'foo', '{"bar: 0}', '--json'])
.run((err, stdout, exitcode) => {
const expected = 'error\tinvalid JSON provided'
expect(stdout[0]).to.equal(expected)
expect(err).to.not.exist
expect(exitcode).to.equal(1)
done()
})
describe('get/set', () => {
it('get a config key value', (done) => {
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'config', 'Identity.PeerID'])
.run((err, stdout, exitcode) => {
const expected = 'QmQ2zigjQikYnyYUSXZydNXrDRhBut2mubwJBaLXobMt3A'
expect(stdout[0]).to.equal(expected)
expect(err).to.not.exist
expect(exitcode).to.equal(0)
done()
})
})

it('set a config key with a string value', (done) => {
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'config', 'foo', 'bar'])
.run((err, stdout, exitcode) => {
expect(err).to.not.exist
expect(exitcode).to.equal(0)
expect(updatedConfig().foo).to.equal('bar')
done()
})
})

it('set a config key with true', (done) => {
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'config', 'foo', true, '--bool'])
.run((err, stdout, exitcode) => {
expect(err).to.not.exist
expect(exitcode).to.equal(0)
expect(updatedConfig().foo).to.equal(true)
done()
})
})

it('set a config key with false', (done) => {
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'config', 'foo', false, '--bool'])
.run((err, stdout, exitcode) => {
expect(err).to.not.exist
expect(exitcode).to.equal(0)
expect(updatedConfig().foo).to.equal(false)
done()
})
})

it('set a config key with json', (done) => {
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'config', 'foo', '{"bar": 0}', '--json'])
.run((err, stdout, exitcode) => {
expect(err).to.not.exist
expect(exitcode).to.equal(0)
expect(updatedConfig().foo).to.deep.equal({ bar: 0 })
done()
})
})

it('set a config key with invalid json', (done) => {
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'config', 'foo', '{"bar: 0}', '--json'])
.run((err, stdout, exitcode) => {
const expected = 'error\tinvalid JSON provided'
expect(stdout[0]).to.equal(expected)
expect(err).to.not.exist
expect(exitcode).to.equal(1)
done()
})
})

it('call config with no arguments', (done) => {
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'config'])
.run((err, stdout, exitcode) => {
const expected = "error\targument 'key' is required"
expect(stdout[0]).to.equal(expected)
expect(err).to.not.exist
expect(exitcode).to.equal(1)
done()
})
})
})

it('call config with no arguments', (done) => {
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'config'])
.run((err, stdout, exitcode) => {
const expected = "error\targument 'key' is required"
expect(stdout[0]).to.equal(expected)
expect(err).to.not.exist
expect(exitcode).to.equal(1)
done()
})
describe('replace', () => {
it('replace config with file', (done) => {
const filePath = 'test/test-data/otherconfig'
const expectedConfig = JSON.parse(fs.readFileSync(filePath, 'utf8'))

nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'config', 'replace', filePath])
.run((err, stdout, exitcode) => {
expect(err).to.not.exist
expect(exitcode).to.equal(0)

expect(updatedConfig()).to.deep.equal(expectedConfig)
done()
})
})

after(() => {
restoreConfig()
})
})
})

describe('api running', () => {})
})

0 comments on commit fdcc5af

Please sign in to comment.