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

Add ipfs config edit to cli #77

Merged
merged 1 commit into from
Mar 4, 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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
"lodash.get": "^4.0.0",
"lodash.set": "^4.0.0",
"peer-id": "^0.5.0",
"ronin": "^0.3.11"
"ronin": "^0.3.11",
"temp": "^0.8.3"
}
}
114 changes: 93 additions & 21 deletions src/cli/commands/config/edit.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,108 @@
'use strict'

const Command = require('ronin').Command
const spawn = require('child_process').spawn
const fs = require('fs')
const temp = require('temp')
const async = require('async')
const IPFS = require('../../../ipfs-core')
const debug = require('debug')
const log = debug('cli:version')
log.error = debug('cli:version:error')

module.exports = Command.extend({
desc: 'Shows IPFS version information',

options: {
number: {
alias: 'n',
type: 'boolean',
default: false
},
commit: {
type: 'boolean',
default: false
},
repo: {
type: 'boolean',
default: false
}
},
desc: 'Opens the config file for editing in $EDITOR',

run: (name) => {
var node = new IPFS()
node.version((err, version) => {
if (err) { return log.error(err) }
const node = new IPFS()
const editor = process.env.EDITOR

if (!editor) {
throw new Error('ENV variable $EDITOR not set')
}

function getConfig (next) {
node.config.show((err, config) => {
if (err) {
log.error(err)
next(new Error('failed to get the config'))
}

next(null, config)
})
}

function saveTempConfig (config, next) {
temp.open('ipfs-config', (err, info) => {
if (err) {
log.error(err)
next(new Error('failed to open the config'))
}

fs.write(info.fd, JSON.stringify(config, null, 2))
fs.close(info.fd, (err) => {
if (err) {
log.error(err)
next(new Error('failed to open the config'))
}
})

next(null, info.path)
})
}

function openEditor (path, next) {
const child = spawn(editor, [path], {
stdio: 'inherit'
})

child.on('exit', (err, code) => {
if (err) {
log.error(err)
throw new Error('error on the editor')
}

next(null, path)
})
}

function readTempConfig (path, next) {
fs.readFile(path, 'utf8', (err, data) => {
if (err) {
log.error(err)
next(new Error('failed to get the updated config'))
}

try {
next(null, JSON.parse(data))
} catch (err) {
log.error(err)
next(new Error(`failed to parse the updated config "${err.message}"`))
}
})
}

function saveConfig (config, next) {
node.config.replace(config, (err) => {
if (err) {
log.error(err)
next(new Error('failed to save the config'))
}

next()
})
}

console.log(version)
async.waterfall([
getConfig,
saveTempConfig,
openEditor,
readTempConfig,
saveConfig
], (err) => {
if (err) {
throw err
Copy link
Member

Choose a reason for hiding this comment

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

throw to where? this will print a stack trace to the user, better print a proper error log

Copy link
Member Author

Choose a reason for hiding this comment

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

ronin catches it and prints it properly:
screen shot 2016-03-04 at 18 39 31

Copy link
Member

Choose a reason for hiding this comment

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

right ronin! :)

}
})
}
})