use of jline.ConsoleReader in project distributedlog by twitter.
the class ConsoleProxyMultiWriter method main.
public static void main(String[] args) throws Exception {
if (2 != args.length) {
System.out.println(HELP);
return;
}
String finagleNameStr = args[0];
final String streamList = args[1];
DistributedLogClient client = DistributedLogClientBuilder.newBuilder().clientId(ClientId$.MODULE$.apply("console-proxy-writer")).name("console-proxy-writer").thriftmux(true).finagleNameStr(finagleNameStr).build();
String[] streamNameList = StringUtils.split(streamList, ',');
DistributedLogMultiStreamWriter multiStreamWriter = DistributedLogMultiStreamWriter.newBuilder().streams(Lists.newArrayList(streamNameList)).bufferSize(0).client(client).flushIntervalMs(0).firstSpeculativeTimeoutMs(10000).maxSpeculativeTimeoutMs(20000).requestTimeoutMs(50000).build();
ConsoleReader reader = new ConsoleReader();
String line;
while ((line = reader.readLine(PROMPT_MESSAGE)) != null) {
multiStreamWriter.write(ByteBuffer.wrap(line.getBytes(UTF_8))).addEventListener(new FutureEventListener<DLSN>() {
@Override
public void onFailure(Throwable cause) {
System.out.println("Encountered error on writing data");
cause.printStackTrace(System.err);
Runtime.getRuntime().exit(0);
}
@Override
public void onSuccess(DLSN value) {
// done
}
});
}
multiStreamWriter.close();
client.close();
}
use of jline.ConsoleReader in project GNS by MobilityFirst.
the class ProxyConsole method main.
/**
* @param args -silent to avoid any prompt in non-interactive mode
*/
public static void main(String[] args) {
boolean silent = (args.length > 0 && "-silent".equalsIgnoreCase(args[0]));
try {
ConsoleReader consoleReader = new ConsoleReader(System.in, new PrintWriter(System.out, true));
ConsoleModule module = new ConsoleModule(consoleReader);
module.setSilent(silent);
module.handlePrompt();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.exit(-1);
}
}
use of jline.ConsoleReader in project GNS by MobilityFirst.
the class CommandLineInterface method main.
/**
* Starts the GNS command line interface (CLI) console
*
* @param args optional argument is -silent for no console output
* @throws Exception
*/
public static void main(String[] args) throws Exception {
try {
CommandLine parser = initializeOptions(args);
if (parser.hasOption("help")) {
printUsage();
//System.out.println("-host and -port are required!");
System.exit(1);
}
boolean silent = parser.hasOption("silent");
boolean noDefaults = parser.hasOption("noDefaults");
ConsoleReader consoleReader = new ConsoleReader(System.in, new PrintWriter(System.out, true));
ConsoleModule module = new ConsoleModule(consoleReader);
if (noDefaults) {
module.setUseGnsDefaults(false);
KeyPairUtils.removeDefaultGns();
}
module.setSilent(silent);
if (!silent) {
module.printString("GNS Client Version: " + GNSClientConfig.readBuildVersion() + "\n");
}
module.handlePrompt();
System.exit(0);
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
}
use of jline.ConsoleReader in project GNS by MobilityFirst.
the class ConsoleBasedTest method runCommandsInConsole.
/**
* Run inCommands commands through the CLI and compare the output with
* expectedOutput. Can also check if a default GNS and/or GUID have been set.
*
* @param inCommands list of console commands separated by '\n'
* @param expectedOutput list of expected console output (can use Java regular
* expressions, each line separated by '\n')
* @param requireDefaultGns check if a default GNS has been defined (will
* error out if not)
* @param requireDefaultAccountGuid check if a default GUID has been defined
* (will error out if not)
*/
protected void runCommandsInConsole(String inCommands, String expectedOutput, boolean requireDefaultGns, boolean requireDefaultAccountGuid) {
boolean success = false;
StringWriter output = new StringWriter();
try {
ConsoleReader consoleReader = new ConsoleReader(new ByteArrayInputStream(inCommands.getBytes("UTF-8")), output);
ConsoleModule module = new ConsoleModule(consoleReader);
module.printString("GNS Client Version: " + GNSClientConfig.readBuildVersion() + "\n");
// Run the commands
module.handlePrompt();
// Check the output
StringTokenizer expected = new StringTokenizer(expectedOutput, "\n");
StringTokenizer actual = new StringTokenizer(output.toString(), "\n");
if (!actual.hasMoreTokens()) {
fail("No console output");
}
if (!actual.nextToken().startsWith("GNS Client Version")) {
fail("Unexpected console output, should start with 'GNS Client Version'");
}
// Check that default GNS defaults is set
if (requireDefaultGns) {
if (!actual.hasMoreTokens()) {
fail("Default GNS not set");
}
String defaultGns = actual.nextToken();
if ("Default GNS: null".equals(defaultGns)) {
fail("Default GNS not set");
}
if (!defaultGns.startsWith("Default GNS: ")) {
fail("Unexpected console output, should start with 'Default GNS: '");
}
// Check GNS Connectivity .
if (!actual.hasMoreTokens()) {
fail("No console output during GNS connectivity check");
}
if (!actual.nextToken().startsWith("Checking GNS connectivity")) {
fail("Unexpected console output during GNS connectivity check");
}
if (!actual.hasMoreTokens()) {
fail("No console output during GNS connectivity check");
}
if (!actual.nextToken().startsWith("Connected to GNS")) {
fail("Default GNS is not reachable");
}
} else {
// Consume lines until we connected or not to a default GNS
while (actual.hasMoreTokens()) {
String line = actual.nextToken();
if (line.startsWith("Connected to GNS") || line.startsWith("Failed to connect to GNS") || line.startsWith("Couldn't connect to default GNS")) {
break;
}
}
}
if (requireDefaultAccountGuid) {
// Check default GUID
if (!actual.hasMoreTokens()) {
fail("Default GUID not set");
}
String defaultGuid = actual.nextToken();
if (defaultGuid.matches("Default GUID: null")) {
fail("Default GUID not set");
}
if (!actual.hasMoreTokens()) {
fail("Default GUID not set");
}
defaultGuid = actual.nextToken();
if (!defaultGuid.matches("Looking up alias .*")) {
fail("Unexpected console output, should start with 'Looking up alias'");
}
if (!actual.hasMoreTokens()) {
fail("Default GUID not set");
}
if (!actual.nextToken().startsWith("Current GUID set to ")) {
fail("Default GUID not set or not valid");
}
if (!actual.hasMoreTokens()) {
fail("Default GUID not set or not valid");
}
// Next should be the console prompt
if (!actual.nextToken().startsWith(GNS_CLI_PROMPT)) {
fail("Default GUID not properly set, expected console prompt");
}
} else {
// Consume all input until first prompt
while (actual.hasMoreTokens() && !actual.nextToken().startsWith(GNS_CLI_PROMPT)) ;
}
// Diff outputs
while (expected.hasMoreTokens()) {
String nextExpected = expected.nextToken();
String nextActual = null;
if (actual.hasMoreTokens()) {
// Skip command line prompts to get real output
nextActual = actual.nextToken();
while (nextActual.startsWith(GNS_CLI_PROMPT) && actual.hasMoreTokens()) {
nextActual = actual.nextToken();
}
// Treat expected as a regular expression
if (!nextActual.matches("(?s)" + nextExpected)) {
if (StringUtils.getLevenshteinDistance(nextActual, nextExpected) < 5) {
for (int i = 0; i < nextActual.length(); i++) {
final char actualChar = nextActual.charAt(i);
if (i < expectedOutput.length()) {
final char expectedChar = nextExpected.charAt(i);
if (actualChar != expectedChar) {
System.out.println("Character " + i + " differs: " + ((int) actualChar) + " vs expected " + ((int) expectedChar) + " - " + actualChar + "<>" + expectedChar + "\n");
}
} else {
System.out.println("Character " + i + " is extra: " + ((int) actualChar) + " - " + actualChar);
}
}
}
fail("Got : '" + nextActual + "'\n" + "Expected: '" + nextExpected + "'\n");
}
} else {
fail("Missing expected output: " + nextExpected);
}
}
// Check extra output
while (actual.hasMoreTokens()) {
String nextActual = actual.nextToken();
while (nextActual.startsWith(GNS_CLI_PROMPT) && actual.hasMoreTokens()) {
nextActual = actual.nextToken();
}
if (!nextActual.startsWith(GNS_CLI_PROMPT)) {
fail("Extra output: " + actual.nextToken());
}
}
success = true;
} catch (IOException e) {
fail("Error during console execution " + e);
} finally {
if (!success) {
System.out.println("**** COMPLETE OUTPUT for " + testName.getMethodName() + " ****");
System.out.println(output.toString());
}
}
}
use of jline.ConsoleReader in project zm-mailbox by Zimbra.
the class CliUtil method enableCommandLineEditing.
/**
* Turns on command line editing with JLine.
* @param histFilePath path to the history file, or {@code null} to not save history
* @throws IOException if the history file is not writable or cannot be created
*/
public static void enableCommandLineEditing(String histFilePath) throws IOException {
File histFile = null;
if (histFilePath != null) {
histFile = new File(histFilePath);
if (!histFile.exists()) {
if (!histFile.createNewFile()) {
throw new IOException("Unable to create history file " + histFilePath);
}
}
if (!histFile.canWrite()) {
throw new IOException(histFilePath + " is not writable");
}
}
ConsoleReader reader = new ConsoleReader();
if (histFile != null) {
reader.setHistory(new History(histFile));
}
ConsoleReaderInputStream.setIn(reader);
}
Aggregations