-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcli.js
executable file
·90 lines (79 loc) · 2.29 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
87
88
89
90
#!/usr/bin/env node
const clipboardy = require('clipboardy');
const program = require('commander')
const Table = require('cli-table');
const qrcodeGen = require('qrcode-terminal');
const TwoFA = require('./TwoFA');
const twofa = new TwoFA();
const __stdout = message => {
console.log(message);
};
__stdout.error = message =>
__stdout(`\x1b[31m[1m${message}`);
__stdout.success = message =>
__stdout(`\x1b[32m[1m${message}`);
__stdout.code = code =>
__stdout.success(
`The code for "${code.service} - ${code.label}" is: ${code.code}`
);
program
.command('add <service>')
.description('Add a new service to generate authentication code')
.option('-i, --image [image]', 'Image instead screen capture')
.action((service, cmd) => {
twofa.add(service, {
imagePath: cmd.image,
})
.then(code => {
__stdout.success(`The "${service}" added with success!`);
clipboardy.writeSync(code.code);
__stdout.code(code);
})
.catch(error => __stdout.error(error));
});
program
.command('del <service>')
.description('Delete a service registered')
.action(service => {
twofa.del(service)
.then(() =>
__stdout.success(`The "${service}" deleted with success!`)
)
.catch(error => __stdout.error(error));
});
program
.command('gen [service]')
.description('Generate authentication code')
.action(service => {
twofa.gen(service)
.then(code => {
if (service) {
clipboardy.writeSync(code.code);
return __stdout.code(code);
}
if (!Array.isArray(code)) {
return __stdout.error('Something is wrong.');
}
const table = new Table({
head: ['SERVICE', 'LABEL', 'CODE'],
});
code.map(c => table.push([c.service, c.label, c.code]));
console.log('Listing all services and your codes.');
return __stdout.success(
table.toString()
);
})
.catch(e => __stdout.error(e));
});
program
.command('qrcode <service>')
.description('Generate qrcode from a service')
.action(service => {
twofa.qrcode(service)
.then(qrcode => {
__stdout.success(`Show QRCode for "${service}".\n`);
console.log(qrcode);
})
.catch(e => __stdout.error(e));
});
program.parse(process.argv);