Skip to content

Commit 8084f15

Browse files
[git] split in more classes
1 parent fb1013d commit 8084f15

10 files changed

Lines changed: 284 additions & 141 deletions

File tree

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.frogdevelopment.consul.populate.git;
2+
3+
import lombok.RequiredArgsConstructor;
4+
import lombok.extern.slf4j.Slf4j;
5+
6+
import java.io.IOException;
7+
8+
import jakarta.inject.Singleton;
9+
10+
import org.apache.commons.io.FileUtils;
11+
12+
import io.micronaut.context.annotation.Requires;
13+
import io.micronaut.core.util.StringUtils;
14+
import io.micronaut.runtime.event.annotation.EventListener;
15+
import io.micronaut.runtime.server.event.ServerShutdownEvent;
16+
17+
/**
18+
* Optional cleanup handler that deletes the cloned git repository on server shutdown.
19+
* Only active when {@code consul.git.clean-up=true} is configured.
20+
*
21+
* @author Le Gall Benoît
22+
* @since 1.2.0
23+
*/
24+
@Slf4j
25+
@Singleton
26+
@RequiredArgsConstructor
27+
@Requires(property = "consul.git.clean-up", value = StringUtils.TRUE, defaultValue = StringUtils.FALSE)
28+
public class GitCleanUp {
29+
30+
/** Provides the cloned git repository directory path */
31+
private final RepositoryDirectoryProvider repositoryDirectoryProvider;
32+
33+
/**
34+
* Deletes the cloned git repository directory on server shutdown.
35+
*
36+
* @param ignored the server shutdown event
37+
*/
38+
@EventListener
39+
public void onServerShutdownEvent(final ServerShutdownEvent ignored) {
40+
try {
41+
final var repositoryDirectory = repositoryDirectoryProvider.getRepository();
42+
log.info("Deleting Git Repository {}...", repositoryDirectory);
43+
FileUtils.deleteDirectory(repositoryDirectory.toFile());
44+
} catch (final IOException e) {
45+
log.error("Failed to delete repository", e);
46+
}
47+
}
48+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.frogdevelopment.consul.populate.git;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
5+
import jakarta.inject.Singleton;
6+
7+
import org.eclipse.jgit.api.Git;
8+
import org.eclipse.jgit.api.errors.GitAPIException;
9+
import org.eclipse.jgit.lib.Constants;
10+
import org.eclipse.jgit.transport.CredentialsProvider;
11+
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
12+
13+
import io.micronaut.context.annotation.Bean;
14+
import io.micronaut.context.annotation.Factory;
15+
import io.micronaut.core.util.StringUtils;
16+
17+
/**
18+
* Factory for creating and configuring the {@link Git} instance.
19+
* Clones the repository on bean creation and automatically closes it on shutdown.
20+
*
21+
* @author Le Gall Benoît
22+
* @since 1.2.0
23+
*/
24+
@Slf4j
25+
@Factory
26+
public class GitFactory {
27+
28+
/**
29+
* Creates a Git instance by cloning the configured repository.
30+
* The instance is automatically closed on application shutdown via {@link Bean#preDestroy()}.
31+
*
32+
* @param gitProperties git configuration properties
33+
* @param repositoryDirectoryProvider provides the target directory for cloning
34+
* @return configured Git instance connected to the cloned repository
35+
* @throws GitAPIException if cloning fails
36+
*/
37+
@Singleton
38+
@Bean(preDestroy = "close")
39+
public Git git(final GitProperties gitProperties,
40+
final RepositoryDirectoryProvider repositoryDirectoryProvider) throws GitAPIException {
41+
final var credentialsProvider = createCredentialsProvider(gitProperties);
42+
// CredentialsProvider.setDefault(credentialsProvider);
43+
final var repositoryDirectory = repositoryDirectoryProvider.getRepository();
44+
45+
log.debug("Cloning repository [{}]", gitProperties.getUri());
46+
return Git.cloneRepository()
47+
.setURI(gitProperties.getUri())
48+
.setDirectory(repositoryDirectory.toFile())
49+
.setGitDir(repositoryDirectory.resolve(Constants.DOT_GIT).toFile())
50+
.setBranch(gitProperties.getBranch())
51+
.setRemote(Constants.DEFAULT_REMOTE_NAME)
52+
.setCredentialsProvider(credentialsProvider)
53+
.call();
54+
}
55+
56+
private static CredentialsProvider createCredentialsProvider(final GitProperties gitProperties) {
57+
final var token = gitProperties.getToken();
58+
if (StringUtils.isNotEmpty(token)) {
59+
return new UsernamePasswordCredentialsProvider(token, "");
60+
} else {
61+
return new UsernamePasswordCredentialsProvider(gitProperties.getUsername(), gitProperties.getPassword());
62+
}
63+
}
64+
65+
}

consul-populate-core/src/main/java/com/frogdevelopment/consul/populate/git/GitImportFactory.java

Lines changed: 0 additions & 94 deletions
This file was deleted.

consul-populate-core/src/main/java/com/frogdevelopment/consul/populate/git/GitImportJob.java

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,34 @@
33
import lombok.RequiredArgsConstructor;
44
import lombok.extern.slf4j.Slf4j;
55

6-
import java.io.IOException;
7-
import java.nio.file.Path;
6+
import jakarta.inject.Named;
87

9-
import org.apache.commons.io.FileUtils;
108
import org.eclipse.jgit.api.Git;
119
import org.eclipse.jgit.api.errors.GitAPIException;
12-
import org.eclipse.jgit.lib.Constants;
1310

1411
import com.frogdevelopment.consul.populate.PopulateService;
1512

13+
import io.micronaut.scheduling.TaskExecutors;
1614
import io.micronaut.scheduling.TaskScheduler;
1715

16+
/**
17+
* Orchestrates git repository population and optional polling for changes.
18+
* Triggers Consul population and sets up scheduled pulls when polling is enabled.
19+
*
20+
* @author Le Gall Benoît
21+
* @since 1.2.0
22+
*/
1823
@Slf4j
1924
@RequiredArgsConstructor
20-
public class GitImportJob implements AutoCloseable {
25+
public class GitImportJob {
2126

2227
private final GitProperties gitProperties;
23-
private final Path repositoryDirectory;
2428
private final PopulateService populateService;
29+
@Named(TaskExecutors.SCHEDULED)
2530
private final TaskScheduler taskScheduler;
26-
27-
private Git git;
31+
private final Git git;
2832

2933
public void init() throws GitAPIException {
30-
log.debug("Cloning repository [{}]", gitProperties.getUri());
31-
git = Git.cloneRepository()
32-
.setURI(gitProperties.getUri())
33-
.setDirectory(repositoryDirectory.toFile())
34-
.setGitDir(repositoryDirectory.resolve(Constants.DOT_GIT).toFile())
35-
.setBranch(gitProperties.getBranch())
36-
.setRemote(Constants.DEFAULT_REMOTE_NAME)
37-
.call();
38-
3934
log.info("Populating Consul with repository");
4035
populateService.populate();
4136

@@ -66,23 +61,4 @@ private void pull() {
6661
log.error("Scheduled task encountered an error. Please check logs", e);
6762
}
6863
}
69-
70-
@Override
71-
public void close() {
72-
try {
73-
if (git != null) {
74-
git.close();
75-
}
76-
} catch (final Exception e) {
77-
log.error("Error closing git repository", e);
78-
}
79-
80-
try {
81-
log.info("Deleting Git Repository...");
82-
FileUtils.deleteDirectory(repositoryDirectory.toFile());
83-
} catch (final IOException e) {
84-
log.error("Failed to delete repository", e);
85-
}
86-
}
87-
8864
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.frogdevelopment.consul.populate.git;
2+
3+
import lombok.RequiredArgsConstructor;
4+
import lombok.extern.slf4j.Slf4j;
5+
6+
import java.util.Map;
7+
8+
import jakarta.inject.Singleton;
9+
10+
import com.fasterxml.jackson.databind.ObjectMapper;
11+
import com.frogdevelopment.consul.populate.DataImporter;
12+
import com.frogdevelopment.consul.populate.files.JsonFilesImporter;
13+
import com.frogdevelopment.consul.populate.files.PropertiesFilesImporter;
14+
import com.frogdevelopment.consul.populate.files.YamlFilesImporter;
15+
16+
import io.micronaut.core.annotation.NonNull;
17+
import io.micronaut.core.util.StringUtils;
18+
19+
/**
20+
* Git-based data importer that reads configuration files from a cloned repository.
21+
* Delegates to the appropriate {@link com.frogdevelopment.consul.populate.files.FilesImporter}
22+
* based on the configured file format.
23+
*
24+
* @author Le Gall Benoît
25+
* @since 1.2.0
26+
*/
27+
@Slf4j
28+
@Singleton
29+
@RequiredArgsConstructor
30+
public class GitImporter implements DataImporter {
31+
32+
private final RepositoryDirectoryProvider repositoryDirectoryProvider;
33+
private final GitProperties gitProperties;
34+
private final ObjectMapper objectMapper;
35+
36+
/**
37+
* Reads configuration files from the cloned git repository and converts them to key-value pairs.
38+
*
39+
* @return map of configuration keys to their JSON/YAML/Properties string values
40+
*/
41+
@NonNull
42+
@Override
43+
public Map<String, String> execute() {
44+
final var repositoryDirectory = repositoryDirectoryProvider.getRepository();
45+
log.debug("Reading configurations from git repository at {}", repositoryDirectory);
46+
47+
final var fileProperties = gitProperties.getFileProperties();
48+
49+
// Calculate root and target paths within the cloned repository
50+
final var rootPath = StringUtils.isEmpty(fileProperties.getRootPath())
51+
? repositoryDirectory
52+
: repositoryDirectory.resolve(fileProperties.getRootPath());
53+
54+
final var targetPath = StringUtils.isEmpty(fileProperties.getTarget())
55+
? rootPath
56+
: rootPath.resolve(fileProperties.getTarget());
57+
58+
// Delegate to the appropriate FilesImporter based on format
59+
final DataImporter filesImporter = switch (fileProperties.getFormat()) {
60+
case JSON -> new JsonFilesImporter(rootPath, targetPath, objectMapper);
61+
case PROPERTIES -> new PropertiesFilesImporter(rootPath, targetPath);
62+
case YAML -> new YamlFilesImporter(rootPath, targetPath);
63+
};
64+
65+
return filesImporter.execute();
66+
}
67+
}

0 commit comments

Comments
 (0)