Skip to content

Commit

Permalink
#26 Added the new parameters to the CLI interface and refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
galovics committed Jun 14, 2020
1 parent ac658c5 commit 4aee2b8
Show file tree
Hide file tree
Showing 39 changed files with 1,178 additions and 154 deletions.
2 changes: 1 addition & 1 deletion swagger-brake-cli/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ dependencies {
}

jar {
enabled = false
enabled = true
manifest {
attributes 'Main-Class': 'io.redskap.swagger.brake.cli.SwaggerBrakeMain'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

import java.util.Collection;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import io.redskap.swagger.brake.cli.options.CliHelpException;
import io.redskap.swagger.brake.cli.options.CliOptions;
import io.redskap.swagger.brake.cli.options.CliOption;
import io.redskap.swagger.brake.cli.options.CliOptionsProvider;
import io.redskap.swagger.brake.core.BreakingChange;
import io.redskap.swagger.brake.runner.Options;
Expand All @@ -17,24 +16,35 @@
@Component
@RequiredArgsConstructor
@Slf4j
@SuppressFBWarnings("DM_EXIT")
public class Cli {
private final CliOptionsProvider optionsProvider;

public void start() {
/**
* Starts Swagger Brake and returns the exit code.
* <br>
* 0 exit code means no breaking changes were detected and the check was executed successfully.<br>
* 1 exit code means that breaking changes were found.<br>
* 2 exit code means that there was an error during the execution.<br>
* 3 exit code means that help was invoked.<br>
* @return the exit code
*/
public int start() {
try {
Options options = optionsProvider.provide();
Collection<BreakingChange> breakingChanges = Starter.start(options);
if (CollectionUtils.isNotEmpty(breakingChanges)) {
System.exit(1);
return 1;
} else {
return 0;
}
} catch (CliHelpException e) {
log.info(e.getMessage());
return 3;
} catch (Exception e) {
log.debug("Exception occured", e);
log.error(e.getMessage());
log.error("For help please use " + CliOptions.getAsCliOption(CliOptions.HELP));
System.exit(1);
log.error("For help please use " + CliOption.HELP.asCliOption());
return 2;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
package io.redskap.swagger.brake.cli;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.SimpleCommandLinePropertySource;

@SuppressFBWarnings("DM_EXIT")
public class SwaggerBrakeMain {
public static void main(String[] args) {
Cli cli = createCliInterface(args);
int exitCode = cli.start();
if (exitCode > 0) {
System.exit(exitCode);
}
}

public static Cli createCliInterface(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(CliConfiguration.class);
ConfigurableEnvironment environment = context.getEnvironment();
environment.getPropertySources().addFirst(new SimpleCommandLinePropertySource(args));
Cli cli = context.getBean(Cli.class);
cli.start();
return context.getBean(Cli.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,20 @@ public class CliHelpProvider {
public String getHelp() {
StringBuilder sb = new StringBuilder();
sb.append(format("Usage: java -jar swagger-brake.jar %s=/home/user/something.yaml %s=/home/user/something_v2.yaml",
formatCliOption(CliOptions.OLD_API_PATH), formatCliOption(CliOptions.NEW_API_PATH)));
CliOption.OLD_API_PATH.asCliOption(), CliOption.NEW_API_PATH.asCliOption()));
sb.append(lineSeparator());
sb.append(lineSeparator());
sb.append("Parameters:");
for (CliOptionHandler handler : optionHandlers) {
sb.append(lineSeparator());
sb.append("\t");
sb.append(format("%s %s", formatPadCliOption(handler.getHandledPropertyName()), handler.getHelpMessage()));
sb.append(format("%s %s", formatPadCliOption(handler.getHandledCliOption()), handler.getHelpMessage()));
}
return sb.toString();
}

private String formatPadCliOption(String option) {
return StringUtils.rightPad(formatCliOption(option), 40);
private String formatPadCliOption(CliOption option) {
return StringUtils.rightPad(option.asCliOption(), 40);
}

private String formatCliOption(String option) {
return CliOptions.getAsCliOption(option);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package io.redskap.swagger.brake.cli.options;

import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public enum CliOption {
OLD_API_PATH("old-api"),
NEW_API_PATH("new-api"),
HELP("help"),
VERBOSE("verbose"),

OUTPUT_FORMAT("output-format"),
OUTPUT_PATH("output-path"),

MAVEN_REPO_URL("maven-repo-url"),
MAVEN_SNAPSHOT_REPO_URL("maven-snapshot-repo-url"),
MAVEN_REPO_USERNAME("maven-repo-username"),
MAVEN_REPO_PASSWORD("maven-repo-password"),
ARTIFACT_ID("artifactId"),
GROUP_ID("groupId"),
CURRENT_ARTIFACT_VERSION("current-artifact-version"),

DEPRECATED_API_DELETION_ALLOWED("deprecated-api-deletion-allowed"),

API_FILENAME("api-filename"),
BETA_API_EXTENSION_NAME("beta-api-extension-name");


private final String cliOptionName;

public String asName() {
return cliOptionName;
}

public String asCliOption() {
return "--" + cliOptionName;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
public class CliOptionsProvider {
private final List<CliOptionHandler> handlers;
private final CliHelpProvider helpProvider;
private final CliOptionsValidator cliOptionsValidator;
private final Environment environment;

public Options provide() {
Expand All @@ -23,15 +24,16 @@ public Options provide() {
}
Options options = new Options();
for (CliOptionHandler handler : handlers) {
String handledPropertyName = handler.getHandledPropertyName();
String propertyValue = environment.getProperty(handledPropertyName);
log.debug("Handling command line argument {} with value of {}", handledPropertyName, propertyValue);
CliOption handledOption = handler.getHandledCliOption();
String propertyValue = environment.getProperty(handledOption.asName());
log.debug("Handling command line argument {} with value of {}", handledOption, propertyValue);
handler.handle(propertyValue, options);
}
cliOptionsValidator.validate(options);
return options;
}

private boolean isHelpRequired() {
return environment.getProperty(CliOptions.HELP) != null;
return environment.getProperty(CliOption.HELP.asName()) != null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package io.redskap.swagger.brake.cli.options;

import static io.redskap.swagger.brake.cli.options.CliOption.*;
import static java.lang.String.format;
import static java.util.stream.Collectors.toList;
import static org.apache.commons.lang3.StringUtils.isBlank;
import static org.apache.commons.lang3.StringUtils.isNotBlank;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

import com.google.common.collect.ImmutableMap;
import io.redskap.swagger.brake.runner.Options;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

@Component
public class CliOptionsValidator {
public static final Map<CliOption, Function<Options, String>> MAVEN_CONFIG_MAP = ImmutableMap.of(
MAVEN_REPO_URL, Options::getMavenRepoUrl,
MAVEN_SNAPSHOT_REPO_URL, Options::getMavenSnapshotRepoUrl,
CURRENT_ARTIFACT_VERSION, Options::getCurrentArtifactVersion,
GROUP_ID, Options::getGroupId,
ARTIFACT_ID, Options::getArtifactId
);

public void validate(Options options) {
if (isBlank(options.getNewApiPath())) {
throw new IllegalArgumentException(format("%s must be provided.", NEW_API_PATH.asCliOption()));
}
if (isNotBlank(options.getOldApiPath()) && isAnyMavenConfigurationSet(options)) {
throw new IllegalArgumentException(format("Maven configuration is detected along with %s. Please use only one of them.", OLD_API_PATH.asCliOption()));
}
if (isBlank(options.getOldApiPath()) && !isAnyMavenConfigurationSet(options)) {
throw new IllegalArgumentException(format("Either %s must be provided or a Maven configuration.", OLD_API_PATH.asCliOption()));
}
if (isAnyMavenConfigurationSet(options)) {
if (!isFullMavenConfigurationSet(options)) {
Collection<CliOption> missingMavenCliOptions = findMissingMavenConfiguration(options);
List<String> mavenCliOptionsFormatted = missingMavenCliOptions.stream().map(CliOption::asCliOption).collect(toList());
String joinedMavenCliOptions = StringUtils.join(mavenCliOptionsFormatted, ",");
throw new IllegalArgumentException(format("Partial Maven configuration detected, make sure you set %s as well.", joinedMavenCliOptions));
}
}
}

private boolean isFullMavenConfigurationSet(Options options) {
return findMissingMavenConfiguration(options).isEmpty();
}

private boolean isAnyMavenConfigurationSet(Options options) {
return findMissingMavenConfiguration(options).size() < MAVEN_CONFIG_MAP.size();
}

private Collection<CliOption> findMissingMavenConfiguration(Options options) {
return MAVEN_CONFIG_MAP.entrySet().stream()
.filter(e -> isBlank(e.getValue().apply(options)))
.map(Map.Entry::getKey)
.collect(toList());
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package io.redskap.swagger.brake.cli.options.handler;

import io.redskap.swagger.brake.cli.options.CliOptions;
import io.redskap.swagger.brake.cli.options.CliOption;
import io.redskap.swagger.brake.runner.Options;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

@Component
public class ApiFilenameHandler implements CliOptionHandler {
@Override
public void handle(String propertyValue, Options options) {
if (StringUtils.isNotBlank(propertyValue)) {
options.setApiFilename(propertyValue);
public void handle(String optionValue, Options options) {
if (StringUtils.isNotBlank(optionValue)) {
options.setApiFilename(optionValue);
}
}

@Override
public String getHandledPropertyName() {
return CliOptions.API_FILENAME;
public CliOption getHandledCliOption() {
return CliOption.API_FILENAME;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package io.redskap.swagger.brake.cli.options.handler;

import io.redskap.swagger.brake.cli.options.CliOptions;
import io.redskap.swagger.brake.cli.options.CliOption;
import io.redskap.swagger.brake.runner.Options;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

@Component
public class ArtifactIdHandler implements CliOptionHandler {
@Override
public void handle(String propertyValue, Options options) {
if (StringUtils.isNotBlank(propertyValue)) {
options.setArtifactId(propertyValue);
public void handle(String optionValue, Options options) {
if (StringUtils.isNotBlank(optionValue)) {
options.setArtifactId(optionValue);
}
}

@Override
public String getHandledPropertyName() {
return CliOptions.ARTIFACT_ID;
public CliOption getHandledCliOption() {
return CliOption.ARTIFACT_ID;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package io.redskap.swagger.brake.cli.options.handler;

import io.redskap.swagger.brake.cli.options.CliOptions;
import io.redskap.swagger.brake.cli.options.CliOption;
import io.redskap.swagger.brake.runner.Options;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

@Component
public class BetaApiExtensionNameHandler implements CliOptionHandler {
@Override
public void handle(String propertyValue, Options options) {
if (StringUtils.isNotBlank(propertyValue)) {
options.setBetaApiExtensionName(propertyValue);
public void handle(String optionValue, Options options) {
if (StringUtils.isNotBlank(optionValue)) {
options.setBetaApiExtensionName(optionValue);
}
}

@Override
public String getHandledPropertyName() {
return CliOptions.BETA_API_EXTENSION_NAME;
public CliOption getHandledCliOption() {
return CliOption.BETA_API_EXTENSION_NAME;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package io.redskap.swagger.brake.cli.options.handler;

import io.redskap.swagger.brake.cli.options.CliOption;
import io.redskap.swagger.brake.runner.Options;

public interface CliOptionHandler {
void handle(String propertyValue, Options options);
void handle(String optionValue, Options options);

String getHandledPropertyName();
CliOption getHandledCliOption();

String getHelpMessage();
}
Loading

0 comments on commit 4aee2b8

Please sign in to comment.