diff options
author | Lars Wirzenius <liw@liw.fi> | 2022-04-21 18:27:17 +0300 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2022-04-21 18:27:17 +0300 |
commit | f5783321e102ade9a60a8a95eac226f0e52d0f11 (patch) | |
tree | ba3e908520be73f6cf688addfd0abd654939add4 /src/cmd/show_gen.rs | |
parent | 8882409f31ddb4d77cb94dd6c57f27af1b3310d5 (diff) | |
download | obnam2-f5783321e102ade9a60a8a95eac226f0e52d0f11.tar.gz |
feat: change "show-generation" output to JSON
This is more friendly towards anyone wanting to use the output in a script.
Sponsored-by: author
Diffstat (limited to 'src/cmd/show_gen.rs')
-rw-r--r-- | src/cmd/show_gen.rs | 38 |
1 files changed, 34 insertions, 4 deletions
diff --git a/src/cmd/show_gen.rs b/src/cmd/show_gen.rs index 970a165..6641d3c 100644 --- a/src/cmd/show_gen.rs +++ b/src/cmd/show_gen.rs @@ -5,7 +5,9 @@ use crate::client::BackupClient; use crate::config::ClientConfig; use crate::error::ObnamError; use crate::fsentry::FilesystemKind; +use crate::generation::GenId; use indicatif::HumanBytes; +use serde::Serialize; use structopt::StructOpt; use tempfile::NamedTempFile; use tokio::runtime::Runtime; @@ -51,11 +53,39 @@ impl ShowGeneration { }); let total_bytes = total_bytes?; - println!("generation-id: {}", gen_id); - println!("file-count: {}", gen.file_count()?); - println!("file-bytes: {}", HumanBytes(total_bytes)); - println!("file-bytes-raw: {}", total_bytes); + let output = Output::new(gen_id) + .file_count(gen.file_count()?) + .file_bytes(total_bytes); + serde_json::to_writer_pretty(std::io::stdout(), &output)?; Ok(()) } } + +#[derive(Debug, Default, Serialize)] +struct Output { + generation_id: String, + file_count: u64, + file_bytes: String, + file_bytes_raw: u64, +} + +impl Output { + fn new(gen_id: GenId) -> Self { + Self { + generation_id: format!("{}", gen_id), + ..Self::default() + } + } + + fn file_count(mut self, n: u64) -> Self { + self.file_count = n; + self + } + + fn file_bytes(mut self, n: u64) -> Self { + self.file_bytes_raw = n; + self.file_bytes = HumanBytes(n).to_string(); + self + } +} |