Skip to content

Commit

Permalink
Merge pull request #20913 from ebullient/jsonParsing
Browse files Browse the repository at this point in the history
DevTools: consolidate json/yaml parsing
  • Loading branch information
aloubyansky authored Nov 20, 2021
2 parents 5147461 + ee98586 commit 8d501fc
Show file tree
Hide file tree
Showing 123 changed files with 16,073 additions and 2,712 deletions.
7 changes: 0 additions & 7 deletions devtools/cli/src/main/java/io/quarkus/cli/QuarkusCli.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

import io.quarkus.cli.common.OutputOptionMixin;
import io.quarkus.cli.common.PropertiesOptions;
import io.quarkus.registry.config.RegistriesConfigLocator;
import io.quarkus.runtime.QuarkusApplication;
import picocli.CommandLine;
import picocli.CommandLine.Help;
Expand Down Expand Up @@ -43,12 +42,6 @@ public class QuarkusCli implements QuarkusApplication, Callable<Integer> {
@CommandLine.ArgGroup(exclusive = false, validate = false)
protected PropertiesOptions propertiesOptions = new PropertiesOptions();

@CommandLine.Option(names = {
"--tools-config" }, description = "Quarkus Tools configuration file", hidden = true, scope = CommandLine.ScopeType.INHERIT)
void setToolsConfig(String toolsConfig) {
System.setProperty(RegistriesConfigLocator.CONFIG_FILE_PATH_PROPERTY, toolsConfig);
}

@Override
public int run(String... args) throws Exception {
CommandLine cmd = factory == null ? new CommandLine(this) : new CommandLine(this, factory);
Expand Down
59 changes: 24 additions & 35 deletions devtools/cli/src/main/java/io/quarkus/cli/RegistryAddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,68 +3,57 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.List;

import io.quarkus.cli.registry.BaseRegistryCommand;
import io.quarkus.registry.config.RegistriesConfig;
import io.quarkus.registry.config.RegistriesConfigLocator;
import io.quarkus.registry.config.RegistryConfig;
import io.quarkus.registry.config.json.JsonRegistriesConfig;
import io.quarkus.registry.config.json.JsonRegistryConfig;
import io.quarkus.registry.config.json.RegistriesConfigMapperHelper;
import picocli.CommandLine;

@CommandLine.Command(name = "add", sortOptions = false, showDefaultValues = true, mixinStandardHelpOptions = false, header = "Add a Quarkus extension registry", description = "%n"
+ "This command will add a Quarkus extension registry to the registry client configuration unless it's already present.", headerHeading = "%n", commandListHeading = "%nCommands:%n", synopsisHeading = "%nUsage: ", parameterListHeading = "%n", optionListHeading = "Options:%n")
public class RegistryAddCommand extends BaseRegistryCommand {

@CommandLine.Parameters(arity = "0..1", paramLabel = "REGISTRY-ID[,REGISTRY-ID]", description = "Registry ID to add to the registry client configuration%n"
@CommandLine.Parameters(arity = "1..*", split = ",", paramLabel = "REGISTRY-ID[,REGISTRY-ID]", description = "Registry ID to add to the registry client configuration%n"
+ " Example:%n"
+ " registry.quarkus.io%n"
+ " registry.quarkus.acme.com,registry.quarkus.io%n")
String registryIds;
List<String> registryIds;

@Override
public Integer call() throws Exception {
boolean existingConfig = false;
Path configYaml = null;

registryClient.refreshRegistryCache(output);

Path configYaml;
if (registryClient.getConfigArg() == null) {
configYaml = RegistriesConfigLocator.locateConfigYaml();
if (configYaml == null) {
configYaml = RegistriesConfigLocator.getDefaultConfigYamlLocation();
}
} else {
// If a configuration was specified, check if it exists
if (registryClient.getConfigArg() != null) {
configYaml = Paths.get(registryClient.getConfigArg());
existingConfig = Files.exists(configYaml);
}

final RegistriesConfig config;
if (Files.exists(configYaml)) {
config = RegistriesConfigMapperHelper.deserialize(configYaml, JsonRegistriesConfig.class);
final RegistriesConfig.Mutable config;
if (existingConfig) {
registryClient.refreshRegistryCache(output);
config = registryClient.resolveConfig().mutable();
if (config.getSource().getFilePath() == null) {
output.error("Can only modify file-based configuration. Config source is " + config.getSource().describe());
return CommandLine.ExitCode.SOFTWARE;
}
} else {
config = new JsonRegistriesConfig();
config = RegistriesConfig.builder();
}

final Set<String> existingIds = config.getRegistries().stream().map(RegistryConfig::getId).collect(Collectors.toSet());
boolean persist = false;
for (String registryId : registryIds.split(",")) {
if (existingIds.add(registryId)) {
persist = true;
final JsonRegistryConfig registry = new JsonRegistryConfig();
registry.setId(registryId);
config.getRegistries().add(registry);
output.info("Registry " + registryId + " was added");
} else {
output.info("Registry " + registryId + " was skipped since it is already present");
}
for (String registryId : registryIds) {
persist |= config.addRegistry(registryId);
}

if (persist) {
RegistriesConfigMapperHelper.serialize(config, configYaml);
if (existingConfig) {
config.persist();
} else {
config.persist(configYaml);
}
}

return CommandLine.ExitCode.OK;
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package io.quarkus.cli;

import java.nio.file.Path;

import io.quarkus.cli.registry.BaseRegistryCommand;
import io.quarkus.registry.ExtensionCatalogResolver;
import io.quarkus.registry.catalog.Platform;
import io.quarkus.registry.catalog.PlatformCatalog;
import io.quarkus.registry.catalog.PlatformStream;
import io.quarkus.registry.config.RegistriesConfig;
import io.quarkus.registry.config.RegistriesConfigLocator;
import io.quarkus.registry.config.RegistryConfig;
import picocli.CommandLine;

Expand Down Expand Up @@ -51,10 +48,7 @@ public Integer call() throws Exception {
}
}

final Path configYaml = RegistriesConfigLocator.locateConfigYaml();
if (configYaml != null) {
output.info("(Read from " + configYaml + ")");
}
output.info("(Config source: " + config.getSource().describe() + ")");

return CommandLine.ExitCode.OK;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,65 +1,57 @@
package io.quarkus.cli;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.List;

import io.quarkus.cli.registry.BaseRegistryCommand;
import io.quarkus.registry.config.RegistriesConfig;
import io.quarkus.registry.config.RegistriesConfigLocator;
import io.quarkus.registry.config.RegistryConfig;
import io.quarkus.registry.config.json.JsonRegistriesConfig;
import io.quarkus.registry.config.json.RegistriesConfigMapperHelper;
import picocli.CommandLine;

@CommandLine.Command(name = "remove", sortOptions = false, showDefaultValues = true, mixinStandardHelpOptions = false, header = "Remove a Quarkus extension registry", description = "%n"
+ "This command will remove a Quarkus extension registry from the registry client configuration.", headerHeading = "%n", commandListHeading = "%nCommands:%n", synopsisHeading = "%nUsage: ", parameterListHeading = "%n", optionListHeading = "Options:%n")
public class RegistryRemoveCommand extends BaseRegistryCommand {

@CommandLine.Parameters(arity = "0..1", paramLabel = "REGISTRY-ID[,REGISTRY-ID]", description = "Registry ID to remove from the registry client configuration%n"
@CommandLine.Parameters(arity = "1..*", split = ",", paramLabel = "REGISTRY-ID[,REGISTRY-ID]", description = "Registry ID to remove from the registry client configuration%n"
+ " Example:%n"
+ " registry.quarkus.io%n"
+ " registry.quarkus.acme.com,registry.quarkus.io%n")
String registryIds;
List<String> registryIds;

@Override
public Integer call() throws Exception {
boolean existingConfig = false;
Path configYaml = null;

registryClient.refreshRegistryCache(output);
// If a configuration was specified, check if it exists
if (registryClient.getConfigArg() != null) {
configYaml = Paths.get(registryClient.getConfigArg());
existingConfig = Files.exists(configYaml);
}

Path configYaml;
if (registryClient.getConfigArg() == null) {
configYaml = RegistriesConfigLocator.locateConfigYaml();
if (configYaml == null) {
output.error("Failed to locate the registry client configuration file");
final RegistriesConfig.Mutable config;
if (existingConfig) {
registryClient.refreshRegistryCache(output);
config = registryClient.resolveConfig().mutable();
if (config.getSource().getFilePath() == null) {
output.error("Can only modify file-based configuration. Config source is " + config.getSource().describe());
return CommandLine.ExitCode.SOFTWARE;
}
} else {
configYaml = Paths.get(registryClient.getConfigArg());
output.error("Can only remove registries from an existing configuration. The specified config file does not exist: "
+ configYaml);
return CommandLine.ExitCode.SOFTWARE;
}

final RegistriesConfig config = RegistriesConfigMapperHelper.deserialize(configYaml, JsonRegistriesConfig.class);

final Map<String, RegistryConfig> registries = new LinkedHashMap<>(config.getRegistries().size());
config.getRegistries().forEach(r -> registries.put(r.getId(), r));
boolean persist = false;
for (String registryId : registryIds.split(",")) {
if (registries.remove(registryId) == null) {
output.info("Registry " + registryId + " was not previously configured");
} else {
output.info("Registry " + registryId + " was removed");
persist = true;
}
for (String registryId : registryIds) {
persist |= config.removeRegistry(registryId);
}

if (persist) {
final JsonRegistriesConfig jsonConfig = new JsonRegistriesConfig();
jsonConfig.setRegistries(new ArrayList<>(registries.values()));
RegistriesConfigMapperHelper.serialize(jsonConfig, configYaml);
config.persist();
}

return CommandLine.ExitCode.OK;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@
io.quarkus.registry.catalog.Extension.class,
io.quarkus.registry.catalog.ExtensionCatalog.class,
io.quarkus.registry.catalog.ExtensionOrigin.class,
io.quarkus.registry.catalog.json.JsonArtifactCoordsDeserializer.class,
io.quarkus.registry.catalog.json.JsonCategory.class,
io.quarkus.registry.catalog.json.JsonExtension.class,
io.quarkus.registry.catalog.json.JsonExtensionCatalog.class,
io.quarkus.registry.catalog.json.JsonExtensionOrigin.class,
org.apache.maven.repository.internal.DefaultArtifactDescriptorReader.class,
org.apache.maven.repository.internal.DefaultVersionRangeResolver.class,
org.apache.maven.repository.internal.DefaultVersionResolver.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import io.quarkus.registry.RegistryResolutionException;
import io.quarkus.registry.catalog.ExtensionCatalog;
import io.quarkus.registry.config.RegistriesConfig;
import io.quarkus.registry.config.RegistriesConfigLocator;
import picocli.CommandLine;

public class RegistryClientMixin {
Expand All @@ -42,8 +41,10 @@ public String getConfigArg() {
return config;
}

public RegistriesConfig resolveConfig() {
return config == null ? RegistriesConfigLocator.resolveConfig() : RegistriesConfigLocator.load(Paths.get(config));
public RegistriesConfig resolveConfig() throws RegistryResolutionException {
return config == null
? RegistriesConfig.resolveConfig()
: RegistriesConfig.resolveFromFile(Paths.get(config));
}

public QuarkusProject createQuarkusProject(Path projectRoot, TargetQuarkusVersionGroup targetVersion, BuildTool buildTool,
Expand Down
27 changes: 12 additions & 15 deletions devtools/cli/src/test/java/io/quarkus/cli/CliNonProjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@

import io.quarkus.devtools.testing.RegistryClientTestHelper;
import io.quarkus.registry.config.RegistriesConfig;
import io.quarkus.registry.config.RegistriesConfigLocator;
import io.quarkus.registry.config.json.JsonRegistriesConfig;
import io.quarkus.registry.config.json.RegistriesConfigMapperHelper;
import picocli.CommandLine;

public class CliNonProjectTest {
Expand Down Expand Up @@ -149,6 +146,7 @@ public void testRegistryStreams() throws Exception {
result = CliDriver.execute(workspaceRoot, "registry", "--streams", "-e");
Assertions.assertEquals(CommandLine.ExitCode.OK, result.exitCode,
"Expected OK return code." + result);

try (BufferedReader reader = new BufferedReader(new StringReader(result.stdout))) {
String line = reader.readLine();
while (line != null && !TEST_QUARKUS_REGISTRY.equals(line)) {
Expand All @@ -164,14 +162,13 @@ public void testRegistryStreams() throws Exception {

line = reader.readLine();
Assertions.assertNotNull(line);
Assertions.assertTrue(line.startsWith("(Read from "), "Expected (Read from ...");
Assertions.assertTrue(line.startsWith("(Config source:"), "Expected (Config source: ...");
Assertions.assertNull(reader.readLine(), "No further content expected");
}
}

@Test
public void testRegistryRefresh() throws Exception {

CliDriver.Result result;

// refresh the local cache and list the registries
Expand All @@ -181,7 +178,7 @@ public void testRegistryRefresh() throws Exception {

Path configPath = resolveConfigPath("enabledConfig.yml");
result = CliDriver.execute(workspaceRoot, "registry", "--refresh", "-e",
"--tools-config", configPath.toAbsolutePath().toString());
"--config", configPath.toAbsolutePath().toString());
Assertions.assertEquals(CommandLine.ExitCode.OK, result.exitCode,
"Expected OK return code." + result);
Assertions.assertTrue(result.stdout.contains(configPath.toString()),
Expand All @@ -193,7 +190,7 @@ public void testRegistryRefresh() throws Exception {

configPath = resolveConfigPath("disabledConfig.yml");
result = CliDriver.execute(workspaceRoot, "registry", "--refresh", "-e",
"--tools-config", configPath.toAbsolutePath().toString());
"--config", configPath.toAbsolutePath().toString());
Assertions.assertEquals(CommandLine.ExitCode.OK, result.exitCode,
"Expected OK return code." + result);
Assertions.assertTrue(result.stdout.contains(configPath.toString()),
Expand All @@ -206,7 +203,6 @@ public void testRegistryRefresh() throws Exception {

@Test
public void testRegistryAddRemove() throws Exception {

CliDriver.Result result;

final Path testConfigYaml = workspaceRoot.resolve("test-registry-add-remove.yaml").toAbsolutePath();
Expand All @@ -218,7 +214,7 @@ public void testRegistryAddRemove() throws Exception {
"Expected OK return code." + result);

assertThat(testConfigYaml).exists();
RegistriesConfig testConfig = RegistriesConfigLocator.load(testConfigYaml);
RegistriesConfig testConfig = RegistriesConfig.fromFile(testConfigYaml);
assertThat(testConfig.getRegistries()).hasSize(2);
assertThat(testConfig.getRegistries().get(0).getId()).isEqualTo("one");
assertThat(testConfig.getRegistries().get(1).getId()).isEqualTo("two");
Expand All @@ -227,7 +223,7 @@ public void testRegistryAddRemove() throws Exception {
Assertions.assertEquals(CommandLine.ExitCode.OK, result.exitCode,
"Expected OK return code." + result);

testConfig = RegistriesConfigLocator.load(testConfigYaml);
testConfig = RegistriesConfig.fromFile(testConfigYaml);
assertThat(testConfig.getRegistries()).hasSize(3);
assertThat(testConfig.getRegistries().get(0).getId()).isEqualTo("one");
assertThat(testConfig.getRegistries().get(1).getId()).isEqualTo("two");
Expand All @@ -237,15 +233,15 @@ public void testRegistryAddRemove() throws Exception {
Assertions.assertEquals(CommandLine.ExitCode.OK, result.exitCode,
"Expected OK return code." + result);

testConfig = RegistriesConfigLocator.load(testConfigYaml);
testConfig = RegistriesConfig.fromFile(testConfigYaml);
assertThat(testConfig.getRegistries()).hasSize(1);
assertThat(testConfig.getRegistries().get(0).getId()).isEqualTo("three");

result = CliDriver.execute(workspaceRoot, "registry", "add", "four", "--config", testConfigYaml.toString());
Assertions.assertEquals(CommandLine.ExitCode.OK, result.exitCode,
"Expected OK return code." + result);

testConfig = RegistriesConfigLocator.load(testConfigYaml);
testConfig = RegistriesConfig.fromFile(testConfigYaml);
assertThat(testConfig.getRegistries()).hasSize(2);
assertThat(testConfig.getRegistries().get(0).getId()).isEqualTo("three");
assertThat(testConfig.getRegistries().get(1).getId()).isEqualTo("four");
Expand All @@ -255,10 +251,11 @@ public void testRegistryAddRemove() throws Exception {
Assertions.assertEquals(CommandLine.ExitCode.OK, result.exitCode,
"Expected OK return code." + result);

testConfig = RegistriesConfigMapperHelper.deserialize(testConfigYaml, JsonRegistriesConfig.class);
assertThat(testConfig.getRegistries()).isEmpty();
String contents = Files.readString(testConfigYaml);
System.out.println(contents);
assertThat(contents).isEqualTo("---\n");

testConfig = RegistriesConfigLocator.load(testConfigYaml);
testConfig = RegistriesConfig.fromFile(testConfigYaml);
assertThat(testConfig.getRegistries()).hasSize(1);
assertThat(testConfig.getRegistries().get(0).getId()).isEqualTo("registry.quarkus.io");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public void testCreateAppOverrides() throws Exception {
"--no-wrapper", "--package-name=custom.pkg",
"--output-directory=" + nested,
"--app-config=" + String.join(",", configs),
"-x resteasy-reactive,micrometer",
"-x resteasy-reactive,micrometer-registry-prometheus",
"silly:my-project:0.1.0");

Assertions.assertEquals(CommandLine.ExitCode.OK, result.exitCode, "Expected OK return code." + result);
Expand Down
Loading

0 comments on commit 8d501fc

Please sign in to comment.