summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKeuin <[email protected]>2024-01-20 18:54:10 +0800
committerKeuin <[email protected]>2024-01-20 19:05:25 +0800
commit44227db56e3669b6b1575bf750df815e6903c906 (patch)
tree57c7a5503659aa0676119d7a9bdb48164fa6724f
parent7e3cd92742383c43f2741449c551208e6487154e (diff)
feature: config file support JSON comment
-rw-r--r--README.md2
-rw-r--r--src/main/java/com/keuin/kbackupfabric/config/KBackupConfig.java11
-rw-r--r--src/test/java/com/keuin/kbackupfabric/config/KBackupConfigTest.java29
3 files changed, 38 insertions, 4 deletions
diff --git a/README.md b/README.md
index e3a95e5..23942bf 100644
--- a/README.md
+++ b/README.md
@@ -92,7 +92,7 @@ The config file will be generated if not exist. The default is:
- `incbak_cow`: (experimental) Enable filesystem CoW (copy-on-write) for incremental backup.
**Will fall back to normal file copy if the filesystem does not support CoW.** Please read section 2.5 for more info.
-Note: JSON comment is **NOT** supported for now.
+Note: JSON comment **is** supported.
## 2.3 Script for auto-restart after restoring
diff --git a/src/main/java/com/keuin/kbackupfabric/config/KBackupConfig.java b/src/main/java/com/keuin/kbackupfabric/config/KBackupConfig.java
index 8ece8d2..8ce5ed4 100644
--- a/src/main/java/com/keuin/kbackupfabric/config/KBackupConfig.java
+++ b/src/main/java/com/keuin/kbackupfabric/config/KBackupConfig.java
@@ -1,6 +1,7 @@
package com.keuin.kbackupfabric.config;
import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.keuin.kbackupfabric.util.PrintUtil;
@@ -25,20 +26,24 @@ public class KBackupConfig {
return new KBackupConfig(false);
}
- public static void load() throws IOException {
- File file = new File(CONFIG_FILE);
+ public static void load(File file) throws IOException {
ObjectMapper om = new ObjectMapper();
+ om.enable(JsonParser.Feature.ALLOW_COMMENTS);
try {
instance = om.readValue(file, KBackupConfig.class);
} catch (FileNotFoundException ignored) {
// generate default config file
- PrintUtil.info("Config file does not exist. Creating default config: " + CONFIG_FILE);
+ PrintUtil.info("Config file does not exist. Creating default config: " + file.getAbsolutePath());
instance = getDefault();
ObjectWriter w = om.writerWithDefaultPrettyPrinter();
w.writeValue(file, instance);
}
}
+ public static void load() throws IOException {
+ load(new File(CONFIG_FILE));
+ }
+
public KBackupConfig() {
}
diff --git a/src/test/java/com/keuin/kbackupfabric/config/KBackupConfigTest.java b/src/test/java/com/keuin/kbackupfabric/config/KBackupConfigTest.java
new file mode 100644
index 0000000..811156a
--- /dev/null
+++ b/src/test/java/com/keuin/kbackupfabric/config/KBackupConfigTest.java
@@ -0,0 +1,29 @@
+package com.keuin.kbackupfabric.config;
+
+import com.keuin.kbackupfabric.TestUtils;
+import org.junit.Test;
+
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.nio.file.StandardOpenOption;
+
+public class KBackupConfigTest {
+
+ @Test
+ public void load() throws Exception {
+ String tempDir = TestUtils.getTempDirectory("config_test");
+ Files.createDirectories(Paths.get(tempDir));
+ Path configPath = Paths.get(tempDir, "test_config.json");
+ Files.write(
+ configPath,
+ "{\n//comment\n}".getBytes(StandardCharsets.UTF_8),
+ StandardOpenOption.CREATE,
+ StandardOpenOption.WRITE,
+ StandardOpenOption.TRUNCATE_EXISTING
+ );
+ System.out.println(configPath.toFile().getAbsolutePath());
+ KBackupConfig.load(configPath.toFile());
+ }
+} \ No newline at end of file