diff --git a/.cargo/config.toml b/.cargo/config.toml index c5834ea..37fecab 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -1,3 +1,3 @@ [alias] -runs = "run -- run --config data/config --backup data/backups --world data/worlds --jar paper-1.19.4-550.jar --layers 2min,2,4,4;3min,3,2,2" +runs = "run -- --config data/config --backup data/backups --world data/worlds --layers 2min,2,4,4;3min,3,2,2" runrs = "run --release -- paper 1.19.4-545 --config data/config --backup data/backups --world data/worlds --jar data/paper-1.19.4-525.jar" diff --git a/src/backup/manager/meta.rs b/src/backup/manager/meta.rs index 076bf14..6ba9c06 100644 --- a/src/backup/manager/meta.rs +++ b/src/backup/manager/meta.rs @@ -101,4 +101,19 @@ where Ok(()) } } + + /// Create a manual backup for a specific layer + pub fn create_backup(&mut self, layer: &str) -> Option> { + if let Some(manager) = self.managers.get_mut(layer) { + let mut res = manager.create_backup(); + + if res.is_ok() { + res = manager.remove_old_backups(); + } + + Some(res) + } else { + None + } + } } diff --git a/src/cli/backup.rs b/src/cli/backup.rs index 2eeb4e4..b71d992 100644 --- a/src/cli/backup.rs +++ b/src/cli/backup.rs @@ -1,7 +1,10 @@ +use crate::cli::Cli; use clap::{Args, Subcommand}; +use std::io; #[derive(Subcommand)] pub enum BackupCommands { + Create(BackupCreateArgs), List, } @@ -10,3 +13,30 @@ pub struct BackupArgs { #[command(subcommand)] pub command: BackupCommands, } + +#[derive(Args)] +pub struct BackupCreateArgs { + /// What layer to create a backup in + layer: String, +} + +impl BackupArgs { + pub fn run(&self, cli: &Cli) -> io::Result<()> { + match &self.command { + BackupCommands::Create(args) => args.run(cli), + BackupCommands::List => Ok(()), + } + } +} + +impl BackupCreateArgs { + pub fn run(&self, cli: &Cli) -> io::Result<()> { + let mut meta = cli.meta()?; + + if let Some(res) = meta.create_backup(&self.layer) { + res + } else { + Err(io::Error::new(io::ErrorKind::Other, "Unknown layer")) + } + } +} diff --git a/src/cli/mod.rs b/src/cli/mod.rs index 9ecd9f3..eb4ba63 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -1,6 +1,8 @@ mod backup; mod run; +pub use crate::backup::MetaManager; +pub use crate::server::Metadata; pub use backup::{BackupArgs, BackupCommands}; pub use run::RunArgs; @@ -68,7 +70,23 @@ impl Cli { pub fn run(&self) -> io::Result<()> { match &self.command { Commands::Run(args) => args.run(self), - Commands::Backup(_) => Ok(()), + Commands::Backup(args) => args.run(self), } } + + /// Convenience method to initialize backup manager from the cli arguments + pub fn meta(&self) -> io::Result> { + let metadata = Metadata { + server_type: self.server, + server_version: self.server_version.clone(), + }; + let dirs = vec![ + (PathBuf::from("config"), self.config.clone()), + (PathBuf::from("worlds"), self.world.clone()), + ]; + let mut meta = MetaManager::new(self.backup.clone(), dirs, metadata); + meta.add_all(&self.layers)?; + + Ok(meta) + } }