This repository has been archived by the owner on Mar 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 299
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #307 from ipfs/config-api
interface-ipfs-core config API spec compliant
- Loading branch information
Showing
5 changed files
with
66 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,64 @@ | ||
'use strict' | ||
|
||
const argCommand = require('../cmd-helpers').argCommand | ||
const streamifier = require('streamifier') | ||
|
||
module.exports = (send) => { | ||
return { | ||
get: argCommand(send, 'config'), | ||
set (key, value, opts, cb) { | ||
if (typeof (opts) === 'function') { | ||
cb = opts | ||
get (key, callback) { | ||
if (typeof key === 'function') { | ||
callback = key | ||
key = undefined | ||
} | ||
|
||
if (!key) { | ||
return send('config/show', null, null, null, true, callback) | ||
} | ||
|
||
return send('config', key, null, null, (err, result) => { | ||
if (err) { | ||
return callback(err) | ||
} | ||
callback(null, result.Value) | ||
}) | ||
}, | ||
set (key, value, opts, callback) { | ||
if (typeof opts === 'function') { | ||
callback = opts | ||
opts = {} | ||
} | ||
if (typeof key !== 'string') { | ||
return callback(new Error('Invalid key type')) | ||
} | ||
|
||
if (typeof value !== 'object' && | ||
typeof value !== 'boolean' && | ||
typeof value !== 'string') { | ||
return callback(new Error('Invalid value type')) | ||
} | ||
|
||
if (typeof (value) === 'object') { | ||
if (typeof value === 'object') { | ||
value = JSON.stringify(value) | ||
opts = { json: true } | ||
} else if (typeof (value) === 'boolean') { | ||
} | ||
|
||
if (typeof value === 'boolean') { | ||
value = value.toString() | ||
opts = { bool: true } | ||
} | ||
|
||
return send('config', [key, value], opts, null, cb) | ||
}, | ||
show (cb) { | ||
return send('config/show', null, null, null, true, cb) | ||
return send('config', [key, value], opts, null, callback) | ||
}, | ||
replace (file, cb) { | ||
return send('config/replace', null, null, file, cb) | ||
replace (config, callback) { | ||
// Its a path | ||
if (typeof config === 'string') { | ||
return send('config/replace', null, null, config, callback) | ||
} | ||
|
||
// Its a config obj | ||
if (typeof config === 'object') { | ||
config = streamifier.createReadStream(new Buffer(JSON.stringify(config))) | ||
return send('config/replace', null, null, config, callback) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters