2
2
3
3
'use strict' ;
4
4
5
+ const blindfold = require ( 'blindfold' ) ;
5
6
const editor = require ( 'editor' ) ;
6
7
const { tmpdir, homedir} = require ( 'os' ) ;
7
8
const fs = require ( 'fs' ) ;
8
9
const storj = require ( 'storj-lib' ) ;
9
10
const path = require ( 'path' ) ;
10
11
const mkdirp = require ( 'mkdirp' ) ;
11
- const config = require ( '../lib/config/daemon ' ) ;
12
+ const stripJsonComments = require ( 'strip-json-comments ' ) ;
12
13
const storjshare_create = require ( 'commander' ) ;
13
14
15
+ const defaultConfig = JSON . parse ( stripJsonComments ( fs . readFileSync (
16
+ path . join ( __dirname , '../example/farmer.config.json' )
17
+ ) . toString ( ) ) ) ;
18
+
14
19
storjshare_create
15
20
. description ( 'generates a new share configuration' )
16
- . option ( '-a, --sjcx <addr>' , 'specify the sjcx address (required)' )
17
- . option ( '-s, --storage <path>' , 'specify the storage path' )
18
- . option ( '-l, --logfile <path>' , 'specify the logfile path' )
19
- . option ( '-k, --privkey <privkey>' , 'specify the private key' )
21
+ . option ( '--sjcx <addr>' , 'specify the sjcx address (required)' )
22
+ . option ( '--key <privkey>' , 'specify the private key' )
23
+ . option ( '--storage <path>' , 'specify the storage path' )
24
+ . option ( '--size <maxsize>' , 'specify share size (ex: 10GB, 1TB)' )
25
+ . option ( '--rpcport <port>' , 'specify the rpc port number' )
26
+ . option ( '--rpcaddress <addr>' , 'specify the rpc address' )
27
+ . option ( '--maxtunnels <tunnels>' , 'specify the max tunnels' )
28
+ . option ( '--tunnelportmin <port>' , 'specify min gateway port' )
29
+ . option ( '--tunnelportmax <port>' , 'specify max gateway port' )
30
+ . option ( '--manualforwarding' , 'do not use nat traversal strategies' )
31
+ . option ( '--logfile <path>' , 'specify the logfile path' )
32
+ . option ( '--noedit' , 'do not open generated config in editor' )
20
33
. option ( '-o, --outfile <writepath>' , 'write config to path' )
21
- . option ( '-n, --noedit' , 'do not open generated config in editor' )
22
34
. parse ( process . argv ) ;
23
35
24
36
if ( ! storjshare_create . sjcx ) {
25
37
console . error ( '\n no payment address was given, try --help' ) ;
26
38
process . exit ( 1 ) ;
27
39
}
28
40
29
- if ( ! storjshare_create . privkey ) {
30
- storjshare_create . privkey = storj . KeyPair ( ) . getPrivateKey ( ) ;
41
+ if ( ! storjshare_create . key ) {
42
+ storjshare_create . key = storj . KeyPair ( ) . getPrivateKey ( ) ;
31
43
}
32
44
33
45
if ( ! storjshare_create . storage ) {
34
46
storjshare_create . storage = path . join (
35
47
homedir ( ) ,
36
48
'.config/storjshare/shares' ,
37
- storj . KeyPair ( storjshare_create . privkey ) . getNodeID ( )
49
+ storj . KeyPair ( storjshare_create . key ) . getNodeID ( )
38
50
) ;
39
51
mkdirp . sync ( storjshare_create . storage ) ;
40
52
}
@@ -43,33 +55,72 @@ if (!storjshare_create.logfile) {
43
55
storjshare_create . logfile = path . join (
44
56
homedir ( ) ,
45
57
'.config/storjshare/logs' ,
46
- storj . KeyPair ( storjshare_create . privkey ) . getNodeID ( ) + '.log'
58
+ storj . KeyPair ( storjshare_create . key ) . getNodeID ( ) + '.log'
47
59
) ;
48
60
}
49
61
50
62
if ( ! storjshare_create . outfile ) {
51
63
storjshare_create . outfile = path . join (
52
64
tmpdir ( ) ,
53
- storj . KeyPair ( storjshare_create . privkey ) . getNodeID ( ) + '.json'
65
+ storj . KeyPair ( storjshare_create . key ) . getNodeID ( ) + '.json'
54
66
) ;
55
67
}
56
68
57
69
let exampleConfigPath = path . join ( __dirname , '../example/farmer.config.json' ) ;
58
70
let exampleConfigString = fs . readFileSync ( exampleConfigPath ) . toString ( ) ;
59
71
60
- function replaceEmptyConfig ( prop , value ) {
61
- value = value . split ( '\\' ) . join ( '\\\\' ) ; // NB: Hack windows paths into JSON
72
+ function getDefaultConfigValue ( prop ) {
73
+ return {
74
+ value : blindfold ( defaultConfig , prop ) ,
75
+ type : typeof blindfold ( defaultConfig , prop )
76
+ } ;
77
+ }
78
+
79
+ function replaceDefaultConfigValue ( prop , value ) {
80
+ let defaultValue = getDefaultConfigValue ( prop ) ;
81
+
82
+ function toStringReplace ( prop , value , type ) {
83
+ switch ( type ) {
84
+ case 'string' :
85
+ value = value . split ( '\\' ) . join ( '\\\\' ) ; // NB: Hack windows paths
86
+ return `"${ prop } ": "${ value } "` ;
87
+ case 'boolean' :
88
+ case 'number' :
89
+ return `"${ prop } ": ${ value } ` ;
90
+ default :
91
+ return '' ;
92
+ }
93
+ }
94
+
95
+ prop = prop . split ( '.' ) . pop ( ) ;
62
96
exampleConfigString = exampleConfigString . replace (
63
- `" ${ prop } ": ""` ,
64
- `" ${ prop } ": " ${ value } "`
97
+ toStringReplace ( prop , defaultValue . value , defaultValue . type ) ,
98
+ toStringReplace ( prop , value , defaultValue . type )
65
99
) ;
66
100
}
67
101
68
- replaceEmptyConfig ( 'paymentAddress' , storjshare_create . sjcx ) ;
69
- replaceEmptyConfig ( 'networkPrivateKey' , storjshare_create . privkey ) ;
70
- replaceEmptyConfig ( 'storagePath' , path . normalize ( storjshare_create . storage ) ) ;
71
- replaceEmptyConfig ( 'loggerOutputFile' ,
72
- path . normalize ( storjshare_create . logfile ) ) ;
102
+ replaceDefaultConfigValue ( 'paymentAddress' , storjshare_create . sjcx ) ;
103
+ replaceDefaultConfigValue ( 'networkPrivateKey' , storjshare_create . key ) ;
104
+ replaceDefaultConfigValue ( 'storagePath' ,
105
+ path . normalize ( storjshare_create . storage ) ) ;
106
+ replaceDefaultConfigValue ( 'loggerOutputFile' ,
107
+ path . normalize ( storjshare_create . logfile ) ) ;
108
+
109
+ const optionalReplacements = [
110
+ { option : storjshare_create . size , name : 'storageAllocation' } ,
111
+ { option : storjshare_create . rpcaddress , name : 'rpcAddress' } ,
112
+ { option : storjshare_create . rpcport , name : 'rpcPort' } ,
113
+ { option : storjshare_create . maxtunnels , name : 'maxTunnels' } ,
114
+ { option : storjshare_create . tunnelportmin , name : 'tunnelGatewayRange.min' } ,
115
+ { option : storjshare_create . tunnelportmax , name : 'tunnelGatewayRange.max' } ,
116
+ { option : storjshare_create . manualforwarding , name : 'doNotTraverseNat' }
117
+ ] ;
118
+
119
+ optionalReplacements . forEach ( ( repl ) => {
120
+ if ( repl . option ) {
121
+ replaceDefaultConfigValue ( repl . name , repl . option ) ;
122
+ }
123
+ } ) ;
73
124
74
125
let outfile = path . isAbsolute ( storjshare_create . outfile ) ?
75
126
path . normalize ( storjshare_create . outfile ) :
0 commit comments