init: Support updating an existing site configuration
The init prompts now default to use the current data read from gerrit.config, rather than assuming we are building a site from scratch. This allows us to prompt for new properties like the database configuration when updating an existing site from a <2.1 to >=2.1 layout. The prompts have also been reworked, and boolean questions given suitable defaults, so its easier to initialize a default site by just pressing the enter key and leaving every question unanswered. As a side effect, much of the code is more readable, especially around the HTTPD listener configuration and SSL setup. Change-Id: Ib78e825bf954449ec187ecefe88f21e75daf2844 Signed-off-by: Shawn O. Pearce <sop@google.com>
This commit is contained in:
@@ -62,22 +62,13 @@ public abstract class ConsoleUI {
|
||||
public abstract void header(String fmt, Object... args);
|
||||
|
||||
/** Request the user to answer a yes/no question. */
|
||||
public abstract boolean yesno(String fmt, Object... args);
|
||||
public abstract boolean yesno(Boolean def, String fmt, Object... args);
|
||||
|
||||
/** Prints a message asking the user to let us know when its safe to continue. */
|
||||
public abstract void waitForUser();
|
||||
|
||||
/** Prompt the user for a string, suggesting a default, and returning choice. */
|
||||
public final String readString(String def, String fmt, Object... args) {
|
||||
if (def != null && def.isEmpty()) {
|
||||
def = null;
|
||||
}
|
||||
return readStringImpl(def, fmt, args);
|
||||
}
|
||||
|
||||
/** Prompt the user for a string, suggesting a default, and returning choice. */
|
||||
protected abstract String readStringImpl(String def, String fmt,
|
||||
Object... args);
|
||||
public abstract String readString(String def, String fmt, Object... args);
|
||||
|
||||
/** Prompt the user for a password, returning the string; null if blank. */
|
||||
public abstract String password(String fmt, Object... args);
|
||||
@@ -86,7 +77,6 @@ public abstract class ConsoleUI {
|
||||
public abstract <T extends Enum<?>> T readEnum(T def, String fmt,
|
||||
Object... args);
|
||||
|
||||
|
||||
private static class Interactive extends ConsoleUI {
|
||||
private final Console console;
|
||||
|
||||
@@ -100,13 +90,27 @@ public abstract class ConsoleUI {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean yesno(String fmt, Object... args) {
|
||||
public boolean yesno(Boolean def, String fmt, Object... args) {
|
||||
final String prompt = String.format(fmt, args);
|
||||
for (;;) {
|
||||
final String yn = console.readLine("%-30s [y/n]? ", prompt);
|
||||
String y = "y";
|
||||
String n = "n";
|
||||
if (def != null) {
|
||||
if (def) {
|
||||
y = "Y";
|
||||
} else {
|
||||
n = "N";
|
||||
}
|
||||
}
|
||||
|
||||
String yn = console.readLine("%-30s [%s/%s]? ", prompt, y, n);
|
||||
if (yn == null) {
|
||||
throw abort();
|
||||
}
|
||||
yn = yn.trim();
|
||||
if (def != null && yn.isEmpty()) {
|
||||
return def;
|
||||
}
|
||||
if (yn.equalsIgnoreCase("y") || yn.equalsIgnoreCase("yes")) {
|
||||
return true;
|
||||
}
|
||||
@@ -124,7 +128,7 @@ public abstract class ConsoleUI {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String readStringImpl(String def, String fmt, Object... args) {
|
||||
public String readString(String def, String fmt, Object... args) {
|
||||
final String prompt = String.format(fmt, args);
|
||||
String r;
|
||||
if (def != null) {
|
||||
@@ -208,12 +212,12 @@ public abstract class ConsoleUI {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean yesno(String fmt, Object... args) {
|
||||
return true;
|
||||
public boolean yesno(Boolean def, String fmt, Object... args) {
|
||||
return def != null ? def : true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String readStringImpl(String def, String fmt, Object... args) {
|
||||
public String readString(String def, String fmt, Object... args) {
|
||||
return def;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user