Added 'latest' keyword to restore (#85)
RestoreHelper::getAvailableBackups now returns sorted LinkedList2.x
parent
2f11548fef
commit
8427eebfcc
|
@ -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();
|
return builder.buildFuture();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,7 +34,7 @@ public class ListBackupsCommand {
|
||||||
public static LiteralArgumentBuilder<ServerCommandSource> register() {
|
public static LiteralArgumentBuilder<ServerCommandSource> register() {
|
||||||
return CommandManager.literal("list")
|
return CommandManager.literal("list")
|
||||||
.executes(ctx -> { StringBuilder builder = new StringBuilder();
|
.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) {
|
if(backups.size() == 0) {
|
||||||
builder.append("There a no backups available for this world.");
|
builder.append("There a no backups available for this world.");
|
||||||
|
|
|
@ -36,6 +36,7 @@ import net.szum123321.textile_backup.core.restore.RestoreHelper;
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.time.format.DateTimeParseException;
|
import java.time.format.DateTimeParseException;
|
||||||
|
import java.util.Objects;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
public class RestoreBackupCommand {
|
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, "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, "[YEAR]-[MONTH]-[DAY]_[HOUR].[MINUTE].[SECOND]");
|
||||||
log.sendInfo(source, "Example: /backup restore 2020-08-05_10.58.33");
|
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;
|
return 1;
|
||||||
});
|
});
|
||||||
|
@ -78,21 +80,26 @@ public class RestoreBackupCommand {
|
||||||
}
|
}
|
||||||
|
|
||||||
LocalDateTime dateTime;
|
LocalDateTime dateTime;
|
||||||
|
Optional<RestoreableFile> backupFile;
|
||||||
|
|
||||||
|
if(Objects.equals(file, "latest")) {
|
||||||
|
backupFile = RestoreHelper.getLatestAndLockIfPresent(source.getServer());
|
||||||
|
dateTime = backupFile.map(RestoreableFile::getCreationTime).orElse(LocalDateTime.now());
|
||||||
|
} else {
|
||||||
try {
|
try {
|
||||||
dateTime = LocalDateTime.from(Globals.defaultDateTimeFormatter.parse(file));
|
dateTime = LocalDateTime.from(Globals.defaultDateTimeFormatter.parse(file));
|
||||||
} catch (DateTimeParseException e) {
|
} catch (DateTimeParseException e) {
|
||||||
throw CommandExceptions.DATE_TIME_PARSE_COMMAND_EXCEPTION_TYPE.create(e);
|
throw CommandExceptions.DATE_TIME_PARSE_COMMAND_EXCEPTION_TYPE.create(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
Optional<RestoreableFile> backupFile = RestoreHelper.findFileAndLockIfPresent(dateTime, source.getServer());
|
backupFile = RestoreHelper.findFileAndLockIfPresent(dateTime, source.getServer());
|
||||||
|
}
|
||||||
|
|
||||||
if(backupFile.isPresent()) {
|
if(backupFile.isEmpty()) {
|
||||||
log.info("Found file to restore {}", backupFile.get().getFile().getFileName().toString());
|
|
||||||
} else {
|
|
||||||
log.sendInfo(source, "No file created on {} was found!", dateTime.format(Globals.defaultDateTimeFormatter));
|
log.sendInfo(source, "No file created on {} was found!", dateTime.format(Globals.defaultDateTimeFormatter));
|
||||||
return -1;
|
return -1;
|
||||||
}
|
} else {
|
||||||
|
log.info("Found file to restore {}", backupFile.get().getFile().getFileName().toString());
|
||||||
|
|
||||||
Globals.INSTANCE.setAwaitThread(
|
Globals.INSTANCE.setAwaitThread(
|
||||||
RestoreHelper.create(
|
RestoreHelper.create(
|
||||||
|
@ -108,5 +115,5 @@ public class RestoreBackupCommand {
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -50,6 +50,17 @@ public class RestoreHelper {
|
||||||
return optionalFile;
|
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) {
|
public static AwaitThread create(RestoreContext ctx) {
|
||||||
if(ctx.initiator() == ActionInitiator.Player)
|
if(ctx.initiator() == ActionInitiator.Player)
|
||||||
log.info("Backup restoration was initiated by: {}", ctx.commandSource().getName());
|
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));
|
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),
|
e -> log.error("Error while listing available backups", e),
|
||||||
s -> s.collect(Collectors.toList()));
|
s -> s.sorted().collect(Collectors.toCollection(LinkedList::new)));
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue