use of com.google.copybara.exception.CommandLineException in project copybara by google.
the class Main method resolveLocalConfig.
/**
* Returns a {@link ConfigFile} resolving the {@code configLocation} in the local filesystem.
*/
protected ConfigFile<Path> resolveLocalConfig(GeneralOptions generalOptions, String configLocation) throws ValidationException {
Path configPath = generalOptions.getFileSystem().getPath(configLocation);
String fileName = configPath.getFileName().toString();
checkCondition(fileName.contentEquals(COPYBARA_SKYLARK_CONFIG_FILENAME), "Copybara config file filename should be '%s' but it is '%s'.", COPYBARA_SKYLARK_CONFIG_FILENAME, configPath.getFileName());
// Treat the top level element specially since it is passed thru the command line.
if (!Files.exists(configPath)) {
throw new CommandLineException("Configuration file not found: " + configPath);
}
Path root = generalOptions.getConfigRoot() != null ? generalOptions.getConfigRoot() : findConfigRootHeuristic(configPath.toAbsolutePath());
return new PathBasedConfigFile(configPath.toAbsolutePath(), root).withContentLogging();
}
use of com.google.copybara.exception.CommandLineException in project copybara by google.
the class Main method runInternal.
/**
* Runs the command and returns the {@link ExitCode}.
*
* <p>This method is also responsible for the exception handling/logging.
*/
private CommandResult runInternal(String[] args, Console console, FileSystem fs) {
CommandEnv commandEnv = null;
CopybaraCmd subcommand = null;
try {
ModuleSet moduleSet = newModuleSet(environment, fs, console);
final MainArguments mainArgs = new MainArguments();
Options options = moduleSet.getOptions();
jCommander = new JCommander(ImmutableList.builder().addAll(options.getAll()).add(mainArgs).build());
jCommander.setProgramName("copybara");
String version = getVersion();
logger.atInfo().log("Copybara version: %s", version);
jCommander.parse(args);
ConfigLoaderProvider configLoaderProvider = newConfigLoaderProvider(moduleSet);
ImmutableMap<String, CopybaraCmd> commands = Maps.uniqueIndex(getCommands(moduleSet, configLoaderProvider, jCommander), CopybaraCmd::name);
// generating the usage info.
for (Map.Entry<String, CopybaraCmd> cmd : commands.entrySet()) {
jCommander.addCommand(cmd.getKey(), cmd.getValue());
}
CommandWithArgs cmdToRun = mainArgs.parseCommand(commands, commands.get("migrate"));
subcommand = cmdToRun.getSubcommand();
initEnvironment(options, cmdToRun.getSubcommand(), ImmutableList.copyOf(args));
GeneralOptions generalOptions = options.get(GeneralOptions.class);
Path baseWorkdir = mainArgs.getBaseWorkdir(generalOptions, generalOptions.getFileSystem());
commandEnv = new CommandEnv(baseWorkdir, options, cmdToRun.getArgs());
generalOptions.console().progressFmt("Running %s", subcommand.name());
// TODO(malcon): Remove this after 2019-09-15, once tested that temp features work.
logger.atInfo().log("Temporary features test: %s", options.get(GeneralOptions.class).isTemporaryFeature("TEST_TEMP_FEATURES", true));
ExitCode exitCode = subcommand.run(commandEnv);
return new CommandResult(exitCode, subcommand, commandEnv);
} catch (CommandLineException | ParameterException e) {
printCauseChain(Level.WARNING, console, args, e);
console.error("Try 'copybara help'.");
return new CommandResult(ExitCode.COMMAND_LINE_ERROR, subcommand, commandEnv);
} catch (RepoException e) {
printCauseChain(Level.SEVERE, console, args, e);
// have to do this hack.
if (e.getCause() instanceof InterruptedException) {
return new CommandResult(ExitCode.INTERRUPTED, subcommand, commandEnv);
}
return new CommandResult(ExitCode.REPOSITORY_ERROR, subcommand, commandEnv);
} catch (EmptyChangeException e) {
// This is not necessarily an error. Maybe the tool was run previously and there are no new
// changes to import.
console.warn(e.getMessage());
return new CommandResult(ExitCode.NO_OP, subcommand, commandEnv);
} catch (ValidationException e) {
printCauseChain(Level.WARNING, console, args, e);
return new CommandResult(ExitCode.CONFIGURATION_ERROR, subcommand, commandEnv);
} catch (IOException e) {
handleUnexpectedError(console, e.getMessage(), args, e);
return new CommandResult(ExitCode.ENVIRONMENT_ERROR, subcommand, commandEnv);
} catch (RuntimeException e) {
// This usually indicates a serious programming error that will require Copybara team
// intervention. Print stack trace without concern for presentation.
e.printStackTrace();
handleUnexpectedError(console, "Unexpected error (please file a bug against copybara): " + e.getMessage(), args, e);
return new CommandResult(ExitCode.INTERNAL_ERROR, subcommand, commandEnv);
}
}
use of com.google.copybara.exception.CommandLineException in project copybara by google.
the class Main method validateLocalConfig.
/**
* Validate that the passed config file is correct (exists, follows the correct format, parent
* if passed is a real parent, etc.).
*
* <p>Returns the absolute {@link Path} of the config file.
*/
protected Path validateLocalConfig(GeneralOptions generalOptions, String configLocation) throws ValidationException {
Path configPath = generalOptions.getFileSystem().getPath(configLocation).normalize();
String fileName = configPath.getFileName().toString();
checkCondition(fileName.contentEquals(COPYBARA_SKYLARK_CONFIG_FILENAME), "Copybara config file filename should be '%s' but it is '%s'.", COPYBARA_SKYLARK_CONFIG_FILENAME, configPath.getFileName());
// Treat the top level element specially since it is passed thru the command line.
if (!Files.exists(configPath)) {
throw new CommandLineException("Configuration file not found: " + configPath);
}
return configPath.toAbsolutePath();
}
use of com.google.copybara.exception.CommandLineException in project copybara by google.
the class MainArguments method parseUnnamedArgs.
void parseUnnamedArgs(ImmutableMap<String, ? extends CopybaraCmd> commands, CopybaraCmd defaultCmd) throws CommandLineException {
if (unnamed.isEmpty()) {
throw new CommandLineException("Expected at least a configuration file.");
} else if (unnamed.size() > 4) {
throw new CommandLineException(String.format("Expected at most four arguments: %s. Note that flag values that contain " + "whitespaces must be between quotes: --some-flag \"Some Value\"", unnamed));
}
CopybaraCmd subcommand = defaultCmd;
int argumentId = 0;
String firstArg = unnamed.get(argumentId);
// This should be enough for now
if (!firstArg.endsWith(COPYBARA_SKYLARK_CONFIG_FILENAME)) {
if (!commands.containsKey(firstArg.toLowerCase())) {
throw new CommandLineException(String.format("Invalid subcommand '%s'. Available commands: %s", firstArg, new TreeSet<>(commands.keySet())));
}
subcommand = commands.get(firstArg.toLowerCase());
argumentId++;
}
if (argumentId >= unnamed.size()) {
throw new CommandLineException(String.format("Configuration file missing for '%s' subcommand.", subcommand.name()));
}
String configPath = unnamed.get(argumentId);
argumentId++;
String workflowName = "default";
if (argumentId < unnamed.size()) {
workflowName = unnamed.get(argumentId);
argumentId++;
}
String sourceRef = null;
if (argumentId < unnamed.size()) {
// TODO(malcon): Move this to the commands
if (subcommand.name().equals("info") || subcommand.name().equals("validate")) {
throw new CommandLineException(String.format("Too many arguments for subcommand '%s'", subcommand.name()));
}
sourceRef = unnamed.get(argumentId);
}
argumentHolder = new ArgumentHolder(subcommand, configPath, workflowName, sourceRef);
}
use of com.google.copybara.exception.CommandLineException in project copybara by google.
the class Workflow method run.
@Override
public void run(Path workdir, ImmutableList<String> sourceRefs) throws RepoException, IOException, ValidationException {
if (sourceRefs.size() > 1) {
throw new CommandLineException(String.format("Workflow does not support multiple source_ref arguments yet: %s", ImmutableList.copyOf(sourceRefs)));
}
@Nullable String sourceRef = sourceRefs.size() == 1 ? sourceRefs.get(0) : null;
validateFlags();
try (ProfilerTask ignore = profiler().start("run/" + name)) {
console.progress("Getting last revision: " + "Resolving " + ((sourceRef == null) ? "origin reference" : sourceRef));
O resolvedRef = generalOptions.repoTask("origin.resolve_source_ref", () -> origin.resolve(sourceRef));
logger.log(Level.INFO, String.format("Running Copybara for workflow '%s' and ref '%s': %s", name, resolvedRef.asString(), this.toString()));
logger.log(Level.INFO, String.format("Using working directory : %s", workdir));
ImmutableList.Builder<DestinationEffect> allEffects = ImmutableList.builder();
WorkflowRunHelper<O, D> helper = newRunHelper(workdir, resolvedRef, sourceRef, event -> {
allEffects.addAll(event.getDestinationEffects());
eventMonitors().dispatchEvent(m -> m.onChangeMigrationFinished(event));
});
try (ProfilerTask ignored = profiler().start(mode.toString().toLowerCase())) {
mode.run(helper);
} finally {
if (!getGeneralOptions().dryRunMode) {
try (ProfilerTask ignored = profiler().start("after_all_migration")) {
ImmutableList<DestinationEffect> effects = allEffects.build();
ImmutableList<DestinationEffect> resultEffects = runHooks(effects, getAfterAllMigrationActions(), // Only do this once for all the actions
memoized(c -> helper.getOriginReader().getFeedbackEndPoint(c)), // Only do this once for all the actions
memoized(c -> helper.getDestinationWriter().getFeedbackEndPoint(c)), resolvedRef);
if (effects.size() != resultEffects.size()) {
console.warn("Effects where created in after_all_migrations, but they are ignored.");
}
}
}
}
}
}
Aggregations