forked from LiJinyao/BIT-campus-network-CLI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BIT.js
executable file
·88 lines (81 loc) · 2.49 KB
/
BIT.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
#!/usr/bin/env node
const {login, logout, usageStatus} = require('./account.js')
function printHelp() {
console.log(`
用法
登陆:
node index.js login your-account
注销:
node index.js logout your-account
`)
}
async function getPassword(prompt) {
process.stdout.write(prompt)
return new Promise((resolve, reject) => {
const stdin = process.stdin
stdin.setRawMode(true)
stdin.setEncoding('utf8')
var password = ''
stdin.on('data', function (ch) {
ch = ch.toString('utf8')
switch (ch) {
case "\n":
case "\r":
case "\u0004":
// They've finished typing their password
process.stdout.write('\n')
stdin.setRawMode(false)
stdin.pause()
resolve(password)
break
case "\u0003":
// Ctrl-C
process.stdout.write('\n')
stdin.setRawMode(false)
stdin.pause()
process.exit(0)
break
default:
// More passsword characters
process.stdout.write('*')
password += ch
break
}
})
})
}
async function exec(args) {
if(args.length !== 4) {
printHelp()
}
switch (args[2]) {
case 'login':
const password = await getPassword('Password: ')
const loginResponse = await login(args[3], password)
if(loginResponse.suc_msg === 'login_ok') {
console.log(`login successfully`)
let usage = await usageStatus(loginResponse)
for (key in usage) {
console.log('\x1b[36m%s\x1b[0m', `${key.padEnd(9)}: ${usage[key]}`)
}
} else {
if(loginResponse.error_msg.length > 0) {
console.log(`login failed,${loginResponse.error_msg}`)
} else {
console.log(`login failed,${loginResponse.suc_msg}`)
}
}
break
case 'logout':
const logoutResponse = await logout(args[3])
if(logoutResponse.res === 'ok') {
console.log('logout successfully')
} else {
console.log(`logout failed, ${logoutResponse.res}`)
}
break
default:
break
}
}
exec(process.argv)