-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCLI.java
More file actions
115 lines (107 loc) · 5.02 KB
/
Copy pathCLI.java
File metadata and controls
115 lines (107 loc) · 5.02 KB
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
package io.aether.cli;
import io.aether.api.common.CryptoLib;
import io.aether.api.common.ServerDescriptor;
import io.aether.cloud.client.ClientStateInMemory;
import io.aether.logger.Log;
import io.aether.logger.LogFilter;
import io.aether.utils.AString;
import io.aether.utils.CTypeI;
import io.aether.utils.HexUtils;
import io.aether.utils.RU;
import io.aether.utils.consoleCanonical.ConsoleMgrCanonical;
import io.aether.utils.flow.Flow;
import io.aether.utils.futures.ARFuture;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.UUID;
public class CLI {
public final CliApi api;
private final ARFuture<Object> resultFuture;
private final CliState cliState;
public CLI(String... aa) {
// Проверяем наличие флага подробного логирования
boolean verbose = java.util.Arrays.asList(aa).contains("--verbose") || java.util.Arrays.asList(aa).contains("-v");
// Создаем и настраиваем фильтр
LogFilter filter = new LogFilter();
if (!verbose) {
// В обычном режиме показываем только логи самого CLI (согласно правилам фильтрации)
filter.filter(n -> n.check(Log.SYSTEM_COMPONENT, "CLI"));
}
// Включаем цветной вывод в консоль с применением фильтра
Log.printConsoleColored(filter);
this.cliState = new CliState();
this.cliState.load();
this.api = new CliApi(this.cliState);
var consoleMgr = new ConsoleMgrCanonical(aa) {
@Override
public String getAppName() {
return "aether-cli";
}
};
consoleMgr.footer = "For more information, please visit the website https://aethernet.io";
consoleMgr.regConverter(CTypeI.of(CryptoLib.class), CryptoLib::valueOf);
consoleMgr.regConverter(CTypeI.of(UUID.class), s -> {
if (s == null) return null;
return api.resolveUuid(s);
});
consoleMgr.regResultConverter("bin", CTypeI.of(ClientStateInMemory.class), ClientStateInMemory::save);
setupMsgConverters(consoleMgr);
setupClientStateJsonConverter(consoleMgr);
this.resultFuture = consoleMgr.execute(api);
// Гарантируем закрытие всех ресурсов после завершения работы ConsoleMgr
resultFuture.toFuture().apply(() -> api.destroyer.destroy(true));
}
private void setupMsgConverters(ConsoleMgrCanonical consoleMgr) {
consoleMgr.regResultConverterCtx("bin", CTypeI.of(CliApi.Msg.class), (ctx, v) -> {
if (ctx.isToFile() && ctx.getFileName() == null) {
ctx.setFileName(v.address.toString());
}
return v.data;
});
consoleMgr.regResultConverterCtx("json", CTypeI.of(CliApi.Msg.class), (ctx, v) -> {
if (ctx.isToFile() && ctx.getFileName() == null) {
ctx.setFileName(v.address.toString());
}
Map<String, Object> m = Map.of("uid", v.address, "data", v.data);
return RU.toJson(m).toString().getBytes(StandardCharsets.UTF_8);
});
consoleMgr.regResultConverterCtx("hex", CTypeI.of(CliApi.Msg.class), (ctx, v) -> {
if (ctx.isToFile() && ctx.getFileName() == null) {
ctx.setFileName(v.address.toString());
}
return HexUtils.toHexString(v.data).getBytes();
});
consoleMgr.regResultConverterCtx("utf8", CTypeI.of(CliApi.Msg.class), (ctx, v) -> {
var s = AString.of();
s.add(v.address).add(" -> ").add(new String(v.data));
return s.getBytes();
});
}
private void setupClientStateJsonConverter(ConsoleMgrCanonical consoleMgr) {
consoleMgr.regResultConverter("json", CTypeI.of(ClientStateInMemory.class), v -> {
Map<String, Object> m = Map.of(
"uid", v.getUid(),
"alias", v.getAlias(),
"cloud", v.getCloud(v.getUid()),
"serverDescriptors", Flow.flow(v.getCloud(v.getUid()).getOrderedSids())
.mapToInt()
.mapToObj(v::getServerDescriptor)
.toMapExtractKey(ServerDescriptor::getId)
);
return RU.toJson(m).toString().getBytes(StandardCharsets.UTF_8);
});
}
public ARFuture<Object> getResultFuture() {
return resultFuture;
}
public static void main(String... aa) {
var cli = new CLI(aa);
try {
// Блокируем основной поток до завершения асинхронной команды.
// Фильтрация и вывод логов уже настроены в конструкторе CLI.
cli.getResultFuture().get();
} catch (Exception e) {
Log.error("CLI Execution failed", e);
}
}
}