Skip to content

Commit 8b87036

Browse files
[git] populate Consul from a Git repository
1 parent f9703e3 commit 8b87036

58 files changed

Lines changed: 3905 additions & 178 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@ out/
1313
.settings
1414
.classpath
1515
.factorypath
16+
.tmp/
17+
**/application-local.yaml

build.gradle.kts

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ plugins {
55
jacoco
66
}
77

8-
version = "1.2.7"
8+
version = "1.3.0-SNAPSHOT"
99
group = "com.frogdevelopment.consul.populate"
1010

1111
repositories {
@@ -24,6 +24,8 @@ sonar {
2424
property("sonar.projectKey", "FrogDevelopment_consul-populate")
2525
property("sonar.organization", "frogdevelopment")
2626
property("sonar.junit.reportPaths", "build/test-results/test/")
27+
property("sonar.inclusions", "**/src/main/**/*")
28+
property("sonar.test.exclusions", "**/src/test/**/*")
2729
}
2830
}
2931

@@ -36,19 +38,3 @@ testing {
3638
}
3739
}
3840
}
39-
40-
tasks {
41-
test {
42-
finalizedBy(jacocoTestReport)
43-
}
44-
45-
jacocoTestReport {
46-
dependsOn(test)
47-
48-
reports {
49-
xml.required.set(true)
50-
html.required.set(false)
51-
}
52-
}
53-
}
54-
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
plugins {
2+
java
3+
jacoco
4+
}
5+
6+
tasks {
7+
test {
8+
finalizedBy(jacocoTestReport)
9+
}
10+
11+
jacocoTestReport {
12+
dependsOn(test)
13+
14+
reports {
15+
xml.required.set(true)
16+
html.required.set(false)
17+
}
18+
}
19+
}

consul-populate-cli/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
plugins {
22
id("io.micronaut.minimal.application") version "4.6.1"
33
id("com.frogdevelopment.jreleaser.publish-convention")
4+
id("com.frogdevelopment.jacoco")
45
alias(libs.plugins.jib)
56
alias(libs.plugins.shadow)
67
}

consul-populate-core/build.gradle.kts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
plugins {
22
id("io.micronaut.minimal.library") version "4.6.1"
33
id("com.frogdevelopment.jreleaser.publish-convention")
4+
id("com.frogdevelopment.jacoco")
45
}
56

67
micronaut {
@@ -17,7 +18,7 @@ dependencies {
1718
annotationProcessor(mn.micronaut.validation.processor)
1819

1920
implementation(mn.micronaut.jackson.databind)
20-
implementation(mn.micronaut.validation.asProvider())
21+
implementation(mn.micronaut.validation)
2122
implementation(libs.vertx.consul)
2223
implementation(libs.commons.io)
2324
implementation(mn.snakeyaml)

consul-populate-core/src/main/java/com/frogdevelopment/consul/populate/PopulateServiceImpl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ public void populate() {
4646
// Importing data from configured type
4747
final var configsToImport = dataImporter.execute()
4848
.entrySet()
49-
.stream().collect(Collectors.toMap(entry -> kvPath + entry.getKey(), Map.Entry::getValue));
49+
.stream()
50+
.collect(Collectors.toMap(entry -> kvPath + entry.getKey(), Map.Entry::getValue));
5051

5152
// Create/Update/Delete config
5253
final var txnRequest = new TxnRequest();
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.frogdevelopment.consul.populate.files;
2+
3+
import java.nio.file.Paths;
4+
5+
import com.fasterxml.jackson.databind.ObjectMapper;
6+
7+
import io.micronaut.context.annotation.Bean;
8+
import io.micronaut.context.annotation.Factory;
9+
import io.micronaut.context.annotation.Requires;
10+
11+
/**
12+
* Factory handling Files import based on the {@link ImportFileProperties.Format} defined in configuration.
13+
*/
14+
@Factory
15+
@Requires(property = "consul.files")
16+
public class FilesImportFactory {
17+
18+
@Bean
19+
@Requires(property = "consul.files.format", value = "JSON")
20+
JsonFilesImporter jsonFilesImporter(final ImportFileProperties importFileProperties, final ObjectMapper objectMapper) {
21+
final var rootPath = Paths.get(importFileProperties.getRootPath());
22+
final var targetPath = rootPath.resolve(importFileProperties.getTarget());
23+
return new JsonFilesImporter(rootPath, targetPath, objectMapper);
24+
}
25+
26+
@Bean
27+
@Requires(property = "consul.files.format", value = "PROPERTIES")
28+
PropertiesFilesImporter propertiesFilesImporter(final ImportFileProperties importFileProperties) {
29+
final var rootPath = Paths.get(importFileProperties.getRootPath());
30+
final var targetPath = rootPath.resolve(importFileProperties.getTarget());
31+
return new PropertiesFilesImporter(rootPath, targetPath);
32+
}
33+
34+
@Bean
35+
@Requires(property = "consul.files.format", value = "YAML")
36+
YamlFilesImporter yamlFilesImporter(final ImportFileProperties importFileProperties) {
37+
final var rootPath = Paths.get(importFileProperties.getRootPath());
38+
final var targetPath = rootPath.resolve(importFileProperties.getTarget());
39+
return new YamlFilesImporter(rootPath, targetPath);
40+
}
41+
}

consul-populate-core/src/main/java/com/frogdevelopment/consul/populate/files/FilesImporter.java

Lines changed: 27 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import java.io.File;
66
import java.io.IOException;
77
import java.nio.file.Path;
8-
import java.nio.file.Paths;
98
import java.util.HashMap;
109
import java.util.Map;
1110
import java.util.SequencedMap;
@@ -27,37 +26,42 @@
2726
* @since 1.0.0
2827
*/
2928
@RequiredArgsConstructor
30-
abstract sealed class FilesImporter implements DataImporter
29+
public abstract sealed class FilesImporter implements DataImporter
3130
permits JsonFilesImporter, PropertiesFilesImporter, YamlFilesImporter {
3231

3332
private static final Logger log = LoggerFactory.getLogger(FilesImporter.class);
34-
private final ImportFileProperties importProperties;
33+
34+
private final Path rootPath;
35+
private final Path targetPath;
3536

3637
@NonNull
3738
@Override
3839
public Map<String, String> execute() {
3940
// validate paths
40-
final var rootPath = Paths.get(importProperties.getRootPath());
4141
if (!rootPath.toFile().exists()) {
4242
throw new IllegalArgumentException("Root directory does not exist: " + rootPath);
4343
}
44-
final var targetPath = rootPath.resolve(importProperties.getTarget());
4544
if (!targetPath.toFile().exists()) {
4645
throw new IllegalArgumentException("Target directory does not exist: " + targetPath);
4746
}
4847

4948
// list files in root directory
50-
final var rootFiles = getRootFiles(rootPath);
49+
final var rootFiles = readFiles(rootPath);
5150

52-
// list files in target subdirectory
53-
final var targetFiles = getTargetFiles(targetPath);
51+
final Map<String, SequencedMap<String, Object>> finalFiles;
52+
if (rootPath.equals(targetPath)) {
53+
finalFiles = rootFiles;
54+
} else {
55+
// list files in target subdirectory
56+
final var targetFiles = readFiles(targetPath);
5457

55-
// for each target, merge in root if exists
56-
final var merged = MapHelper.merge(rootFiles, targetFiles);
58+
// for each target, merge in root if exists
59+
finalFiles = MapHelper.merge(rootFiles, targetFiles);
60+
}
5761

5862
try {
5963
final var result = new HashMap<String, String>();
60-
for (final var entry : merged.entrySet()) {
64+
for (final var entry : finalFiles.entrySet()) {
6165
final var key = FilenameUtils.removeExtension(entry.getKey());
6266
final var value = writeValueAsString(entry.getValue());
6367
if (value.isEmpty()) {
@@ -73,40 +77,24 @@ public Map<String, String> execute() {
7377
}
7478
}
7579

76-
private Map<String, SequencedMap<String, Object>> getRootFiles(final Path rootPath) {
77-
try {
78-
final var rootFiles = rootPath.toFile().listFiles(this::filterFile);
79-
if (ArrayUtils.isEmpty(rootFiles)) {
80-
throw new IllegalArgumentException("No configuration files found in root directory: " + rootPath);
81-
}
82-
final var rootFilesMap = new HashMap<String, SequencedMap<String, Object>>();
83-
for (final var rootFile : rootFiles) {
84-
rootFilesMap.put(rootFile.getName(), readFile(rootFile));
85-
}
86-
return rootFilesMap;
87-
} catch (final IOException e) {
88-
throw new IllegalStateException("Unable to read the root configuration. Please check the error logs", e);
89-
}
90-
}
91-
92-
private Map<String, SequencedMap<String, Object>> getTargetFiles(final Path targetPath) {
80+
private Map<String, SequencedMap<String, Object>> readFiles(final Path path) {
9381
try {
94-
final var targetFiles = targetPath.toFile().listFiles(this::filterFile);
95-
if (ArrayUtils.isEmpty(targetFiles)) {
96-
throw new IllegalArgumentException("No configuration files found in target directory: " + targetPath);
82+
final var files = path.toFile().listFiles(this::filterFile);
83+
if (ArrayUtils.isEmpty(files)) {
84+
throw new IllegalArgumentException("No configuration files found in directory: " + path);
9785
}
98-
final var targetFilesMap = new HashMap<String, SequencedMap<String, Object>>();
99-
for (final var targetFile : targetFiles) {
100-
final var target = readFile(targetFile);
101-
if (target == null) {
102-
log.warn("Content is null for file: {}", targetFile.getAbsolutePath());
86+
final var dataMap = new HashMap<String, SequencedMap<String, Object>>();
87+
for (final var file : files) {
88+
final var data = readFile(file);
89+
if (data == null) {
90+
log.warn("Content is null for file: {}", file.getAbsolutePath());
10391
continue;
10492
}
105-
targetFilesMap.put(targetFile.getName(), target);
93+
dataMap.put(file.getName(), data);
10694
}
107-
return targetFilesMap;
95+
return dataMap;
10896
} catch (final IOException e) {
109-
throw new IllegalStateException("Unable to read the target configurations. Please check the error logs", e);
97+
throw new IllegalStateException("Unable to read the configurations. Please check the error logs", e);
11098
}
11199
}
112100

consul-populate-core/src/main/java/com/frogdevelopment/consul/populate/files/JsonFilesImporter.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,16 @@
33
import java.io.File;
44
import java.io.IOException;
55
import java.nio.file.Files;
6+
import java.nio.file.Path;
67
import java.util.LinkedHashMap;
78
import java.util.List;
89
import java.util.Map;
910
import java.util.SequencedMap;
1011

11-
import jakarta.inject.Singleton;
12-
1312
import com.fasterxml.jackson.core.JsonProcessingException;
1413
import com.fasterxml.jackson.core.type.TypeReference;
1514
import com.fasterxml.jackson.databind.ObjectMapper;
1615

17-
import io.micronaut.context.annotation.Requires;
1816
import io.micronaut.core.annotation.NonNull;
1917
import io.micronaut.core.annotation.Nullable;
2018

@@ -25,8 +23,6 @@
2523
* @see ObjectMapper
2624
* @since 1.0.0
2725
*/
28-
@Singleton
29-
@Requires(property = "consul.files.format", value = "JSON")
3026
public final class JsonFilesImporter extends FilesImporter {
3127

3228
private static final List<String> EXTENSIONS = List.of("json");
@@ -39,8 +35,8 @@ public final class JsonFilesImporter extends FilesImporter {
3935
* @param importProperties Properties for the import
4036
* @param objectMapper ObjectMapper instance used for Json I/O
4137
*/
42-
public JsonFilesImporter(final ImportFileProperties importProperties, final ObjectMapper objectMapper) {
43-
super(importProperties);
38+
public JsonFilesImporter(final Path rootPath, final Path targetPath, final ObjectMapper objectMapper) {
39+
super(rootPath, targetPath);
4440
this.objectMapper = objectMapper;
4541
}
4642

consul-populate-core/src/main/java/com/frogdevelopment/consul/populate/files/PropertiesFilesImporter.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,14 @@
33
import java.io.File;
44
import java.io.IOException;
55
import java.nio.file.Files;
6+
import java.nio.file.Path;
67
import java.util.LinkedHashMap;
78
import java.util.List;
89
import java.util.Map;
910
import java.util.Properties;
1011
import java.util.SequencedMap;
1112
import java.util.stream.Collectors;
1213

13-
import jakarta.inject.Singleton;
14-
15-
import io.micronaut.context.annotation.Requires;
1614
import io.micronaut.core.annotation.NonNull;
1715
import io.micronaut.core.annotation.Nullable;
1816

@@ -23,8 +21,6 @@
2321
* @see Properties
2422
* @since 1.0.0
2523
*/
26-
@Singleton
27-
@Requires(property = "consul.files.format", value = "PROPERTIES")
2824
public final class PropertiesFilesImporter extends FilesImporter {
2925

3026
private static final List<String> EXTENSIONS = List.of("properties");
@@ -34,8 +30,8 @@ public final class PropertiesFilesImporter extends FilesImporter {
3430
*
3531
* @param importProperties Properties for the import
3632
*/
37-
public PropertiesFilesImporter(final ImportFileProperties importProperties) {
38-
super(importProperties);
33+
public PropertiesFilesImporter(final Path rootPath, final Path targetPath) {
34+
super(rootPath, targetPath);
3935
}
4036

4137
@Override

0 commit comments

Comments
 (0)