Search in sources :

Example 1 with CommandWithArgs

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);
    }
}
Also used : ValidationException(com.google.copybara.exception.ValidationException) ExitCode(com.google.copybara.util.ExitCode) CommandLineException(com.google.copybara.exception.CommandLineException) JCommander(com.beust.jcommander.JCommander) ParameterException(com.beust.jcommander.ParameterException) Path(java.nio.file.Path) RepoException(com.google.copybara.exception.RepoException) IOException(java.io.IOException) EmptyChangeException(com.google.copybara.exception.EmptyChangeException) CommandWithArgs(com.google.copybara.MainArguments.CommandWithArgs) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 2 with CommandWithArgs

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");
}
Also used : CommandWithArgs(com.google.copybara.MainArguments.CommandWithArgs) Test(org.junit.Test)

Example 3 with CommandWithArgs

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);
}
Also used : CommandWithArgs(com.google.copybara.MainArguments.CommandWithArgs) Test(org.junit.Test)

Example 4 with CommandWithArgs

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);
}
Also used : CommandWithArgs(com.google.copybara.MainArguments.CommandWithArgs) Test(org.junit.Test)

Aggregations

CommandWithArgs (com.google.copybara.MainArguments.CommandWithArgs)4 Test (org.junit.Test)3 JCommander (com.beust.jcommander.JCommander)1 ParameterException (com.beust.jcommander.ParameterException)1 ImmutableMap (com.google.common.collect.ImmutableMap)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 ExitCode (com.google.copybara.util.ExitCode)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 Map (java.util.Map)1