Added 'latest' keyword to restore (#85)

RestoreHelper::getAvailableBackups now returns sorted LinkedList
This commit is contained in:
Szum123321 2022-11-06 11:00:14 +01:00
parent 2f11548fef
commit 8427eebfcc
4 changed files with 52 additions and 28 deletions

View File

@ -62,6 +62,12 @@ public final class FileSuggestionProvider implements SuggestionProvider<ServerCo
}
}
if("latest".startsWith(remaining) && !files.isEmpty()) //suggest latest
builder.suggest("latest", new LiteralMessage (
files.getLast().getCreationTime().format(Globals.defaultDateTimeFormatter) +
(files.getLast().getComment().map(s -> "#" + s).orElse("")))
);
return builder.buildFuture();
}
}

View File

@ -34,7 +34,7 @@ public class ListBackupsCommand {
public static LiteralArgumentBuilder<ServerCommandSource> register() {
return CommandManager.literal("list")
.executes(ctx -> { StringBuilder builder = new StringBuilder();
List<RestoreableFile> backups = RestoreHelper.getAvailableBackups(ctx.getSource().getServer());
var backups = RestoreHelper.getAvailableBackups(ctx.getSource().getServer());
if(backups.size() == 0) {
builder.append("There a no backups available for this world.");

View File

@ -36,6 +36,7 @@ import net.szum123321.textile_backup.core.restore.RestoreHelper;
import javax.annotation.Nullable;
import java.time.LocalDateTime;
import java.time.format.DateTimeParseException;
import java.util.Objects;
import java.util.Optional;
public class RestoreBackupCommand {
@ -65,6 +66,7 @@ public class RestoreBackupCommand {
log.sendInfo(source, "To restore given backup you have to provide exact creation time in format:");
log.sendInfo(source, "[YEAR]-[MONTH]-[DAY]_[HOUR].[MINUTE].[SECOND]");
log.sendInfo(source, "Example: /backup restore 2020-08-05_10.58.33");
log.sendInfo(source, "You may also type '/backup restore latest' to restore the freshest backup");
return 1;
});
@ -78,35 +80,40 @@ public class RestoreBackupCommand {
}
LocalDateTime dateTime;
Optional<RestoreableFile> backupFile;
try {
dateTime = LocalDateTime.from(Globals.defaultDateTimeFormatter.parse(file));
} catch (DateTimeParseException e) {
throw CommandExceptions.DATE_TIME_PARSE_COMMAND_EXCEPTION_TYPE.create(e);
if(Objects.equals(file, "latest")) {
backupFile = RestoreHelper.getLatestAndLockIfPresent(source.getServer());
dateTime = backupFile.map(RestoreableFile::getCreationTime).orElse(LocalDateTime.now());
} else {
try {
dateTime = LocalDateTime.from(Globals.defaultDateTimeFormatter.parse(file));
} catch (DateTimeParseException e) {
throw CommandExceptions.DATE_TIME_PARSE_COMMAND_EXCEPTION_TYPE.create(e);
}
backupFile = RestoreHelper.findFileAndLockIfPresent(dateTime, source.getServer());
}
Optional<RestoreableFile> backupFile = RestoreHelper.findFileAndLockIfPresent(dateTime, source.getServer());
if(backupFile.isPresent()) {
log.info("Found file to restore {}", backupFile.get().getFile().getFileName().toString());
} else {
if(backupFile.isEmpty()) {
log.sendInfo(source, "No file created on {} was found!", dateTime.format(Globals.defaultDateTimeFormatter));
return -1;
} else {
log.info("Found file to restore {}", backupFile.get().getFile().getFileName().toString());
Globals.INSTANCE.setAwaitThread(
RestoreHelper.create(
RestoreContext.Builder.newRestoreContextBuilder()
.setCommandSource(source)
.setFile(backupFile.get())
.setComment(comment)
.build()
)
);
Globals.INSTANCE.getAwaitThread().get().start();
return 1;
}
Globals.INSTANCE.setAwaitThread(
RestoreHelper.create(
RestoreContext.Builder.newRestoreContextBuilder()
.setCommandSource(source)
.setFile(backupFile.get())
.setComment(comment)
.build()
)
);
Globals.INSTANCE.getAwaitThread().get().start();
return 1;
}
}

View File

@ -50,6 +50,17 @@ public class RestoreHelper {
return optionalFile;
}
public static Optional<RestoreableFile> getLatestAndLockIfPresent( MinecraftServer server) {
var available = RestoreHelper.getAvailableBackups(server);
if(available.isEmpty()) return Optional.empty();
else {
var latest = available.getLast();
Globals.INSTANCE.setLockedFile(latest.getFile());
return Optional.of(latest);
}
}
public static AwaitThread create(RestoreContext ctx) {
if(ctx.initiator() == ActionInitiator.Player)
log.info("Backup restoration was initiated by: {}", ctx.commandSource().getName());
@ -67,11 +78,11 @@ public class RestoreHelper {
);
}
public static List<RestoreableFile> getAvailableBackups(MinecraftServer server) {
public static LinkedList<RestoreableFile> getAvailableBackups(MinecraftServer server) {
Path root = Utilities.getBackupRootPath(Utilities.getLevelName(server));
return RestoreableFile.applyOnFiles(root, List.of(),
return RestoreableFile.applyOnFiles(root, new LinkedList<>(),
e -> log.error("Error while listing available backups", e),
s -> s.collect(Collectors.toList()));
s -> s.sorted().collect(Collectors.toCollection(LinkedList::new)));
}
}