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

Commit 263a70d

Browse files
committed
Add ipfs config edit to cli
1 parent 876ab2e commit 263a70d

File tree

2 files changed

+90
-5
lines changed

2 files changed

+90
-5
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
"lodash.get": "^4.0.0",
7979
"lodash.set": "^4.0.0",
8080
"peer-id": "^0.5.0",
81-
"ronin": "^0.3.11"
81+
"ronin": "^0.3.11",
82+
"temp": "^0.8.3"
8283
}
8384
}

src/cli/commands/config/edit.js

Lines changed: 88 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
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')
@@ -26,11 +30,91 @@ module.exports = Command.extend({
2630
},
2731

2832
run: (name) => {
29-
var node = new IPFS()
30-
node.version((err, version) => {
31-
if (err) { return log.error(err) }
33+
const node = new IPFS()
34+
const editor = process.env.EDITOR || 'vi'
3235

33-
console.log(version)
36+
function getConfig (next) {
37+
node.config.show((err, config) => {
38+
if (err) {
39+
log.error(err)
40+
next(new Error('failed to get the config'))
41+
}
42+
43+
next(null, config)
44+
})
45+
}
46+
47+
function saveTempConfig (config, next) {
48+
temp.open('ipfs-config', (err, info) => {
49+
if (err) {
50+
log.error(err)
51+
next(new Error('failed to open the config'))
52+
}
53+
54+
fs.write(info.fd, JSON.stringify(config, null, 2))
55+
fs.close(info.fd, (err) => {
56+
if (err) {
57+
log.error(err)
58+
next(new Error('failed to open the config'))
59+
}
60+
})
61+
62+
next(null, info.path)
63+
})
64+
}
65+
66+
function openEditor (path, next) {
67+
const child = spawn(editor, [path], {
68+
stdio: 'inherit'
69+
})
70+
71+
child.on('exit', (err, code) => {
72+
if (err) {
73+
log.error(err)
74+
throw new Error('error on the editor')
75+
}
76+
77+
next(null, path)
78+
})
79+
}
80+
81+
function readTempConfig (path, next) {
82+
fs.readFile(path, 'utf8', (err, data) => {
83+
if (err) {
84+
log.error(err)
85+
next(new Error('failed to get the updated config'))
86+
}
87+
88+
try {
89+
next(null, JSON.parse(data))
90+
} catch (err) {
91+
log.error(err)
92+
next(new Error(`failed to parse the updated config "${err.message}"`))
93+
}
94+
})
95+
}
96+
97+
function saveConfig (config, next) {
98+
node.config.replace(config, (err) => {
99+
if (err) {
100+
log.error(err)
101+
next(new Error('failed to save the config'))
102+
}
103+
104+
next()
105+
})
106+
}
107+
108+
async.waterfall([
109+
getConfig,
110+
saveTempConfig,
111+
openEditor,
112+
readTempConfig,
113+
saveConfig
114+
], (err) => {
115+
if (err) {
116+
throw err
117+
}
34118
})
35119
}
36120
})

0 commit comments

Comments
 (0)