From 5159bfdddd5fda8e8416972a12d7c9a7f1776c7d Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Sat, 24 Jun 2023 13:35:59 +0200 Subject: [PATCH] feat: basic list command --- src/backup/manager/meta.rs | 4 ++++ src/backup/manager/mod.rs | 5 +++++ src/cli/backup.rs | 32 ++++++++++++++++++++++++++++++-- 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/src/backup/manager/meta.rs b/src/backup/manager/meta.rs index ff91310..de82576 100644 --- a/src/backup/manager/meta.rs +++ b/src/backup/manager/meta.rs @@ -127,4 +127,8 @@ where .get(layer) .map(|manager| manager.restore_backup(start_time)) } + + pub fn managers(&self) -> &HashMap> { + &self.managers + } } diff --git a/src/backup/manager/mod.rs b/src/backup/manager/mod.rs index 2b742f7..47488d9 100644 --- a/src/backup/manager/mod.rs +++ b/src/backup/manager/mod.rs @@ -181,4 +181,9 @@ where Err(other("Unknown backup.")) } + + /// Get a reference to the underlying chains + pub fn chains(&self) -> &Vec>> { + &self.chains + } } diff --git a/src/cli/backup.rs b/src/cli/backup.rs index 32d45c4..44c7bca 100644 --- a/src/cli/backup.rs +++ b/src/cli/backup.rs @@ -9,7 +9,7 @@ use std::path::PathBuf; #[derive(Subcommand)] pub enum BackupCommands { /// List all tracked backups - List, + List(BackupListArgs), /// Manually create a new backup Create(BackupCreateArgs), /// Restore a backup @@ -28,6 +28,12 @@ pub struct BackupCreateArgs { layer: String, } +#[derive(Args)] +pub struct BackupListArgs { + /// What layer to list + layer: Option, +} + #[derive(Args)] pub struct BackupRestoreArgs { /// Path to the backup inside the backup directory @@ -41,7 +47,7 @@ impl BackupArgs { pub fn run(&self, cli: &Cli) -> io::Result<()> { match &self.command { BackupCommands::Create(args) => args.run(cli), - BackupCommands::List => Ok(()), + BackupCommands::List(args) => args.run(cli), BackupCommands::Restore(args) => args.run(cli), } } @@ -120,3 +126,25 @@ impl BackupRestoreArgs { } } } + +impl BackupListArgs { + pub fn run(&self, cli: &Cli) -> io::Result<()> { + let meta = cli.meta()?; + + for (name, manager) in meta.managers().iter() { + println!("{}", name); + + for chain in manager.chains().iter().filter(|c| !c.is_empty()) { + let mut iter = chain.iter(); + let first = iter.next().unwrap(); + println!(" {}", first.start_time.format(Backup::FILENAME_FORMAT)); + + for backup in iter { + println!(" {}", backup.start_time.format(Backup::FILENAME_FORMAT)); + } + } + } + + Ok(()) + } +}