/** * prompts the user with the given question. * * @param session the command session. * @param messageKey the message key. * @param messageArgs the message arguments. * @return true if user hits 'y' OR 'yes' else returns false * @throws IOException Indicates a failure while accessing the session's stdout. */ public static boolean promptUser( final CommandSession session, final String messageKey, final Object... messageArgs) throws IOException { if ((Boolean) session.get(Constants.INTERACTIVE_MODE)) { session.getConsole().print(Ansi.ansi().eraseLine(Erase.ALL)); final String confirmationQuestion = ShellUtils.getFormattedMessage(messageKey, messageArgs); session.getConsole().print(confirmationQuestion + " "); session.getConsole().flush(); char responseChar = '\0'; final StringBuilder responseBuffer = new StringBuilder(); while (true) { responseChar = (char) session.getKeyboard().read(); if (responseChar == '\u007F') { // backspace if (responseBuffer.length() > 0) { responseBuffer.deleteCharAt(responseBuffer.length() - 1); session.getConsole().print(Ansi.ansi().cursorLeft(1).eraseLine()); } } else if (responseChar == WIN_RETURN_CHAR || responseChar == LINUX_RETURN_CHAR) { session.getConsole().println(); break; } else { session.getConsole().print(responseChar); responseBuffer.append(responseChar); } session.getConsole().flush(); } final String responseStr = responseBuffer.toString().trim(); return "y".equalsIgnoreCase(responseStr) || "yes".equalsIgnoreCase(responseStr); } // Shell is running in nonInteractive mode. we skip the question. return true; }
/** * Gets the latest events of this deployment id. Events are sorted by event index. * * @return A list of events. If this is the first time events are requested, all events are * retrieved. Otherwise, only new events (that were not reported earlier) are retrieved. * @throws RestClientException Indicates a failure to get events from the server. */ @Override public boolean lifeCycleEnded() throws RestClientException, CLIException { boolean serviceIsInstalled; try { ServiceDescription serviceDescription = restClient.getServiceDescription(applicationName, serviceName); logger.fine("Service description is : " + serviceDescription); CloudifyConstants.DeploymentState serviceState = serviceDescription.getServiceState(); if (serviceState.equals(CloudifyConstants.DeploymentState.FAILED)) { throw new CLIException( ShellUtils.getFormattedMessage( CloudifyErrorMessages.FAILED_TO_DEPLOY_SERVICE.getName(), serviceName)); } serviceIsInstalled = serviceState.equals(CloudifyConstants.DeploymentState.STARTED); } catch (final RestClientResponseException e) { if (e.getStatusCode() == RESOURCE_NOT_FOUND_EXCEPTION_CODE) { // the service is not available yet serviceIsInstalled = false; } else { throw e; } } return serviceIsInstalled; }
/** * Checks if the latest version is used. * * @param session the command session. */ public static void doVersionCheck(final CommandSession session) { String currentBuildStr = PlatformVersion.getBuildNumber(); if (currentBuildStr.contains("-")) { currentBuildStr = currentBuildStr.substring(0, currentBuildStr.indexOf("-")); } final int currentVersion = Integer.parseInt(currentBuildStr); final int latestBuild = getLatestBuildNumber(currentVersion); String message; if (latestBuild == -1) { message = ShellUtils.getFormattedMessage("could_not_get_version"); } else if (latestBuild > currentVersion) { message = ShellUtils.getFormattedMessage("newer_version_exists"); } else { message = ShellUtils.getFormattedMessage("version_up_to_date"); } session.getConsole().println(message); session.getConsole().println(); }