Skip to content

Commit

Permalink
Implement config migration using yaml-config-updater
Browse files Browse the repository at this point in the history
  • Loading branch information
bivashy committed May 31, 2023
1 parent 6baf04c commit dc690a7
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
8 changes: 8 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<minimessage.version>4.13.0</minimessage.version>
<vk.api.version>0.4.2</vk.api.version>
<password4j.version>1.7.0</password4j.version>
<yaml-config-updater.version>1.4.2</yaml-config-updater.version>
</properties>

<repositories>
Expand Down Expand Up @@ -136,5 +137,12 @@
<artifactId>password4j</artifactId>
<version>${password4j.version}</version>
</dependency>

<!-- Yaml Config Updater -->
<dependency>
<groupId>ru.vyarus</groupId>
<artifactId>yaml-config-updater</artifactId>
<version>${yaml-config-updater.version}</version>
</dependency>
</dependencies>
</project>
24 changes: 24 additions & 0 deletions core/src/main/java/me/mastercapexd/auth/BaseAuthPlugin.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package me.mastercapexd.auth;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executors;

import com.bivashy.auth.api.AuthPlugin;
import com.bivashy.auth.api.AuthPluginProvider;
import com.bivashy.auth.api.account.AccountFactory;
import com.bivashy.auth.api.asset.resource.Resource;
import com.bivashy.auth.api.asset.resource.impl.FolderResource;
import com.bivashy.auth.api.asset.resource.impl.FolderResourceReader;
import com.bivashy.auth.api.bucket.AuthenticatingAccountBucket;
import com.bivashy.auth.api.bucket.AuthenticationStepContextFactoryBucket;
import com.bivashy.auth.api.bucket.AuthenticationStepFactoryBucket;
Expand Down Expand Up @@ -75,6 +80,7 @@
import me.mastercapexd.auth.util.HashUtils;
import me.mastercapexd.auth.util.TimeUtils;
import net.kyori.adventure.platform.AudienceProvider;
import ru.vyarus.yaml.updater.YamlUpdater;

public class BaseAuthPlugin implements AuthPlugin {
private final ConfigurationProcessor configurationProcessor = new SpongeConfigurateProcessor();
Expand Down Expand Up @@ -118,6 +124,13 @@ private void initializeBasic() {
this.registerCryptoProviders();
this.registerConfigurationProcessor();
this.config = new BasePluginConfig(this);
if (config.isAutoMigrateConfigEnabled()) {
try {
this.migrateConfig();
} catch(IOException | URISyntaxException e) {
e.printStackTrace();
}
}

this.authenticationStepContextFactoryBucket = new BaseAuthenticationStepContextFactoryBucket(config.getAuthenticationSteps());
DatabaseHelper databaseHelper = new DatabaseHelper(this);
Expand Down Expand Up @@ -167,6 +180,17 @@ private void initializeTelegram() {
new TelegramCommandRegistry();
}

private void migrateConfig() throws IOException, URISyntaxException {
FolderResource folderResource = new FolderResourceReader(getClass().getClassLoader(), "configurations").read();
for (Resource resource : folderResource.getResources()) {
String realConfigurationName = resource.getName().substring(folderResource.getName().length() + 1);
File resourceConfiguration = new File(getFolder(), realConfigurationName);
if (!resourceConfiguration.exists())
continue;
YamlUpdater.create(resourceConfiguration, resource.getStream()).backup(true).update();
}
}

private void registerConfigurationProcessor() {
configurationProcessor.registerFieldResolver(ConfigurationServer.class, (context) -> new BaseConfigurationServer(context.getString()))
.registerFieldResolver(ConfigurationDuration.class, (context) -> new ConfigurationDuration(TimeUtils.parseDuration(context.getString("1s"))))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
package me.mastercapexd.auth.server.commands;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;

import com.bivashy.auth.api.AuthPlugin;
import com.bivashy.auth.api.asset.resource.Resource;
import com.bivashy.auth.api.asset.resource.impl.FolderResource;
import com.bivashy.auth.api.asset.resource.impl.FolderResourceReader;
import com.bivashy.auth.api.config.PluginConfig;
import com.bivashy.auth.api.config.message.MessageContext;
import com.bivashy.auth.api.crypto.HashInput;
Expand All @@ -17,6 +24,7 @@
import revxrsal.commands.annotation.DefaultFor;
import revxrsal.commands.annotation.Dependency;
import revxrsal.commands.annotation.Subcommand;
import ru.vyarus.yaml.updater.YamlUpdater;

@Command({"authadmin", "adminauth", "auth"})
@Permission("auth.admin")
Expand Down Expand Up @@ -86,4 +94,17 @@ public void reload(ServerCommandActor actor) {
plugin.getConfig().reload();
actor.reply(config.getServerMessages().getMessage("auth-reloaded"));
}

@Subcommand("migrateconfig")
public void migrateConfig(ServerCommandActor actor) throws IOException, URISyntaxException {
FolderResource folderResource = new FolderResourceReader(plugin.getClass().getClassLoader(), "configurations").read();
for (Resource resource : folderResource.getResources()) {
String realConfigurationName = resource.getName().substring(folderResource.getName().length() + 1);
File resourceConfiguration = new File(plugin.getFolder(), realConfigurationName);
if (!resourceConfiguration.exists())
continue;
YamlUpdater.create(resourceConfiguration, resource.getStream()).backup(true).update();
}
actor.reply(config.getServerMessages().getMessage("config-migrated"));
}
}
1 change: 1 addition & 0 deletions core/src/main/resources/configurations/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ messages:
auth-change-success: '&aУспешно изменен пароль игрока'
auth-delete-success: '&aАккаунт успешно &cудалён'
auth-reloaded: '&aПлагин успешно перезагрузил конфигурацию'
config-migrated: '&aКонфигурация успешно мигрирована'
# Ошибки в командах
player-offline: '&cИгрок с ником %account_name% оффлайн!'

Expand Down

0 comments on commit dc690a7

Please sign in to comment.