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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
package server;
import haxe.Json;
import haxe.extern.EitherType as Or;
import haxe.io.Path;
import js.Node.process;
import js.html.Console;
import js.node.Readline;
import sys.FileSystem;
import sys.io.File;
using StringTools;
private typedef CommandData = {
args:Array<String>,
desc:String
}
private enum abstract Command(String) from String {
var AddAdmin = "addAdmin";
var Replay = "replay";
var LogList = "logList";
var Exit = "exit";
}
class ConsoleInput {
final main:Main;
final commands:Map<Command, CommandData> = [
AddAdmin => {
args: ["name", "password"],
desc: "Adds channel admin"
},
Replay => {
args: ["name"],
desc: "Replay log file on server from user/logs/"
},
LogList => {
args: [],
desc: "Show log list from user/logs/"
},
Exit => {
args: [],
desc: "Exit process"
}
];
public function new(main:Main) {
this.main = main;
}
public function initConsoleInput():Void {
final rl = Readline.createInterface({
input: process.stdin,
output: process.stdout,
completer: onCompletion
});
haxe.Log.trace = (msg, ?pos) -> {
Readline.clearLine(process.stdout, 0);
Readline.cursorTo(process.stdout, 0, null);
Console.log(msg);
rl.prompt(true);
};
rl.prompt();
rl.on("line", line -> {
parseLine(line);
rl.prompt();
});
// rl.on("close", exit);
}
function onCompletion(line:String):Array<Or<Array<String>, String>> {
final commands:Array<String> = [
for (item in commands.keys()) '/$item '
];
final matches = commands.filter(item -> item.startsWith(line));
if (matches.length > 0) return [matches, line];
return [commands, line];
}
function parseLine(line:String):Void {
if (line.fastCodeAt(0) != "/".code || line.length < 2) {
printHelp(line);
return;
}
final args = line.trim().split(" ");
final command:Command = args.shift().substr(1);
if (commands[command] == null) {
printHelp(line);
return;
}
if (!isValidArgs(command, args)) return;
switch (command) {
case AddAdmin:
final name = args[0];
final password = args[1];
if (main.badNickName(name)) {
final error = Lang.get("usernameError")
.replace("$MAX", '${main.config.maxLoginLength}');
trace(error);
return;
}
main.addAdmin(name, password);
case Replay:
Utils.ensureDir(main.logsDir);
final name = args[0];
final path = Path.normalize('${main.logsDir}/$name.json');
if (!FileSystem.exists(path)) {
trace('File "$path" not found');
return;
}
final text = File.getContent(path);
final events:Array<ServerEvent> = Json.parse(text);
main.replayLog(events);
case LogList:
Utils.ensureDir(main.logsDir);
final names = FileSystem.readDirectory(main.logsDir)
.filter(s -> s.endsWith(".json"));
for (name in names) {
trace(Path.withoutExtension(name));
}
case Exit:
main.exit();
}
}
function isValidArgs(command:Command, args:Array<String>):Bool {
final len = args.length;
final actual = commands[command].args.length;
if (len != actual) {
trace('Wrong count of arguments for command "$command" ($len instead of $actual)');
return false;
}
return true;
}
function printHelp(line:String):Void {
var maxLength = 0;
for (name => data in commands) {
final len = '/$name ${data.args.join(" ")}'.length;
if (maxLength < len) maxLength = len;
}
final list:Array<String> = [];
for (name => data in commands) {
final args = data.args.join(" ");
final item = '/$name $args'.rpad(" ", maxLength);
list.push('$item | ${data.desc}');
}
final desc = list.join("\n");
trace('Unknown command "$line". List:\n$desc');
}
}
|