Fix: Initialization Issue and Enhance Configuration Management for ConfigInitializer #1324

This commit is contained in:
Ludy87 2024-05-31 23:51:42 +02:00
parent 6afbd8bd24
commit 19c564a6f7
No known key found for this signature in database
GPG Key ID: 92696155E0220F94

View File

@ -1,8 +1,10 @@
package stirling.software.SPDF.config;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
@ -45,15 +47,27 @@ public class ConfigInitializer
}
}
} else {
Path templatePath =
Paths.get(
getClass()
.getClassLoader()
.getResource("settings.yml.template")
.toURI());
Path userPath = Paths.get("configs", "settings.yml");
// Load the template content from classpath
List<String> templateLines;
try (InputStream in =
getClass().getClassLoader().getResourceAsStream("settings.yml.template")) {
if (in == null) {
throw new FileNotFoundException(
"Resource file not found: settings.yml.template");
}
templateLines = new ArrayList<>();
try (var reader = new InputStreamReader(in)) {
try (var bufferedReader = new BufferedReader(reader)) {
String line;
while ((line = bufferedReader.readLine()) != null) {
templateLines.add(line);
}
}
}
}
List<String> templateLines = Files.readAllLines(templatePath);
// Read the user settings file if it exists
Path userPath = Paths.get("configs", "settings.yml");
List<String> userLines =
Files.exists(userPath) ? Files.readAllLines(userPath) : new ArrayList<>();
@ -87,6 +101,7 @@ public class ConfigInitializer
Files.write(userPath, resultLines);
}
// Ensure the custom settings file exists
Path customSettingsPath = Paths.get("configs", "custom_settings.yml");
if (!Files.exists(customSettingsPath)) {
Files.createFile(customSettingsPath);