org.bidib.wizard.common.script.debug.SendDebugTextCommand Maven / Gradle / Ivy
package org.bidib.wizard.common.script.debug;
import java.util.Scanner;
import org.bidib.wizard.api.context.ApplicationContext;
import org.bidib.wizard.api.service.console.ConsoleService;
import org.bidib.wizard.common.script.AbstractScriptCommand;
import org.bidib.wizard.common.script.ScriptUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class SendDebugTextCommand extends AbstractScriptCommand {
private static final Logger LOGGER = LoggerFactory.getLogger(SendDebugTextCommand.class);
public static final String KEY = "sendDebugText";
private String message;
public static final String HELP =
CODE_BLOCK_START + KEY + prepareHelpHtml(" --text=\"\"") + DIV_END
+ prepareHelpHtml("\nSend a string to the debug interface.\n\nExample:\n") + CODE_BLOCK_START
+ prepareHelpHtml(KEY + " --text=\"info\"") + DIV_END;
public SendDebugTextCommand(ConsoleService consoleService) {
super(consoleService, KEY, HELP);
}
@Override
public void parse(String commandLine) {
try (Scanner scanner = new Scanner(commandLine)) {
if (!getKey().equals(scanner.next())) {
LOGGER.warn("Invalid command is scanned, key does not match.");
throw new IllegalArgumentException("Invalid command is scanned, key does not match.");
}
line = commandLine.trim();
String arguments = line.substring(KEY.length() + 1);
try {
this.message = ScriptUtils.parseStringParam("(?<=--text=)(?s)(.*$)", arguments);
}
catch (Exception ex) {
LOGGER.warn("No connectionId found.", ex);
throw new RuntimeException("send text: No message found.");
}
}
LOGGER.info("Parsed command, message: {}", message);
}
public String getMessage() {
return message;
}
@Override
protected void internalExecute(DebugInterfaceScripting scripting, ApplicationContext context) {
LOGGER.info("Connect to debug interface with message: {}", message);
scripting.sendDebugText(message);
}
}