use of com.google.copybara.MainArguments.CommandWithArgs 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.MainArguments.CommandWithArgs in project copybara by google.
the class MainArgumentsTest method colonSyntaxParses.
@Test
public void colonSyntaxParses() throws Exception {
CopybaraCmd cmd = new CopybaraCmd() {
@Override
public ExitCode run(CommandEnv commandEnv) {
return null;
}
@Override
public String name() {
return "cmd";
}
};
mainArguments.unnamed = new ArrayList<>(ImmutableList.of("path/copy.bara.sky:workflow", "ref"));
CommandWithArgs res = mainArguments.parseCommand(ImmutableMap.of("cmd", cmd), cmd);
assertThat(res.getArgs()).containsExactly("path/copy.bara.sky", "workflow", "ref");
}
use of com.google.copybara.MainArguments.CommandWithArgs in project copybara by google.
the class MainArgumentsTest method colonSyntaxParsesWithCmd.
@Test
public void colonSyntaxParsesWithCmd() throws Exception {
CopybaraCmd cmd = new CopybaraCmd() {
@Override
public ExitCode run(CommandEnv commandEnv) {
return null;
}
@Override
public String name() {
return "cmd";
}
};
mainArguments.unnamed = new ArrayList<>(ImmutableList.of("cmd", "path/copy.bara.sky:workflow", "ref"));
CommandWithArgs res = mainArguments.parseCommand(ImmutableMap.of("cmd", cmd), cmd);
assertThat(res.getArgs()).containsExactly("path/copy.bara.sky", "workflow", "ref");
assertThat(res.getSubcommand()).isSameInstanceAs(cmd);
}
use of com.google.copybara.MainArguments.CommandWithArgs in project copybara by google.
the class MainArgumentsTest method noFile.
@Test
public void noFile() throws Exception {
CopybaraCmd cmd = new CopybaraCmd() {
@Override
public ExitCode run(CommandEnv commandEnv) {
return null;
}
@Override
public String name() {
return "cmd";
}
};
mainArguments.unnamed = new ArrayList<>(ImmutableList.of("cmd"));
CommandWithArgs res = mainArguments.parseCommand(ImmutableMap.of("cmd", cmd), cmd);
assertThat(res.getArgs()).isEmpty();
assertThat(res.getSubcommand()).isSameInstanceAs(cmd);
}
Aggregations