Added crude writting of database config
This commit is contained in:
125
src/main/ConfigManager.java
Normal file
125
src/main/ConfigManager.java
Normal file
@@ -0,0 +1,125 @@
|
||||
package src.main;
|
||||
|
||||
import java.util.AbstractMap;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.io.IOException;
|
||||
import java.lang.Object;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.util.Scanner;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
import java.util.Map;
|
||||
|
||||
public class ConfigManager {
|
||||
|
||||
private static final Path CONFIGS_DIR = Paths.get("src/resources/configs");
|
||||
private static final Path CONFIGS_BACKUP_DIR = Paths.get("src/resources/configs/backups");
|
||||
|
||||
private static final Map.Entry<String, String[]> DATABASE_CONFIG = new AbstractMap.SimpleEntry<String, String[]>("database.json",
|
||||
new String[]{
|
||||
"DatabaseAddress",
|
||||
"DataBasePort",
|
||||
"DataBaseUsername",
|
||||
"DataBasePassword"
|
||||
});
|
||||
|
||||
public ConfigManager () {
|
||||
|
||||
makeBackups(CONFIGS_DIR, CONFIGS_BACKUP_DIR);
|
||||
|
||||
if(!Files.exists(Path.of(CONFIGS_DIR.toString(), DATABASE_CONFIG.getKey()))) {
|
||||
writeConfig(Paths.get(CONFIGS_DIR.toString(), DATABASE_CONFIG.getKey()), DATABASE_CONFIG.getValue());
|
||||
}
|
||||
|
||||
//HashMap<String, Object> databaseConfig;
|
||||
//databaseConfig = readConfig(Paths.get(CONFIGS_DIR.toString(), "database.json"));
|
||||
//System.out.println(databaseConfig);
|
||||
}
|
||||
|
||||
private void getUserInputConfig(HashMap<String, Object> config, Scanner scanner, String configEntry) {
|
||||
System.out.println("Please enter " + configEntry);
|
||||
config.put(configEntry, scanner.nextLine());
|
||||
}
|
||||
|
||||
private void writeConfig(Path configPath, String[] configContent) {
|
||||
Scanner inputScanner = new Scanner(System.in);
|
||||
HashMap<String, Object> configData = new HashMap<String, Object>();
|
||||
|
||||
for (String entry : configContent) {
|
||||
getUserInputConfig(configData, inputScanner, entry);
|
||||
}
|
||||
|
||||
JSONObject json = new JSONObject(configData);
|
||||
|
||||
try {
|
||||
Files.writeString(configPath, json.toString(), StandardCharsets.UTF_8);
|
||||
} catch (IOException error) {
|
||||
error.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private HashMap<String, Object> readConfig(Path configPath) {
|
||||
|
||||
HashMap<String, Object> config = new HashMap<String, Object>();
|
||||
|
||||
try {
|
||||
String configContent = new String(Files.readAllBytes(configPath));
|
||||
JSONObject jsonObject = new JSONObject(configContent);
|
||||
System.out.println(jsonObject);
|
||||
} catch (Exception error) {
|
||||
error.printStackTrace();
|
||||
}
|
||||
return config;
|
||||
}
|
||||
|
||||
private void makeBackups(Path source, Path destination) {
|
||||
// Ensure the source path exists.
|
||||
if (!Files.exists(source) || !Files.isDirectory(source)) {
|
||||
System.err.println("Source directory does not exist or is not a directory.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Ensure the destination exists, if not create it
|
||||
if (!Files.exists(destination)) {
|
||||
try {
|
||||
// Create destination directory.
|
||||
Files.createDirectories(destination);
|
||||
} catch (IOException error) {
|
||||
error.printStackTrace();
|
||||
System.err.println("Failed to create destination address \"" + destination.toString() + "\"");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Attempt to copy files from source to destination
|
||||
try (Stream<Path> pathStream = Files.list(source)) {
|
||||
// Get a list of files, filter out non-file types
|
||||
List<Path> fileList = pathStream
|
||||
.filter(Files::isRegularFile)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// Copy the file to the backup directory, replacing existing ones
|
||||
for(Path filePath : fileList) {
|
||||
String newFileName = filePath.getFileName().toString() + ".bak";
|
||||
System.out.println(newFileName);
|
||||
System.out.println(Path.of(destination.toString(), newFileName).toString());
|
||||
Files.copy(filePath, Path.of(destination.toString(), newFileName), StandardCopyOption.REPLACE_EXISTING);
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
System.err.println("Failed to copy file ");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user