add snapshots command

This commit is contained in:
Alexander Weiss 2022-02-04 15:39:25 +01:00
parent 3628e34410
commit 46bd4e2c12
4 changed files with 55 additions and 11 deletions

View File

@ -22,4 +22,5 @@ boomphf = "0.5"
walkdir = "2"
# commands
clap = { version = "3", features = ["derive"] }
prettytable-rs = "0.8"

View File

@ -6,6 +6,7 @@ use crate::repo;
mod cat;
mod list;
mod snapshots;
#[derive(Parser)]
#[clap(about, version)]
@ -29,6 +30,9 @@ enum Command {
/// cat files
Cat(cat::Opts),
/// cat files
Snapshots(snapshots::Opts),
}
pub fn execute() -> Result<()> {
@ -41,5 +45,6 @@ pub fn execute() -> Result<()> {
match args.command {
Command::List(opts) => list::execute(&be, opts),
Command::Cat(opts) => cat::execute(&be, &dbe, opts),
Command::Snapshots(opts) => snapshots::execute(&dbe, opts),
}
}

28
src/commands/snapshots.rs Normal file
View File

@ -0,0 +1,28 @@
use anyhow::Result;
use clap::Parser;
use prettytable::{cell, format, row, Table};
use crate::backend::{FileType, ReadBackend};
use crate::repo::SnapshotFile;
#[derive(Parser)]
pub(super) struct Opts {}
pub(super) fn execute(be: &impl ReadBackend, _opts: Opts) -> Result<()> {
let mut table = Table::new();
table.set_titles(row!["ID", "Time", "Host", "Tags", "Paths"]);
table.set_format(*format::consts::FORMAT_NO_BORDER_LINE_SEPARATOR);
for id in be.list(FileType::Snapshot)? {
let sn = SnapshotFile::from_backend(be, id)?;
let paths = sn
.paths
.iter()
.map(|p| p.to_string_lossy() + "\n")
.collect::<String>();
table.add_row(row![id, sn.time, sn.hostname, "", paths,]);
}
table.printstd();
Ok(())
}

View File

@ -1,28 +1,38 @@
use anyhow::Result;
use chrono::{DateTime, Local};
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
use super::Id;
use chrono::{DateTime, Local};
use serde::{Deserialize, Serialize};
use crate::backend::{FileType, ReadBackend};
#[derive(Debug, Serialize, Deserialize)]
pub struct SnapshotFile {
time: DateTime<Local>,
tree: Id,
paths: Vec<PathBuf>,
pub time: DateTime<Local>,
pub tree: Id,
pub paths: Vec<PathBuf>,
#[serde(default)]
hostname: String,
pub hostname: String,
#[serde(default)]
username: String,
pub username: String,
#[serde(default)]
uid: u32,
pub uid: u32,
#[serde(default)]
gid: u32,
pub gid: u32,
#[serde(default)]
tags: TagList,
pub tags: TagList,
}
impl SnapshotFile {
/// Get an IndexFile from the backend
pub fn from_backend<B: ReadBackend>(be: &B, id: Id) -> Result<Self> {
let data = be.read_full(FileType::Snapshot, id)?;
Ok(serde_json::from_slice(&data)?)
}
}
#[derive(Default, Debug, Serialize, Deserialize)]
struct TagList(Vec<Tag>);
pub struct TagList(Vec<Tag>);
#[derive(Default, Debug, Serialize, Deserialize)]
pub struct Tag(String);