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

Commit e8dd778

Browse files
committed
Merge pull request #77 from ipfs/feature/config-edit
Add ipfs config edit to cli
2 parents 015ab10 + d14df9b commit e8dd778

File tree

2 files changed

+95
-22
lines changed

2 files changed

+95
-22
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
"lodash.get": "^4.0.0",
8282
"lodash.set": "^4.0.0",
8383
"peer-id": "^0.5.0",
84-
"ronin": "^0.3.11"
84+
"ronin": "^0.3.11",
85+
"temp": "^0.8.3"
8586
}
8687
}

src/cli/commands/config/edit.js

Lines changed: 93 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,108 @@
11
'use strict'
22

33
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')
48
const IPFS = require('../../../ipfs-core')
59
const debug = require('debug')
610
const log = debug('cli:version')
711
log.error = debug('cli:version:error')
812

913
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',
2715

2816
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+
}
3295

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+
}
34106
})
35107
}
36108
})

0 commit comments

Comments
 (0)