Doubling down on the BackupStrategy. Expecting to move this into both 'ac' as well as the GUI.

This commit is contained in:
Rob Greene committed 2026-08-09 13:51:16 -05:00
1 parent a391555ca4
commit 04e87e1196
5 files changed
+42 -11

No files matched your search

@@ -19,6 +19,7 @@
*/
package io.github.applecommander.acx.base;
import org.applecommander.util.BackupStrategy;
import picocli.CommandLine.Option;
public abstract class ReadWriteDiskCommandOptions extends ReadOnlyDiskImageCommandOptions {
@@ -33,11 +34,7 @@ public abstract class ReadWriteDiskCommandOptions extends ReadOnlyDiskImageComma
int returnCode = handleCommand();
if (returnCode == 0) {
BackupStrategy backupStrategy = switch (backupCode) {
case "" -> BackupStrategy.none();
case "bak" -> BackupStrategy.fileExtension("bak");
default -> BackupStrategy.directory(backupCode);
};
BackupStrategy backupStrategy = BackupStrategy.create(backupCode);
saveDisk(selectedDisks().getFirst(), backupStrategy);
}
@@ -20,6 +20,7 @@
package io.github.applecommander.acx.base;
import com.webcodepro.applecommander.storage.FormattedDisk;
import org.applecommander.util.BackupStrategy;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
@@ -22,7 +22,7 @@ package io.github.applecommander.acx.command;
import com.webcodepro.applecommander.storage.DiskFactory;
import com.webcodepro.applecommander.storage.Disks;
import com.webcodepro.applecommander.storage.FormattedDisk;
import io.github.applecommander.acx.base.BackupStrategy;
import org.applecommander.util.BackupStrategy;
import io.github.applecommander.acx.base.ReusableCommandOptions;
import org.applecommander.source.Source;
import org.applecommander.source.Sources;
@@ -27,7 +27,7 @@ import com.webcodepro.applecommander.storage.os.pascal.PascalFormatDisk;
import com.webcodepro.applecommander.storage.os.prodos.ProdosFormatDisk;
import io.github.applecommander.acx.OrderType;
import io.github.applecommander.acx.SystemType;
import io.github.applecommander.acx.base.BackupStrategy;
import org.applecommander.util.BackupStrategy;
import io.github.applecommander.acx.base.ReusableCommandOptions;
import io.github.applecommander.acx.converter.DataSizeConverter;
import org.applecommander.device.*;
@@ -17,7 +17,7 @@
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package io.github.applecommander.acx.base;
package org.applecommander.util;
import java.io.IOException;
import java.nio.file.Files;
@@ -26,13 +26,46 @@ import java.nio.file.Path;
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
/// Provides a simple but automated backup strategy to the `acx` tool.
///
/// General usage pattern is as follows:
/// ```java
/// // Only save if there are changes.
/// if (disk.getSource().hasChanged()) {
/// backupStrategy.backup(disk.getFilename());
/// disk.save();
/// }
/// ```
///
/// To create a `BackupStrategy` in a consistent manner, use the #create
/// method:
/// ```'java
/// BackupStrategy backupStrategy = BackupStrategy.create("bak");
/// ```
///
/// If using in a context where a backup is no expected (as in a new file, or
/// a "save as" situation), simply use `BackupStrategy.none()`.
///
/// The coding is as follows:
/// * `null` or blank (`""`): Returns an ineffective strategy. (No branches in code.)
/// * `bak`: Returns a backup strategy that renames the existing file from `name.dsk`
/// to `name.dsk.bak`, overwriting any existing files.
/// * The default strategy is to assume the value is a directory, _which will be created_,
/// and make a copy of the file into this directory.
///
/// @see #create(String)
/// @see #none()
/// @see #fileExtension(String)
/// @see #directory(String)
/// @see ReadWriteDiskCommandOptions
public interface BackupStrategy {
void backup(String filename) throws IOException;
static BackupStrategy create(String backupCode) {
if (backupCode == null) backupCode = "";
return switch (backupCode) {
case "" -> BackupStrategy.none();
case "bak" -> BackupStrategy.fileExtension("bak");
default -> BackupStrategy.directory(backupCode);
};
}
static BackupStrategy none() {
return filename -> {
// Do nothing