-
Notifications
You must be signed in to change notification settings - Fork 2
/
debug.js
100 lines (91 loc) · 2.93 KB
/
debug.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
91
92
93
94
95
96
97
98
99
100
/**
* Entry point of the program.
*
* Authors : Corentin Forler, Pierre Sibut-Bourde, 2021.
*/
const { RedisClient } = require('./redis-client');
async function main() {
if (process.argv.length <= 2) {
console.error([
'Usage: debug-edit-db <command> <...args>',
'Available commands:',
'• read all -- display all the available ids',
'• read used -- display the used ids',
'• fetch all -- refresh all the ids',
'• clear used -- empty the used ids list',
'• push used <id> -- add an id to the used ids list',
'• pop used <id> -- remove an id from the used ids list',
'',
'Database engine: redis',
].join('\n'));
return;
}
const redisClient = await RedisClient();
const [$0, $1, command, ...args] = process.argv;
if (command === 'read') {
if (args[0] === 'all') {
console.log(JSON.parse(await redisClient.get('all-ids')));
} else if (args[0] === 'used') {
console.log(JSON.parse(await redisClient.get('used-ids')));
} else {
invalidArg0('expected: `all` | `used`');
}
} else if (command === 'fetch') {
if (args[0] === 'all') {
const { mainRedis } = require('./fetch-ids');
await mainRedis();
} else {
invalidArg0('expected: `all`');
}
} else if (command === 'clear') {
if (args[0] === 'used') {
await redisClient.set('used-ids', '[]');
} else {
invalidArg0('expected: `used`, cannot clear `all`');
}
} else if (command === 'push') {
if (args[0] === 'used') {
const id = +args[1];
if (!Number.isNaN(id)) {
const list = JSON.parse(await redisClient.get('used-ids')) || [];
list.push(id);
await redisClient.set('used-ids', JSON.stringify(list));
} else {
invalidArg('id', 'expected: number');
}
} else {
invalidArg0('expected: `used`, cannot push elsewhere');
}
} else if (command === 'pop') {
if (args[0] === 'used') {
const id = +args[1];
if (!Number.isNaN(id)) {
const list = JSON.parse(await redisClient.get('used-ids')) || [];
const i = list.indexOf(id);
if (i >= 0) {
list.splice(i, 1);
await redisClient.set('used-ids', JSON.stringify(list));
} else {
console.error(`id ${id} is not used`);
}
} else {
invalidArg('id', 'expected: number');
}
} else {
invalidArg0('expected: `used`, cannot pop elsewhere');
}
}
await redisClient.quit();
function invalidArg0(reason = '(no reason specified)') {
console.error(`error: invalid argument ${args[0]} for command ${command}: ${reason}`);
process.exit(2);
}
function invalidArg(name, reason = '(no reason specified)') {
console.error(`error: invalid value for argument <${name}> for command ${command}: ${reason}`);
process.exit(3);
}
}
main().catch(err => {
console.error('ERROR');
console.error(err);
});