use of com.google.copybara.util.console.Console in project copybara by google.
the class Main method getConsole.
protected Console getConsole(String[] args) {
boolean verbose = isVerbose(args);
// If System.console() is not present, we are forced to use LogConsole
Console console;
if (System.console() == null) {
console = LogConsole.writeOnlyConsole(System.err, verbose);
} else if (Arrays.asList(args).contains(GeneralOptions.NOANSI)) {
// The System.console doesn't detect redirects/pipes, but at least we have
// jobs covered.
console = LogConsole.readWriteConsole(System.in, System.err, verbose);
} else {
console = new AnsiConsole(System.in, System.err, verbose);
}
Optional<String> maybeConsoleFilePath = findFlagValue(args, GeneralOptions.CONSOLE_FILE_PATH);
if (!maybeConsoleFilePath.isPresent()) {
return console;
}
Path consoleFilePath = Paths.get(maybeConsoleFilePath.get());
try {
Files.createDirectories(consoleFilePath.getParent());
} catch (IOException e) {
logger.atSevere().withCause(e).log("Could not create parent directories to file: %s. Redirecting will be disabled.", consoleFilePath);
return console;
}
return new FileConsole(console, consoleFilePath, getConsoleFlushRate(args));
}
use of com.google.copybara.util.console.Console in project copybara by google.
the class MigrateCmd method loadConfig.
private Config loadConfig(Options options, ConfigLoader configLoader, String migrationName) throws IOException, ValidationException {
GeneralOptions generalOptions = options.get(GeneralOptions.class);
Console console = generalOptions.console();
Config config = configLoader.load(console);
console.progress("Validating configuration");
ValidationResult result = configValidator.validate(config, migrationName);
if (!result.hasErrors()) {
return config;
}
result.getErrors().forEach(console::error);
console.error("Configuration is invalid.");
throw new ValidationException("Error validating configuration: Configuration is invalid.");
}
use of com.google.copybara.util.console.Console in project copybara by google.
the class ValidateCmd method validate.
/**
* Validates that the configuration is correct and that there is a valid migration specified by
* {@code migrationName}.
*
* <p>Note that, besides validating the specific migration, all the configuration will be
* validated syntactically.
*
* Returns true iff this configuration is valid.
*/
private ValidationResult validate(Options options, ConfigLoader configLoader, String migrationName) throws IOException {
Console console = options.get(GeneralOptions.class).console();
ValidationResult.Builder resultBuilder = new ValidationResult.Builder();
try {
Config config = configLoader.load(console);
resultBuilder.append(configValidator.validate(config, migrationName));
} catch (ValidationException e) {
// The validate subcommand should not throw Validation exceptions but log a result
StringBuilder error = new StringBuilder(e.getMessage()).append("\n");
Throwable cause = e.getCause();
while (cause != null) {
error.append(" CAUSED BY: ").append(cause.getMessage()).append("\n");
cause = cause.getCause();
}
resultBuilder.error(error.toString());
}
return resultBuilder.build();
}
use of com.google.copybara.util.console.Console in project copybara by google.
the class ApiCheckerTest method testCheckInternalError.
@Test
public void testCheckInternalError() throws CheckerException {
ApiChecker checker = new ApiChecker(new FakeChecker() {
@Override
public void doCheck(ImmutableMap<String, String> fields, Console console) throws IOException {
throw new IOException("Tool error!");
}
}, new TestingConsole());
RuntimeException e1 = assertThrows(RuntimeException.class, () -> checker.check("foo", new Object()));
assertThat(e1).hasMessageThat().isEqualTo("Error running checker");
RuntimeException e2 = assertThrows(RuntimeException.class, () -> checker.check("foo", new Object(), "bar", new Object()));
assertThat(e2).hasMessageThat().isEqualTo("Error running checker");
RuntimeException e3 = assertThrows(RuntimeException.class, () -> checker.check("foo", new Object(), "bar", new Object(), "baz", new Object()));
assertThat(e3).hasMessageThat().isEqualTo("Error running checker");
}
use of com.google.copybara.util.console.Console in project copybara by google.
the class ApiCheckerTest method testCheck.
@Test
public void testCheck() throws CheckerException {
ApiChecker checker = new ApiChecker(new FakeChecker() {
@Override
public void doCheck(ImmutableMap<String, String> fields, Console console) {
}
}, new TestingConsole());
checker.check("foo", new Object());
checker.check("foo", new Object(), "bar", new Object());
checker.check("foo", new Object(), "bar", new Object(), "baz", new Object());
}
Aggregations