1+
2+ var commander = require ( 'commander' ) ;
3+ var package = require ( '../../package.json' ) ;
4+ var output = require ( '../output' ) ;
5+ var config = require ( "../configuration" ) ;
6+ var querystring = require ( 'querystring' ) ;
7+
8+ commander
9+ . version ( package . version )
10+ . option ( '-h, --host <host>' )
11+ . parse ( process . argv ) ;
12+
13+ var host = commander . host ;
14+ var port = 443 ;
15+ var scheme = 'https' ;
16+
17+ var valid = host && config . clientId && config . clientSecret && config . token ;
18+ if ( valid ) {
19+ var https = require ( scheme ) ;
20+
21+ var postData = querystring . stringify ( {
22+ client_id : config . clientId ,
23+ client_secret : config . clientSecret ,
24+ code : config . token
25+ } ) ;
26+
27+ var options = {
28+ protocol : scheme + ':' ,
29+ hostname : host ,
30+ port : port ,
31+ path : '/login/oauth/access_token' ,
32+ method : 'POST' ,
33+ headers : {
34+ 'Content-Type' : 'application/x-www-form-urlencoded' ,
35+ 'Content-Length' : postData . length
36+ }
37+ } ;
38+
39+ var req = https . request ( options , function ( res ) {
40+ var success = res . statusCode == 200 ;
41+
42+ if ( ! success ) {
43+ output . error ( res . statusCode ) ;
44+ } else {
45+ res . on ( 'data' , function ( d ) {
46+ output . custom ( "success" , d , true ) ;
47+ } ) ;
48+
49+ res . on ( 'end' , function ( d ) {
50+ process . exit ( ) ;
51+ } ) ;
52+ }
53+ } ) ;
54+
55+ req . on ( 'error' , function ( error ) {
56+ output . error ( error ) ;
57+ } ) ;
58+
59+ req . write ( postData ) ;
60+
61+ req . end ( ) ;
62+ }
63+ else {
64+ commander . help ( ) ;
65+ process . exit ( - 1 ) ;
66+ }
0 commit comments