fix: error entering CLI command
Massimo Melina committed
Jun 29, 2022 at 10:32 UTC
78861a56ca7eff01553c34ee253750e17a005aaa
1 file changed
+60
-35
server/src/commands.ts
+60
-35
@@ -1,17 +1,17 @@
1
import { addAccount, getAccount, updateAccount } from './perm'
2
import { getConfigDefinition, setConfig } from './config'
3
+import _ from 'lodash'
4
5
console.log(`HINT: type "help" for help`)
6
require('readline').createInterface({ input: process.stdin }).on('line', (line: string) => {
6
- const [command, ...params] = line.split(/ +/)
7
- const fun = (commands as any)[command]
8
- if (!fun)
9
- return console.error("cannot understand entered command")
10
- if (fun.length > params.length) {
11
- const [args] = /\((.+)\)\s*\{/.exec(fun)!
12
- return console.error("insufficient parameters, expected: " + args)
13
- }
14
- fun(...params).then(() =>console.log("command executed"),
7
+ if (!line) return
8
+ const [name, ...params] = line.split(/ +/)
9
+ const cmd = (commands as any)[name]
10
+ if (!cmd)
11
+ return console.error("cannot understand entered command, try 'help'")
12
+ if (cmd.cb.length > params.length)
13
+ return console.error("insufficient parameters, expected: " + cmd.params)
14
+ cmd.cb(...params).then(() =>console.log("command executed"),
15
(err: any) => {
16
if (typeof err === 'string')
17
console.error("command failed:", err)
@@ -20,36 +20,61 @@ require('readline').createInterface({ input: process.stdin }).on('line', (line:
20
})
21
})
22
23
+function getFunctionArgs(fun: Function) {
24
+ const args = /\((.*)\)\s*\{/.exec(fun.toString())![1]
25
+ return args && args.split(',').map(arg => {
26
+ const [name,def] = arg.split('=')
27
+ return `<${name.trim()}>${def ? '='+def.trim() : ''}`
28
+ }).join(' ')
29
+}
30
+
31
const commands = {
24
- async help() {
25
- console.log("supported commands:", ...Object.keys(commands).map(x => '\n - ' + x))
32
+ help: {
33
+ params: '',
34
+ async cb() {
35
+ console.log("supported commands:",
36
+ ..._.map(commands, ({ params }, name) =>
37
+ '\n - ' + name + ' ' + params))
38
+ }
39
},
27
- async 'create-admin'(password: string, username='admin') {
28
- if (getAccount(username))
29
- throw `user ${username} already exists`
30
- const acc = addAccount(username, { admin: true })
31
- await updateAccount(acc!, acc => {
32
- acc.password = password
33
- })
40
+ 'create-admin': {
41
+ params: '<password> [<username>=admin]',
42
+ async cb(password: string, username='admin') {
43
+ if (getAccount(username))
44
+ throw `user ${username} already exists`
45
+ const acc = addAccount(username, { admin: true })
46
+ await updateAccount(acc!, acc => {
47
+ acc.password = password
48
+ })
49
+ }
50
},
35
- async 'change-password'(user: string, password: string) {
36
- const acc = getAccount(user)
37
- if (!acc)
38
- throw "user doesn't exist"
39
- await updateAccount(acc!, acc => {
40
- acc.password = password
41
- })
51
+ 'change-password': {
52
+ params: '<user> <password>',
53
+ async cb(user: string, password: string) {
54
+ const acc = getAccount(user)
55
+ if (!acc)
56
+ throw "user doesn't exist"
57
+ await updateAccount(acc!, acc => {
58
+ acc.password = password
59
+ })
60
+ }
61
},
43
- async config(key: string, value: string) {
44
- const conf = getConfigDefinition(key)
45
- if (!conf)
46
- throw "specified key doesn't exist"
47
- let v: any = value
48
- try { v = JSON.parse(v) }
49
- catch {}
50
- setConfig({ [key]: v })
62
+ config: {
63
+ params: '<key> <value>',
64
+ async cb(key: string, value: string) {
65
+ const conf = getConfigDefinition(key)
66
+ if (!conf)
67
+ throw "specified key doesn't exist"
68
+ let v: any = value
69
+ try { v = JSON.parse(v) }
70
+ catch {}
71
+ setConfig({ [key]: v })
72
+ }
73
},
52
- async quit() {
53
- process.exit(0)
74
+ quit: {
75
+ params: '',
76
+ async cb() {
77
+ process.exit(0)
78
+ }
79
}
80
}