diff options
author | Lars Wirzenius <liw@liw.fi> | 2021-01-16 16:24:30 +0000 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2021-01-16 16:24:30 +0000 |
commit | 6d53680f71d7d88bb9498c75db95bfb1408c6fc7 (patch) | |
tree | fe54eb4a0f396da881ef504482021b62852a3ce3 /src/cmd/show_gen.rs | |
parent | dc7fd9e24ed488af27b2e5b067652f3ba244bc3d (diff) | |
parent | d4abb267d2216281586801abac96cdc5759c83ae (diff) | |
download | obnam2-6d53680f71d7d88bb9498c75db95bfb1408c6fc7.tar.gz |
Merge branch 'showgen' into 'main'
feat: add "obnam show-generation" subcommand
Closes #51
See merge request larswirzenius/obnam!66
Diffstat (limited to 'src/cmd/show_gen.rs')
-rw-r--r-- | src/cmd/show_gen.rs | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/src/cmd/show_gen.rs b/src/cmd/show_gen.rs new file mode 100644 index 0000000..d355389 --- /dev/null +++ b/src/cmd/show_gen.rs @@ -0,0 +1,46 @@ +use crate::client::BackupClient; +use crate::client::ClientConfig; +use crate::error::ObnamError; +use crate::fsentry::FilesystemKind; +use indicatif::HumanBytes; +use tempfile::NamedTempFile; + +pub fn show_generation(config: &ClientConfig, gen_ref: &str) -> anyhow::Result<()> { + // Create a named temporary file. We don't meed the open file + // handle, so we discard that. + let dbname = { + let temp = NamedTempFile::new()?; + let (_, dbname) = temp.keep()?; + dbname + }; + + let client = BackupClient::new(&config.server_url)?; + + let genlist = client.list_generations()?; + let gen_id: String = match genlist.resolve(gen_ref) { + None => return Err(ObnamError::UnknownGeneration(gen_ref.to_string()).into()), + Some(id) => id, + }; + + let gen = client.fetch_generation(&gen_id, &dbname)?; + let files = gen.files()?; + + let total_bytes = files.iter().fold(0, |acc, file| { + let e = file.entry(); + if e.kind() == FilesystemKind::Regular { + acc + file.entry().len() + } else { + acc + } + }); + + println!("generation-id: {}", gen_id); + println!("file-count: {}", gen.file_count()?); + println!("file-bytes: {}", HumanBytes(total_bytes)); + println!("file-bytes-raw: {}", total_bytes); + + // Delete the temporary file. + std::fs::remove_file(&dbname)?; + + Ok(()) +} |