Example usage for java.io Console readline

List of usage examples for java.io Console readline

Introduction

In this page you can find the example usage for java.io Console readline.

Prototype

private char[] readline(boolean zeroOut) throws IOException 

Source Link

Usage

From source file:com.linkedin.bowser.tool.REPLCommandLine.java

public void doRun() throws Exception {
    StringBuilder line = new StringBuilder();
    Console console = System.console();

    while (true) {
        String input;/*from  w  w  w.j a  v a  2 s. c  om*/
        if (line.length() > 0) {
            input = console.readLine(_dots);
        } else {
            input = console.readLine(_prompt);
            if ("quit".equals(input) || "exit".equals(input))
                break;
        }

        if (input.endsWith("\\")) {
            line.append(input.substring(0, input.length() - 1));
            line.append("\n");
            continue;
        }

        if (line.length() > 0) {
            input = line.toString() + input;
            line = new StringBuilder();
        }

        try {
            _repl.execute(input, System.out);
        } catch (QueryRuntimeException e) {
            System.err.println(e.getMessage());
        } catch (QueryFormatException e) {
            System.err.println(e.getMessage());
        }
    }
}

From source file:com.jwm123.loggly.reporter.Configuration.java

public void update() throws Exception {
    Console console = System.console();
    do {/*from   w  w w  .j  a v a2 s .  c o  m*/
        username = console.readLine("Username: ");
    } while (StringUtils.isBlank(username));
    do {
        password = new String(console.readPassword("Password: "));
    } while (StringUtils.isBlank(password));
    do {
        account = console.readLine("Loggly Account: ");
    } while (StringUtils.isBlank(account));
    mailServer = console.readLine("Mail Server: ");
    mailFrom = console.readLine("Mail From: ");
    String mailPortStr = console.readLine("Mail Port [25]: ");
    if (StringUtils.isBlank(mailPortStr) || !mailPortStr.matches("\\d*")) {
        mailPort = 25;
    } else {
        mailPort = new Integer(mailPortStr);
    }
    if (StringUtils.isNotBlank(mailServer)) {
        mailUsername = console.readLine("Mail Username: ");
        if (StringUtils.isNotBlank(mailUsername)) {
            mailPassword = new String(console.readPassword("Mail Password: "));
        }
    }
    writeOut();
}

From source file:org.ballerinalang.containers.docker.cmd.DockerCmd.java

/**
 * Prompt user to continue Docker image building.
 *
 * @param imageName    Name of the image to be built.
 * @param imageVersion Version of the image to be built
 * @return True if confirmed, false if aborted or exceeded attempts.
 *//*from   ww w .  j a  v a2 s  .c o m*/
private boolean canProceed(String imageName, String imageVersion) {
    Console console = System.console();
    String choice;
    String dockerHostToPrint = (dockerHost != null) ? dockerHost : DEFAULT_DOCKER_HOST;

    int attempts = 3;
    do {
        choice = console.readLine("Build docker image [" + imageName + ":" + imageVersion + "] in docker host ["
                + dockerHostToPrint + "]? (y/n): ");

        if (choice.equalsIgnoreCase("y")) {
            return true;
        }

        if (choice.equalsIgnoreCase("n")) {
            return false;
        }

    } while (--attempts > 0);

    return false;
}

From source file:com.netflix.spinnaker.halyard.cli.command.v1.NestableCommand.java

/**
 * This recursively walks the chain of subcommands, until it finds the last in the chain, and runs executeThis.
 *
 * @see NestableCommand#executeThis()//from  w  w  w  .  j  a v a2s  .c o  m
 */
public void execute() {
    String subCommand = commander.getParsedCommand();
    if (subCommand == null) {
        if (help) {
            showHelp();
        } else {
            if (this instanceof DeprecatedCommand) {
                AnsiUi.warning("This command is deprecated.");
                AnsiUi.warning(((DeprecatedCommand) this).getDeprecatedWarning());
            }

            if (this instanceof ProtectedCommand && !GlobalOptions.getGlobalOptions().isQuiet()) {
                String prompt = ((ProtectedCommand) this).getPrompt();
                Console console = System.console();
                String input = console.readLine(prompt + " Do you want to continue? (Y/n) ");
                if (!input.equalsIgnoreCase("y")) {
                    AnsiUi.raw("Aborted.");
                    return;
                }
            }
            safeExecuteThis();
        }
    } else {
        subcommands.get(subCommand).execute();
    }
}

From source file:org.wildfly.security.tool.Command.java

/**
 * Prompt for interactive user input with possible confirmation of input data.
 * When data are not confirmed tool exits with {@link #INPUT_DATA_NOT_CONFIRMED} exit code
 *
 * @param echo echo the characters typed
 * @param prompt text to display before the input
 * @param confirm confirm data after the first input
 * @param confirmPrompt confirmation text
 * @return data as user inputs it//from w ww  . j  a v  a  2 s.  co  m
 * @throws Exception
 */
protected String prompt(boolean echo, String prompt, boolean confirm, String confirmPrompt) throws Exception {
    Console console = System.console();
    if (echo || console == null) {
        if (console == null && redirectionValues == null) {
            try (BufferedReader in = new BufferedReader(new InputStreamReader(System.in))) {
                redirectionValues = new ArrayList<>();
                String value;
                while ((value = in.readLine()) != null) {
                    redirectionValues.add(value);
                }
            } catch (IOException e) {
                setStatus(GENERAL_CONFIGURATION_ERROR);
                throw new Exception(e);
            }
        }
        String first = console != null ? console.readLine(prompt)
                : (redirectionValues.size() == 0 ? null : redirectionValues.remove(0));
        if (first != null && confirm) {
            String second = console != null ? console.readLine(confirmPrompt)
                    : (redirectionValues.size() == 0 ? null : redirectionValues.remove(0));
            if (first.equals(second)) {
                return first;
            } else {
                System.err.println(ElytronToolMessages.msg.inputDataNotConfirmed());
                System.exit(INPUT_DATA_NOT_CONFIRMED);
                return null;
            }
        } else {
            return first;
        }
    } else {
        char[] inVisible = console.readPassword(prompt != null ? prompt : "Password:");
        if (inVisible != null && confirm) {
            char[] inVisible2 = console
                    .readPassword(confirmPrompt != null ? confirmPrompt : "Confirm password:");
            if (Arrays.equals(inVisible, inVisible2)) {
                return new String(inVisible);
            } else {
                System.err.println(ElytronToolMessages.msg.inputDataNotConfirmed());
                System.exit(INPUT_DATA_NOT_CONFIRMED);
                return null;
            }
        }
        if (inVisible != null) {
            return new String(inVisible);
        }
        return null;
    }
}

From source file:org.overlord.sramp.wagon.SrampWagon.java

/**
 * Prompts the user to enter a username for authentication credentials.
 *///www.ja va 2  s .  c  o  m
private String promptForUsername() {
    Console console = System.console();
    if (console != null) {
        return console.readLine(Messages.i18n.format("USERNAME_PROMPT")); //$NON-NLS-1$
    } else {
        System.err.println(Messages.i18n.format("NO_CONSOLE_ERROR_1")); //$NON-NLS-1$
        return null;
    }
}