mirror of
https://github.com/AsamK/signal-cli
synced 2025-09-04 05:00:39 +00:00
write both the command namespace and its output
This commit is contained in:
parent
a6cd1b295e
commit
8160b1ce2c
1 changed files with 18 additions and 1 deletions
|
@ -14,8 +14,12 @@ import org.asamk.signal.ReceiveMessageHandler;
|
||||||
import org.asamk.signal.manager.Manager;
|
import org.asamk.signal.manager.Manager;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.FileDescriptor;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
|
import java.io.PrintStream;
|
||||||
import java.io.PrintWriter;
|
import java.io.PrintWriter;
|
||||||
import java.io.StringWriter;
|
import java.io.StringWriter;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
@ -62,15 +66,28 @@ class InputReader implements Runnable {
|
||||||
while (alive) {
|
while (alive) {
|
||||||
try {
|
try {
|
||||||
String input = reader.readLine();
|
String input = reader.readLine();
|
||||||
if (input != null) {
|
if (input != null && !input.trim().isEmpty()) {
|
||||||
|
// parse namespace
|
||||||
Map<String, Object> commandMap = jsonProcessor.readValue(input, inputType);
|
Map<String, Object> commandMap = jsonProcessor.readValue(input, inputType);
|
||||||
HashMap<String, Object> mergedMap = new HashMap<>(ourNamespace);
|
HashMap<String, Object> mergedMap = new HashMap<>(ourNamespace);
|
||||||
mergedMap.putAll(commandMap);
|
mergedMap.putAll(commandMap);
|
||||||
Namespace commandNamespace = new NamespaceDefaultingToFalse(mergedMap);
|
Namespace commandNamespace = new NamespaceDefaultingToFalse(mergedMap);
|
||||||
|
// find command
|
||||||
String commandKey = commandNamespace.getString("command");
|
String commandKey = commandNamespace.getString("command");
|
||||||
LocalCommand commandObject = (LocalCommand) Commands.getCommand(commandKey);
|
LocalCommand commandObject = (LocalCommand) Commands.getCommand(commandKey);
|
||||||
assert commandObject != null;
|
assert commandObject != null;
|
||||||
|
// capture output
|
||||||
|
ByteArrayOutputStream commandOutput = new ByteArrayOutputStream();
|
||||||
|
System.setOut(new PrintStream(commandOutput));
|
||||||
commandObject.handleCommand(commandNamespace, manager);
|
commandObject.handleCommand(commandNamespace, manager);
|
||||||
|
System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out)));
|
||||||
|
Object output;
|
||||||
|
try {
|
||||||
|
output = jsonProcessor.readTree(commandOutput.toString());
|
||||||
|
} catch (IOException e) {
|
||||||
|
output = commandOutput.toString();
|
||||||
|
}
|
||||||
|
jsonWriter.write(Map.of("namespace", commandNamespace, "output", output));
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
if (this.inJson) {
|
if (this.inJson) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue