new subcommand: backup list

- todo: fancy print
This commit is contained in:
qwjyh 2024-03-15 04:16:57 +09:00
parent d947dd35e0
commit ff32996360
6 changed files with 221 additions and 11 deletions

View file

@ -11,7 +11,7 @@ use serde::{Deserialize, Serialize};
use crate::{
devices::Device,
storages::{self, Storage},
storages::{self, Storage, StorageExt, Storages},
};
/// Directory to store backup configs for each devices.
@ -28,9 +28,9 @@ pub fn backups_file(device: &Device) -> PathBuf {
pub struct BackupTarget {
/// `name()` of [`Storage`].
/// Use `String` for serialization/deserialization.
storage: String,
pub storage: String,
/// Relative path to the `storage`.
path: PathBuf,
pub path: PathBuf,
}
impl BackupTarget {
@ -40,6 +40,12 @@ impl BackupTarget {
path: relative_path,
}
}
pub fn path(&self, storages: &Storages, device: &Device) -> Result<PathBuf> {
let parent = storages.get(&self.storage).unwrap();
let parent_path = parent.mount_path(device, storages)?;
Ok(parent_path.join(self.path.clone()))
}
}
/// Type of backup commands.
@ -48,6 +54,26 @@ pub enum BackupCommand {
ExternallyInvoked(ExternallyInvoked),
}
pub trait BackupCommandExt {
fn name(&self) -> &String;
fn note(&self) -> &String;
}
impl BackupCommandExt for BackupCommand {
fn name(&self) -> &String {
match self {
BackupCommand::ExternallyInvoked(cmd) => cmd.name(),
}
}
fn note(&self) -> &String {
match self {
BackupCommand::ExternallyInvoked(cmd) => cmd.note(),
}
}
}
/// Backup commands which is not invoked from xdbm itself.
/// Call xdbm externally to record backup datetime and status.
#[derive(Debug, Serialize, Deserialize)]
@ -62,6 +88,16 @@ impl ExternallyInvoked {
}
}
impl BackupCommandExt for ExternallyInvoked {
fn name(&self) -> &String {
&self.name
}
fn note(&self) -> &String {
&self.note
}
}
/// Backup execution log.
#[derive(Debug, Serialize, Deserialize)]
pub struct BackupLog {
@ -112,6 +148,22 @@ impl Backup {
pub fn name(&self) -> &String {
&self.name
}
pub fn device<'a>(&'a self, devices: &'a Vec<Device>) -> Option<&Device> {
devices.iter().find(|dev| dev.name() == self.device)
}
pub fn source(&self) -> &BackupTarget {
&self.from
}
pub fn destination(&self) -> &BackupTarget {
&self.to
}
pub fn command(&self) -> &BackupCommand {
&self.command
}
}
#[derive(Debug, Serialize, Deserialize)]