ConsoleInteractiveConfigurer.java

  1. /**
  2.  * Copyright (c) 2013-2017 Polago AB
  3.  * All rights reserved.
  4.  *
  5.  * Permission is hereby granted, free of charge, to any person obtaining
  6.  * a copy of this software and associated documentation files (the
  7.  * "Software"), to deal in the Software without restriction, including
  8.  * without limitation the rights to use, copy, modify, merge, publish,
  9.  * distribute, sublicense, and/or sell copies of the Software, and to
  10.  * permit persons to whom the Software is furnished to do so, subject to
  11.  * the following conditions:
  12.  *
  13.  * The above copyright notice and this permission notice shall be
  14.  * included in all copies or substantial portions of the Software.
  15.  *
  16.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17.  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18.  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19.  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  20.  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  21.  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  22.  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23.  */

  24. package org.polago.deployconf;

  25. import java.io.IOException;
  26. import java.io.OutputStreamWriter;
  27. import java.io.PrintWriter;
  28. import java.nio.charset.Charset;

  29. import org.jline.reader.LineReader;
  30. import org.jline.reader.LineReaderBuilder;
  31. import org.jline.terminal.Terminal;
  32. import org.jline.terminal.TerminalBuilder;

  33. /**
  34.  * Interactive Configurer that ask the user for the value.
  35.  */
  36. public class ConsoleInteractiveConfigurer implements InteractiveConfigurer {

  37.     private final LineReader reader;

  38.     private final PrintWriter writer;

  39.     /**
  40.      * Public Constructor.
  41.      *
  42.      * @throws IOException indicating processing failure
  43.      */
  44.     public ConsoleInteractiveConfigurer() throws IOException {
  45.         Terminal terminal = TerminalBuilder.terminal();
  46.         reader = LineReaderBuilder.builder().terminal(terminal).build();
  47.         writer = new PrintWriter(new OutputStreamWriter(System.out, Charset.defaultCharset()), true);
  48.     }

  49.     /**
  50.      * {@inheritDoc}
  51.      *
  52.      * @throws IOException indicating processing failure
  53.      */
  54.     @Override
  55.     public String configure(String name, String description, String defaultValue) throws IOException {

  56.         String value = null;

  57.         writer.println();
  58.         writer.println(description.replaceAll("(?m)^[\\s&&[^\\r\\n]]+", ""));
  59.         writer.println();

  60.         StringBuilder prompt = new StringBuilder(name);
  61.         if (defaultValue != null) {
  62.             prompt.append("[");
  63.             prompt.append(defaultValue);
  64.             prompt.append("]");
  65.         }
  66.         prompt.append("=");
  67.         while (value == null || value.length() == 0) {
  68.             value = reader.readLine(prompt.toString());
  69.             if (value == null || value.trim().length() == 0) {
  70.                 value = defaultValue;
  71.             } else {
  72.                 value = value.trim();
  73.             }

  74.             if (value != null && value.length() > 0) {
  75.                 writer.println();
  76.                 writer.print("==> '");
  77.                 writer.print(value);
  78.                 writer.println("'");
  79.                 writer.println();
  80.             }
  81.         }
  82.         return value;
  83.     }

  84.     /**
  85.      * {@inheritDoc}
  86.      */
  87.     @Override
  88.     public PrintWriter getWriter() {
  89.         return writer;
  90.     }

  91. }