|
1 | 1 | 'use strict'
|
2 | 2 |
|
3 | 3 | const Command = require('ronin').Command
|
| 4 | +const spawn = require('child_process').spawn |
| 5 | +const fs = require('fs') |
| 6 | +const temp = require('temp') |
| 7 | +const async = require('async') |
4 | 8 | const IPFS = require('../../../ipfs-core')
|
5 | 9 | const debug = require('debug')
|
6 | 10 | const log = debug('cli:version')
|
7 | 11 | log.error = debug('cli:version:error')
|
8 | 12 |
|
9 | 13 | module.exports = Command.extend({
|
10 |
| - desc: 'Shows IPFS version information', |
11 |
| - |
12 |
| - options: { |
13 |
| - number: { |
14 |
| - alias: 'n', |
15 |
| - type: 'boolean', |
16 |
| - default: false |
17 |
| - }, |
18 |
| - commit: { |
19 |
| - type: 'boolean', |
20 |
| - default: false |
21 |
| - }, |
22 |
| - repo: { |
23 |
| - type: 'boolean', |
24 |
| - default: false |
25 |
| - } |
26 |
| - }, |
| 14 | + desc: 'Opens the config file for editing in $EDITOR', |
27 | 15 |
|
28 | 16 | run: (name) => {
|
29 |
| - var node = new IPFS() |
30 |
| - node.version((err, version) => { |
31 |
| - if (err) { return log.error(err) } |
| 17 | + const node = new IPFS() |
| 18 | + const editor = process.env.EDITOR |
| 19 | + |
| 20 | + if (!editor) { |
| 21 | + throw new Error('ENV variable $EDITOR not set') |
| 22 | + } |
| 23 | + |
| 24 | + function getConfig (next) { |
| 25 | + node.config.show((err, config) => { |
| 26 | + if (err) { |
| 27 | + log.error(err) |
| 28 | + next(new Error('failed to get the config')) |
| 29 | + } |
| 30 | + |
| 31 | + next(null, config) |
| 32 | + }) |
| 33 | + } |
| 34 | + |
| 35 | + function saveTempConfig (config, next) { |
| 36 | + temp.open('ipfs-config', (err, info) => { |
| 37 | + if (err) { |
| 38 | + log.error(err) |
| 39 | + next(new Error('failed to open the config')) |
| 40 | + } |
| 41 | + |
| 42 | + fs.write(info.fd, JSON.stringify(config, null, 2)) |
| 43 | + fs.close(info.fd, (err) => { |
| 44 | + if (err) { |
| 45 | + log.error(err) |
| 46 | + next(new Error('failed to open the config')) |
| 47 | + } |
| 48 | + }) |
| 49 | + |
| 50 | + next(null, info.path) |
| 51 | + }) |
| 52 | + } |
| 53 | + |
| 54 | + function openEditor (path, next) { |
| 55 | + const child = spawn(editor, [path], { |
| 56 | + stdio: 'inherit' |
| 57 | + }) |
| 58 | + |
| 59 | + child.on('exit', (err, code) => { |
| 60 | + if (err) { |
| 61 | + log.error(err) |
| 62 | + throw new Error('error on the editor') |
| 63 | + } |
| 64 | + |
| 65 | + next(null, path) |
| 66 | + }) |
| 67 | + } |
| 68 | + |
| 69 | + function readTempConfig (path, next) { |
| 70 | + fs.readFile(path, 'utf8', (err, data) => { |
| 71 | + if (err) { |
| 72 | + log.error(err) |
| 73 | + next(new Error('failed to get the updated config')) |
| 74 | + } |
| 75 | + |
| 76 | + try { |
| 77 | + next(null, JSON.parse(data)) |
| 78 | + } catch (err) { |
| 79 | + log.error(err) |
| 80 | + next(new Error(`failed to parse the updated config "${err.message}"`)) |
| 81 | + } |
| 82 | + }) |
| 83 | + } |
| 84 | + |
| 85 | + function saveConfig (config, next) { |
| 86 | + node.config.replace(config, (err) => { |
| 87 | + if (err) { |
| 88 | + log.error(err) |
| 89 | + next(new Error('failed to save the config')) |
| 90 | + } |
| 91 | + |
| 92 | + next() |
| 93 | + }) |
| 94 | + } |
32 | 95 |
|
33 |
| - console.log(version) |
| 96 | + async.waterfall([ |
| 97 | + getConfig, |
| 98 | + saveTempConfig, |
| 99 | + openEditor, |
| 100 | + readTempConfig, |
| 101 | + saveConfig |
| 102 | + ], (err) => { |
| 103 | + if (err) { |
| 104 | + throw err |
| 105 | + } |
34 | 106 | })
|
35 | 107 | }
|
36 | 108 | })
|
0 commit comments