-
Notifications
You must be signed in to change notification settings - Fork 0
/
getcli.js
185 lines (169 loc) · 5.97 KB
/
getcli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
'use strict';
const boolean = require( 'boolean' );
const dashdash = require( 'dashdash' );
const path = require( 'path' );
module.exports = function() {
dashdash.addOptionType( {
name: 'commaSeparated',
takesArg: true,
helpArg: 'ARG[,ARG]',
array: true,
arrayFlatten: true,
parseArg: function parseCommaSeparated( option, optstr, arg ) {
return arg.split( ',' );
}
} );
dashdash.addOptionType( {
name: 'smartBool',
takesArg: true,
helpArg: '[true|false]',
parseArg: function parseSmartBool( option, optstr, arg ) {
return boolean( typeof arg !== 'undefined' ? arg : true );
}
} );
const options = [ {
name: 'version',
type: 'bool',
help: 'Print version and exit.'
}, {
names: [ 'help', 'h' ],
type: 'bool',
help: 'Print this help and exit.'
}, {
names: [ 'verbose', 'v' ],
type: 'bool',
help: 'Enable verbose output.'
}, {
name: 'norequestlogging',
type: 'bool',
help: 'Disables request logging'
}, {
name: 'logformat',
env: 'EPICENTER_LOGFORMAT',
type: 'string',
help: 'Specify the format of request logging.',
helpArg: 'text|json',
default: 'text'
}, {
names: [ 'require', 'req', 'r' ],
env: 'EPICENTER_REQUIRES',
type: 'arrayOfString',
help: 'Specify requires. By default the following are tried: ./routes, ./handlers, ./api',
helpArg: 'DIR',
default: [ './routes', './handlers', './api', './processors' ]
}, {
name: 'canonical',
env: 'EPICENTER_CANONICAL',
type: 'string',
help: 'Specify a canonical base url for the server, eg: "https://foo.com"',
helpArg: 'BASE_URL'
}, {
name: 'name',
env: 'EPICENTER_NAME',
type: 'string',
help: 'Specify the service name for restify.',
helpArg: 'NAME',
default: null
}, {
name: 'httpport',
env: 'EPICENTER_HTTP_PORT',
type: 'positiveInteger',
help: 'Specify the HTTP port to listen on.',
helpArg: 'PORT',
default: 8000
}, {
name: 'httpsport',
env: 'EPICENTER_HTTPS_PORT',
type: 'positiveInteger',
help: 'Specify the HTTPS port to listen on.',
helpArg: 'PORT',
default: 4443
}, {
name: 'httpscert',
env: 'EPICENTER_HTTPS_CERT',
type: 'string',
help: 'Specify the path of the file containing the SSL certificate for the server.',
helpArg: 'PATH'
}, {
name: 'httpskey',
env: 'EPICENTER_HTTPS_KEY',
type: 'string',
help: 'Specify the path fo the file containing the SSL private key for the server.',
helpArg: 'PATH'
}, {
name: 'httpsciphers',
env: 'EPICENTER_HTTPS_CIPHERS',
type: 'string',
help: 'Specify the SSL ciphers.',
helpArg: 'CIPHERS',
default: 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA'
}, {
name: 'httpsredirect',
env: 'EPICENTER_HTTPS_REDIRECT',
type: 'bool',
help: 'If specified, will redirect all connections on the unsecured port to the HTTPS port.',
default: false
}, {
names: [ 'corsorigin', 'co' ],
env: 'EPICENTER_CORS_ORIGINS',
type: 'commaSeparated',
help: 'Specify allowed CORS origins. If none are specified, the default * is used.',
helpArg: 'ORIGIN',
default: [ '*' ]
}, {
names: [ 'corsheader', 'ch' ],
env: 'EPICENTER_CORS_HEADERS',
type: 'commaSeparated',
help: 'Specify allowed CORS headers.',
helpArg: 'HEADER',
default: null
}, {
name: 'sentrydsn',
env: 'EPICENTER_SENTRY_DSN',
type: 'string',
help: 'Specify a DSN for Sentry (https://getsentry.com) error tracking.',
helpArg: 'DSN',
default: null
} ];
const scriptName = path.basename( require.main.filename );
const parser = dashdash.createParser( {
options: options
} );
let opts = {};
try {
opts = parser.parse( process.argv );
}
catch ( exception ) {
console.error( exception.message );
process.exit( 1 );
}
if ( opts.version ) {
const pkg = require( './package.json' );
console.log( scriptName + ' v' + pkg.version );
process.exit( 0 );
}
if ( opts.help ) {
const help = parser.help( {
includeEnv: true,
includeDefault: true,
maxCol: process.stdout.columns ? process.stdout.columns - 1 : 80
} ).trimRight();
console.log( 'usage: node ' + scriptName + ' [OPTIONS]\n' + 'options:\n' + help );
process.exit( 0 );
}
if ( opts.require[ 0 ].indexOf( ';' ) !== -1 ) {
opts.require = opts.require[ 0 ].split( ';' );
}
opts.requires = opts.require;
delete opts.require;
opts.cors = {
origins: opts.corsorigin,
allowedHeaders: opts.corsheader
};
delete opts.corsorigins;
delete opts.corsheader;
if ( !opts.canonical ) {
opts.canonical = opts.httpscert && opts.httpskey ? 'https://localhost:' + opts.httpsport : 'http://localhost:' + opts.httpport;
}
return opts;
};