-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·86 lines (74 loc) · 2.63 KB
/
cli.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
#!/usr/bin/env node
/**
* gitlogin <https://github.com/tunnckoCore/gitlogin>
*
* Copyright (c) 2014 Charlike Mike Reagent, contributors.
* Released under the MIT license.
*/
'use strict';
var gitlogin = require('./index');
var meow = require('meow');
var cli = meow({
help: [
'Options',
' --help Show this help',
' --version Current version of package',
' -u | --username Your Github username, required',
' -p | --password Your Github password, required',
' -s | --scopes Scopes for github, not required',
' -n | --note Note for the app, not required',
' -t | --token-only If you want to show only token',
' -j | --json Output full JSON.stringified response',
'',
'Usage',
' gitlogin <username> <password> [scopes] [note]',
' gitlogin -p <password> -n [note] -u <username> -s [scopes]',
'',
'Examples',
' gitlogin tunnckoCore myPassword \'repo, gist, delete_repo\' \'some note for app\'',
' gitlogin tunnckoCore myPassword repo,gist,delete_repo \'some note for app\'',
' gitlogin tunnckoCore myPassword \'\' \'\' true false',
' gitlogin --username tunnckoCore --password myPassword',
' gitlogin --scopes repo,gist,delete_repo -u tunnckoCore -n myAwesomeApp -p myPassword',
' gitlogin -u tunnckoCore -p myPassword',
''
].join('\n')
});
var username = cli.input[0] || cli.flags.u || cli.flags.username;
var password = cli.input[1] || cli.flags.p || cli.flags.password;
var scopes = cli.input[2] || cli.flags.s || cli.flags.scopes;
var note = cli.input[3] || cli.flags.n || cli.flags.note;
var tokenOnly = cli.input[4] || cli.flags.t || cli.flags.tokenOnly;
var jsonOnly = cli.input[5] || cli.flags.j || cli.flags.json;
tokenOnly = !tokenOnly || tokenOnly === 'false' ? false : true;
jsonOnly = !jsonOnly || jsonOnly === 'false' ? false : true;
if (!username || !password) {
console.log('username and password is required');
process.exit(1);
}
var opts = {
username: username,
password: password,
scopes: scopes && scopes.split(','),
note: note
};
gitlogin(opts)
.then(function gitloginFulfilled(res) {
var json = JSON.parse(res);
if (jsonOnly) {
process.stdout.write(JSON.stringify(json, null, 2) + '\n');
return;
}
if (tokenOnly) {
process.stdout.write(json.token + '\n')
return;
}
var str = [
'Hi, ' + username + '! You\'ve successfully authenticated!',
'Your personal access token is ' + json.token + '\n'
].join('\n');
process.stdout.write(str);
})
.catch(function gitloginRejected(err) {
console.log(err);
})