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' )
@@ -26,11 +30,91 @@ module.exports = Command.extend({
26
30
} ,
27
31
28
32
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'
32
35
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
+ }
34
118
} )
35
119
}
36
120
} )
0 commit comments