Skip to content

Commit 1e7a0de

Browse files
authored
Merge pull request #680 from ArikSquad/feat/config-migrator
feat: add config merge
2 parents 30e2288 + aa8d8ba commit 1e7a0de

1 file changed

Lines changed: 31 additions & 28 deletions

File tree

commons/src/main/java/net/swofty/commons/config/ConfigProvider.java

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,34 @@
1313

1414
public class ConfigProvider {
1515

16-
@Getter
17-
@Setter
18-
@Accessors(fluent = true)
19-
private static Settings settings;
20-
21-
static {
22-
try {
23-
Logger.info("Loading config...");
24-
YamlConfigurationLoader loader = YamlConfigurationLoader.builder()
25-
.path(Path.of("./configuration/config.yml"))
26-
.nodeStyle(NodeStyle.BLOCK)
27-
.build();
28-
29-
CommentedConfigurationNode node = loader.load();
30-
Settings existingSettings = node.get(Settings.class);
31-
if (existingSettings == null) {
32-
existingSettings = new Settings();
33-
}
34-
Settings config = existingSettings;
35-
node.set(Settings.class, config);
36-
loader.save(node);
37-
38-
settings(config);
39-
} catch (IOException e) {
40-
throw new RuntimeException("Failed to load configuration", e);
41-
}
42-
}
43-
}
16+
@Getter
17+
@Setter
18+
@Accessors(fluent = true)
19+
private static Settings settings;
20+
21+
static {
22+
try {
23+
Logger.info("Loading config...");
24+
25+
YamlConfigurationLoader loader = YamlConfigurationLoader.builder()
26+
.path(Path.of("./configuration/config.yml"))
27+
.nodeStyle(NodeStyle.BLOCK)
28+
.build();
29+
30+
CommentedConfigurationNode root = loader.load();
31+
CommentedConfigurationNode defaults = loader.createNode();
32+
defaults.set(Settings.class, new Settings());
33+
root.mergeFrom(defaults);
34+
35+
Settings loaded = root.get(Settings.class);
36+
if (loaded == null) {
37+
loaded = new Settings();
38+
}
39+
40+
loader.save(root);
41+
settings(loaded);
42+
} catch (IOException e) {
43+
throw new RuntimeException("Failed to load configuration", e);
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)