-
-
Notifications
You must be signed in to change notification settings - Fork 813
/
Copy pathcli
executable file
·234 lines (215 loc) · 5.51 KB
/
cli
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#!/usr/bin/env node
/**
* @license Apache-2.0
*
* Copyright (c) 2021 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
// MODULES //
var createReadStream = require( 'fs' ).createReadStream;
var resolve = require( 'path' ).resolve;
var join = require( 'path' ).join;
var exec = require( 'child_process' ).execSync;
var readline = require( 'readline' );
var readFileSync = require( '@stdlib/fs/read-file' ).sync;
var stdin = require( '@stdlib/streams/node/stdin' );
var stdout = require( '@stdlib/streams/node/stdout' );
var EOL = require( '@stdlib/regexp/eol' ).REGEXP;
var CLI = require( '@stdlib/cli/ctor' );
var ENV = require( '@stdlib/process/env' );
var cwd = require( '@stdlib/process/cwd' );
var create = require( './../lib' );
// MAIN //
/**
* Main execution sequence.
*
* @private
* @throws {Error} must either 1) provide a username via a command-line option, 2) set a GITHUB_USERNAME environment variable, or 3) have git installed and run this command in a .git repository
* @returns {void}
*/
function main() {
var username;
var flags;
var args;
var opts;
var cli;
var rl;
// Create a command-line interface:
cli = new CLI({
'pkg': require( './../package.json' ),
'options': require( './../etc/cli_opts.json' ),
'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), {
'encoding': 'utf8'
})
});
// Get any provided command-line options:
flags = cli.flags();
if ( flags.help || flags.version ) {
return;
}
if ( flags.lscopes ) {
createReadStream( join( __dirname, 'scopes.txt' ) )
.pipe( process.stdout )
.on( 'close', onClose );
return;
}
// Get any provided command-line arguments:
args = cli.args();
opts = {};
if ( flags.username ) {
username = args.username;
}
else if ( ENV.GITHUB_USERNAME ) {
username = ENV.GITHUB_USERNAME;
}
else {
// Attempt to infer from `git config`...
try {
username = exec( 'git config user.name', {
'cwd': cwd()
});
username = username.toString().replace( EOL, '' );
} catch ( err ) {
throw new Error( 'invalid invocation: '+err.message+'. Must either 1) provide a username via a command-line option, 2) set a GITHUB_USERNAME environment variable, or 3) have git installed and run this command in a .git repository.' );
}
}
if ( username ) {
opts.username = username;
}
if ( flags.otp ) {
opts.otp = flags.otp;
}
if ( flags.useragent ) {
opts.useragent = flags.useragent;
}
if ( flags.scopes ) {
opts.scopes = flags.scopes.split( ',' );
}
if ( flags.note ) {
opts.note = flags.note;
}
if ( flags.note_url ) {
opts.note_url = flags.note_url;
}
if ( flags.client_id ) {
opts.client_id = flags.client_id;
}
if ( flags.client_secret ) {
opts.client_secret = flags.client_secret;
}
if ( flags.fingerprint ) {
opts.fingerprint = flags.fingerprint;
}
if ( flags.password ) {
opts.password = flags.password;
create( opts, clbk );
}
else if ( ENV.GITHUB_PASSWORD ) {
opts.password = ENV.GITHUB_PASSWORD;
create( opts, clbk );
}
else {
// Prompt for a password:
rl = readline.createInterface({
'input': stdin,
'output': stdout
});
rl.on( 'close', onCloseRL );
rl.on( 'SIGINT', onSIGINT );
console.log( '\nPlease enter your GitHub credentials: \n' ); // eslint-disable-line no-console
rl.question( 'Enter GitHub password: ', onPassword );
}
/**
* Callback invoked upon writing a file to `stdout`.
*
* @private
*/
function onClose() {
cli.close( 0 );
}
/**
* Callback invoked upon receiving a password.
*
* @private
* @param {string} answer - user input
* @returns {void}
*/
function onPassword( answer ) {
opts.password = answer;
create( opts, clbk );
}
/**
* Callback invoked once a readline interface closes.
*
* @private
*/
function onCloseRL() {
create( opts, clbk );
}
/**
* Callback invoked upon receiving a one-time password.
*
* @private
* @param {string} answer - user input
* @returns {void}
*/
function onOTP( answer ) {
opts.otp = answer;
create( opts, clbk );
}
/**
* Callback invoked upon creating a token.
*
* @private
* @param {(Error|null)} error - error object
* @param {Object} results - query results
* @param {Object} info - rate limit info
* @returns {void}
*/
function clbk( error, results, info ) {
var re;
if ( info ) {
process.stderr.write( JSON.stringify( info )+'\n', 'utf8' );
}
if ( error ) {
re = /otp/ig;
if ( error.status === 401 && re.test( error.message ) ) {
// Prompt for multi-factor authentication:
rl = readline.createInterface({
'input': stdin,
'output': stdout
});
rl.on( 'close', onClose );
rl.on( 'SIGINT', onSIGINT );
rl.question( 'Enter one-time password: ', onOTP );
} else {
process.stderr.write( error.message+'\n', 'utf8' );
return cli.close( 1 );
}
}
process.stdout.write( JSON.stringify( results )+'\n', 'utf8' );
cli.close( 0 );
}
/**
* Callback invoked upon receiving a SIGINT (e.g., ctrl+C).
*
* @private
*/
function onSIGINT() {
console.error( '' ); // eslint-disable-line no-console
cli.close( 1 );
}
}
main();