This repository was archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add ipfs config edit to cli #77
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,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 | ||
} | ||
}) | ||
} | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
right ronin! :)