Skip to content

Commit

Permalink
Add ability to disable automatically loading config
Browse files Browse the repository at this point in the history
Some applications might not want to automatically load a
detected smithy-build.json file. For example, if you're validating
that a JAR built from a package correctly defines all of its
dependencies correctly, then loading "sources" alongiside this
JAR can lead to conflicts. --no-config can be passed to disable
automatically loading a detected smithy-build.json file. Passing
this option and a -c parameter will fail validation.
  • Loading branch information
mtdowling committed Dec 14, 2022
1 parent 0f720f9 commit 87fb739
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,25 @@ public void successfullyDeDupesConfigAndCliArguments() {
assertThat(result.getExitCode(), equalTo(0));
});
}

@Test
public void canDisableConfigFileDetection() {
// Disable the config file detection and don't pass model via positional arguments.
// This causes main.smithy to not be loaded.
IntegUtils.run("simple-config-sources", ListUtils.of("build", "--no-config"), result -> {
assertThat(result.getExitCode(), equalTo(0));
assertThat(result.hasArtifact("source", "sources", "main.smithy"), is(false));
});
}

@Test
public void failsWhenConfigIsProvidedAndDisabled() {
// Disable the config file detection and don't pass model via positional arguments.
// This causes main.smithy to not be loaded.
IntegUtils.run("simple-config-sources", ListUtils.of("build", "--no-config", "-c", "smithy-build.json"),
result -> {
assertThat(result.getExitCode(), equalTo(1));
assertThat(result.getOutput(), containsString("Invalid combination of --no-config and --config"));
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,21 @@
import software.amazon.smithy.build.SmithyBuild;
import software.amazon.smithy.build.model.SmithyBuildConfig;
import software.amazon.smithy.cli.ArgumentReceiver;
import software.amazon.smithy.cli.CliError;
import software.amazon.smithy.cli.HelpPrinter;

final class ConfigOptions implements ArgumentReceiver {

private static final Logger LOGGER = Logger.getLogger(ConfigOptions.class.getName());
private final List<String> config = new ArrayList<>();
private boolean noConfig = false;

@Override
public void registerHelp(HelpPrinter printer) {
printer.param("--config", "-c", "CONFIG_PATH...",
"Path to smithy-build.json configuration (defaults to './smithy-build.json'). "
+ "This option can be repeated and each configured will be merged.");
"Path to smithy-build.json config (defaults to ./smithy-build.json if not specified). "
+ "This option can be repeated, merging each config file.");
printer.option("--no-config", null, "Disable config file detection and use.");
}

@Override
Expand All @@ -52,9 +55,21 @@ public Consumer<String> testParameter(String name) {
}
}

@Override
public boolean testOption(String name) {
if (name.equals("--no-config")) {
noConfig = true;
return true;
} else {
return false;
}
}

List<String> config() {
List<String> config = this.config;
if (config.isEmpty()) {

// Don't find the default config if --no-config is passed.
if (config.isEmpty() && !noConfig) {
Path defaultConfig = Paths.get("smithy-build.json").toAbsolutePath();
if (Files.exists(defaultConfig)) {
LOGGER.fine("Detected smithy-build.json at " + defaultConfig);
Expand All @@ -69,6 +84,11 @@ SmithyBuildConfig createSmithyBuildConfig() {
SmithyBuildConfig smithyBuildConfig;
List<String> config = config();

if (noConfig && !config.isEmpty()) {
throw new CliError("Invalid combination of --no-config and --config. --no-config can be omitted because "
+ "providing --config/-c disables automatically loading ./smithy-build.json.");
}

if (config.isEmpty()) {
smithyBuildConfig = SmithyBuildConfig.builder().version(SmithyBuild.VERSION).build();
} else {
Expand Down

0 comments on commit 87fb739

Please sign in to comment.