Search in sources :

Example 1 with CliException

use of com.commercetools.project.sync.exception.CliException in project commercetools-project-sync by commercetools.

the class CliRunner method processCliArguments.

private static CompletionStage<Void> processCliArguments(@Nonnull final CommandLine commandLine, @Nonnull final Options cliOptions, @Nonnull final SyncerFactory syncerFactory) {
    final Option[] options = commandLine.getOptions();
    if (options.length == 0) {
        return exceptionallyCompletedFuture(new CliException("Please pass at least 1 option to the CLI."));
    } else {
        final Option option = Arrays.stream(options).filter(o -> !SYNC_PROJECT_SYNC_CUSTOM_OBJECTS_OPTION_LONG.equals(o.getLongOpt())).findAny().orElse(null);
        if (option == null) {
            return exceptionallyCompletedFuture(new CliException(format("Please pass at least 1 more option other than %s to the CLI.", SYNC_PROJECT_SYNC_CUSTOM_OBJECTS_OPTION_LONG)));
        }
        final String optionName = option.getOpt();
        CompletionStage<Void> resultCompletionStage = CompletableFuture.completedFuture(null);
        if (SYNC_MODULE_OPTION_SHORT.equalsIgnoreCase(optionName)) {
            resultCompletionStage = processSyncOptionAndExecute(commandLine, syncerFactory);
        }
        if (HELP_OPTION_SHORT.equalsIgnoreCase(optionName)) {
            printHelpToStdOut(cliOptions);
        }
        if (VERSION_OPTION_SHORT.equalsIgnoreCase(optionName)) {
            printApplicationVersion();
        }
        return resultCompletionStage;
    }
}
Also used : CliException(com.commercetools.project.sync.exception.CliException) ProductSyncCustomRequest.parseProductQueryParametersOption(com.commercetools.project.sync.model.ProductSyncCustomRequest.parseProductQueryParametersOption) Option(org.apache.commons.cli.Option)

Example 2 with CliException

use of com.commercetools.project.sync.exception.CliException in project commercetools-project-sync by commercetools.

the class CliRunner method processSyncOptionAndExecute.

@Nonnull
private static CompletionStage<Void> processSyncOptionAndExecute(@Nonnull final CommandLine commandLine, @Nonnull final SyncerFactory syncerFactory) {
    final String[] syncOptionValues = commandLine.getOptionValues(SYNC_MODULE_OPTION_SHORT);
    final String runnerNameValue = commandLine.getOptionValue(RUNNER_NAME_OPTION_SHORT);
    final boolean isFullSync = commandLine.hasOption(FULL_SYNC_OPTION_SHORT);
    final boolean isSyncProjectSyncCustomObjects = commandLine.hasOption(SYNC_PROJECT_SYNC_CUSTOM_OBJECTS_OPTION_LONG);
    final boolean isProductQueryParametersOptionPresent = commandLine.hasOption(PRODUCT_QUERY_PARAMETERS_OPTION);
    final ProductSyncCustomRequest productSyncCustomRequest;
    try {
        productSyncCustomRequest = isProductQueryParametersOptionPresent ? parseProductQueryParametersOption(commandLine.getOptionValue(PRODUCT_QUERY_PARAMETERS_OPTION)) : null;
    } catch (CliException e) {
        return exceptionallyCompletedFuture(e);
    }
    return syncerFactory.sync(syncOptionValues, runnerNameValue, isFullSync, isSyncProjectSyncCustomObjects, productSyncCustomRequest);
}
Also used : CliException(com.commercetools.project.sync.exception.CliException) ProductSyncCustomRequest(com.commercetools.project.sync.model.ProductSyncCustomRequest) Nonnull(javax.annotation.Nonnull)

Example 3 with CliException

use of com.commercetools.project.sync.exception.CliException in project commercetools-project-sync by commercetools.

the class ProductSyncCustomRequest method parseProductQueryParametersOption.

public static ProductSyncCustomRequest parseProductQueryParametersOption(String customRequest) {
    final ObjectMapper objectMapper = new ObjectMapper();
    final ProductSyncCustomRequest productSyncCustomRequest;
    try {
        productSyncCustomRequest = objectMapper.readValue(customRequest, ProductSyncCustomRequest.class);
    } catch (IOException | IllegalArgumentException e) {
        throw new CliException(e.getMessage());
    }
    return productSyncCustomRequest;
}
Also used : CliException(com.commercetools.project.sync.exception.CliException) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

CliException (com.commercetools.project.sync.exception.CliException)3 ProductSyncCustomRequest (com.commercetools.project.sync.model.ProductSyncCustomRequest)1 ProductSyncCustomRequest.parseProductQueryParametersOption (com.commercetools.project.sync.model.ProductSyncCustomRequest.parseProductQueryParametersOption)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 IOException (java.io.IOException)1 Nonnull (javax.annotation.Nonnull)1 Option (org.apache.commons.cli.Option)1