ipfs
Version:
JavaScript implementation of the IPFS specification
64 lines (54 loc) • 1.31 kB
JavaScript
const print = require('../utils').print
module.exports = {
command: 'config <key> [value]',
description: 'Get and set IPFS config values',
builder (yargs) {
return yargs
.commandDir('config')
.options({
bool: {
type: 'boolean',
default: false,
global: false
},
json: {
type: 'boolean',
default: false,
global: false
}
})
},
handler (argv) {
argv.resolve((async () => {
if (argv._handled) {
return
}
argv._handled = true
const { bool, json, key, getIpfs } = argv
const ipfs = await getIpfs()
let value = argv.value
if (!value) {
// Get the value of a given key
value = await ipfs.config.get(key)
if (typeof value === 'object') {
print(JSON.stringify(value, null, 2))
} else {
print(value)
}
} else {
// Set the new value of a given key
if (bool) {
value = (value === 'true')
} else if (json) {
try {
value = JSON.parse(value)
} catch (err) {
throw new Error('invalid JSON provided')
}
}
await ipfs.config.set(key, value)
}
})())
}
}