Search in sources :

Example 1 with SettableSupplier

use of com.google.copybara.util.SettableSupplier 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 ExitCode runInternal(String[] args, Console console, FileSystem fs) {
    try {
        ModuleSupplier moduleSupplier = newModuleSupplier();
        final MainArguments mainArgs = new MainArguments(args);
        GeneralOptions.Args generalOptionsArgs = new GeneralOptions.Args();
        SettableSupplier<GeneralOptions> generalOptionsSupplier = new SettableSupplier<>();
        List<Option> allOptions = new ArrayList<>(moduleSupplier.newOptions(generalOptionsSupplier));
        JCommander jcommander = new JCommander(ImmutableList.builder().addAll(allOptions).add(mainArgs).add(generalOptionsArgs).build());
        jcommander.setProgramName("copybara");
        String version = getVersion();
        logger.log(Level.INFO, "Copybara version: " + version);
        jcommander.parse(args);
        if (mainArgs.help) {
            console.info(usage(jcommander, version));
            return ExitCode.SUCCESS;
        }
        if (mainArgs.version) {
            console.info(getBinaryInfo());
            return ExitCode.SUCCESS;
        }
        ImmutableMap<String, CopybaraCmd> commands = Maps.uniqueIndex(getCommands(), CopybaraCmd::name);
        mainArgs.parseUnnamedArgs(commands, commands.get("migrate"));
        GeneralOptions generalOptions = generalOptionsArgs.init(environment, fs, console);
        generalOptionsSupplier.set(generalOptions);
        allOptions.add(generalOptions);
        Options options = new Options(allOptions);
        initEnvironment(options, mainArgs, jcommander);
        ConfigLoader<?> configLoader = newConfigLoader(moduleSupplier, options, mainArgs.getConfigPath(), mainArgs.getSourceRef());
        Copybara copybara = newCopybaraTool(moduleSupplier, options, mainArgs.getConfigPath());
        return mainArgs.getSubcommand().run(mainArgs, options, configLoader, copybara);
    } catch (CommandLineException | ParameterException e) {
        printCauseChain(Level.WARNING, console, args, e);
        console.error("Try 'copybara --help'.");
        return ExitCode.COMMAND_LINE_ERROR;
    } catch (RepoException e) {
        printCauseChain(Level.SEVERE, console, args, e);
        // have to do this hack.
        if (e.getCause() instanceof InterruptedException) {
            return ExitCode.INTERRUPTED;
        }
        return ExitCode.REPOSITORY_ERROR;
    } 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 ExitCode.NO_OP;
    } catch (ValidationException e) {
        printCauseChain(Level.WARNING, console, args, e);
        // TODO(malcon): Think of a better way of doing this
        return e.isRetryable() ? ExitCode.REPOSITORY_ERROR : ExitCode.CONFIGURATION_ERROR;
    } catch (IOException e) {
        handleUnexpectedError(console, e.getMessage(), args, e);
        return ExitCode.ENVIRONMENT_ERROR;
    } 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): " + e.getMessage(), args, e);
        return ExitCode.INTERNAL_ERROR;
    }
}
Also used : ValidationException(com.google.copybara.exception.ValidationException) ArrayList(java.util.ArrayList) CommandLineException(com.google.copybara.exception.CommandLineException) JCommander(com.beust.jcommander.JCommander) ParameterException(com.beust.jcommander.ParameterException) RepoException(com.google.copybara.exception.RepoException) IOException(java.io.IOException) SettableSupplier(com.google.copybara.util.SettableSupplier) EmptyChangeException(com.google.copybara.exception.EmptyChangeException)

Aggregations

JCommander (com.beust.jcommander.JCommander)1 ParameterException (com.beust.jcommander.ParameterException)1 CommandLineException (com.google.copybara.exception.CommandLineException)1 EmptyChangeException (com.google.copybara.exception.EmptyChangeException)1 RepoException (com.google.copybara.exception.RepoException)1 ValidationException (com.google.copybara.exception.ValidationException)1 SettableSupplier (com.google.copybara.util.SettableSupplier)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1