Search in sources :

Example 1 with CommandLineException

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();
}
Also used : Path(java.nio.file.Path) PathBasedConfigFile(com.google.copybara.config.PathBasedConfigFile) CommandLineException(com.google.copybara.exception.CommandLineException)

Example 2 with CommandLineException

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);
    }
}
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 3 with CommandLineException

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();
}
Also used : Path(java.nio.file.Path) CommandLineException(com.google.copybara.exception.CommandLineException)

Example 4 with CommandLineException

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);
}
Also used : TreeSet(java.util.TreeSet) CommandLineException(com.google.copybara.exception.CommandLineException)

Example 5 with CommandLineException

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.");
                    }
                }
            }
        }
    }
}
Also used : Writer(com.google.copybara.Destination.Writer) ConfigFile(com.google.copybara.config.ConfigFile) MigrationReference(com.google.copybara.Info.MigrationReference) ValidationException.checkCondition(com.google.copybara.exception.ValidationException.checkCondition) RepoException(com.google.copybara.exception.RepoException) FinishHookContext(com.google.copybara.feedback.FinishHookContext) Callable(java.util.concurrent.Callable) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) ChangesResponse(com.google.copybara.Origin.Reader.ChangesResponse) TokenType(com.google.copybara.templatetoken.Token.TokenType) ImmutableList(com.google.common.collect.ImmutableList) Path(java.nio.file.Path) Profiler(com.google.copybara.profiler.Profiler) Nullable(javax.annotation.Nullable) ImmutableSetMultimap(com.google.common.collect.ImmutableSetMultimap) CHANGE_REQUEST_FROM_SOT(com.google.copybara.WorkflowMode.CHANGE_REQUEST_FROM_SOT) Reader(com.google.copybara.Origin.Reader) ProfilerTask(com.google.copybara.profiler.Profiler.ProfilerTask) Identity(com.google.copybara.util.Identity) ImmutableMap(com.google.common.collect.ImmutableMap) Migration(com.google.copybara.config.Migration) Token(com.google.copybara.templatetoken.Token) Action(com.google.copybara.action.Action) MoreObjects(com.google.common.base.MoreObjects) ValidationException(com.google.copybara.exception.ValidationException) SkylarkConsole(com.google.copybara.transform.SkylarkConsole) Set(java.util.Set) LazyResourceLoader.memoized(com.google.copybara.LazyResourceLoader.memoized) Console(com.google.copybara.util.console.Console) IOException(java.io.IOException) EventMonitors(com.google.copybara.monitor.EventMonitor.EventMonitors) DestinationStatus(com.google.copybara.Destination.DestinationStatus) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) Consumer(java.util.function.Consumer) CHANGE_REQUEST(com.google.copybara.WorkflowMode.CHANGE_REQUEST) Authoring(com.google.copybara.authoring.Authoring) Glob(com.google.copybara.util.Glob) List(java.util.List) ChangeMigrationFinishedEvent(com.google.copybara.monitor.EventMonitor.ChangeMigrationFinishedEvent) Paths(java.nio.file.Paths) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) CommandLineException(com.google.copybara.exception.CommandLineException) ProfilerTask(com.google.copybara.profiler.Profiler.ProfilerTask) ImmutableList(com.google.common.collect.ImmutableList) CommandLineException(com.google.copybara.exception.CommandLineException) Nullable(javax.annotation.Nullable)

Aggregations

CommandLineException (com.google.copybara.exception.CommandLineException)6 Path (java.nio.file.Path)4 ImmutableMap (com.google.common.collect.ImmutableMap)2 RepoException (com.google.copybara.exception.RepoException)2 ValidationException (com.google.copybara.exception.ValidationException)2 IOException (java.io.IOException)2 JCommander (com.beust.jcommander.JCommander)1 ParameterException (com.beust.jcommander.ParameterException)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 MoreObjects (com.google.common.base.MoreObjects)1 Preconditions (com.google.common.base.Preconditions)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableSetMultimap (com.google.common.collect.ImmutableSetMultimap)1 DestinationStatus (com.google.copybara.Destination.DestinationStatus)1 Writer (com.google.copybara.Destination.Writer)1 MigrationReference (com.google.copybara.Info.MigrationReference)1 LazyResourceLoader.memoized (com.google.copybara.LazyResourceLoader.memoized)1 CommandWithArgs (com.google.copybara.MainArguments.CommandWithArgs)1 Reader (com.google.copybara.Origin.Reader)1 ChangesResponse (com.google.copybara.Origin.Reader.ChangesResponse)1