From f35fd36c7789b0167c34c072a32091dde4cdabf2 Mon Sep 17 00:00:00 2001 From: Alexander Weiss Date: Thu, 15 Sep 2022 12:02:17 +0200 Subject: [PATCH] snapshots: Add --json option --- src/commands/mod.rs | 4 ++-- src/commands/snapshots.rs | 12 +++++++++++- src/repo/snapshotfile.rs | 7 +++++-- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/commands/mod.rs b/src/commands/mod.rs index a1b3efa..2fe40d0 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -241,8 +241,8 @@ pub async fn execute() -> Result<()> { .then(|| Cache::new(config.id, opts.cache_dir).ok()) .flatten(); match &cache { - None => v1!("using no cache"), - Some(cache) => v1!("using cache at {}", cache.location()), + None => ve1!("using no cache"), + Some(cache) => ve1!("using cache at {}", cache.location()), } let be_cached = CachedBackend::new(be.clone(), cache.clone()); let dbe = DecryptBackend::new(&be_cached, key.clone()); diff --git a/src/commands/snapshots.rs b/src/commands/snapshots.rs index 46987cf..0f0374b 100644 --- a/src/commands/snapshots.rs +++ b/src/commands/snapshots.rs @@ -30,8 +30,12 @@ pub(super) struct Opts { #[clap(long)] long: bool, + /// Show snapshots in json format + #[clap(long, conflicts_with = "long")] + json: bool, + /// Show all snapshots instead of summarizing identical follow-up snapshots - #[clap(long)] + #[clap(long, conflicts_with_all = &["long", "json"])] all: bool, /// Snapshots to show @@ -67,6 +71,12 @@ pub(super) async fn execute( )], }; + if opts.json { + let mut stdout = std::io::stdout(); + serde_json::to_writer_pretty(&mut stdout, &groups)?; + return Ok(()); + } + for (group, mut snapshots) in groups { if !group.is_empty() { println!("\nsnapshots for {:?}", group); diff --git a/src/repo/snapshotfile.rs b/src/repo/snapshotfile.rs index c30a03d..1a9df24 100644 --- a/src/repo/snapshotfile.rs +++ b/src/repo/snapshotfile.rs @@ -90,7 +90,7 @@ pub struct SnapshotFile { #[serde(default, skip_serializing_if = "Option::is_none")] pub summary: Option, - #[serde(skip)] + #[serde(default, skip_serializing_if = "Id::is_null")] pub id: Id, } @@ -356,10 +356,13 @@ impl FromStr for SnapshotGroupCriterion { } } -#[derive(Default, Debug)] +#[derive(Default, Debug, Serialize)] pub struct SnapshotGroup { + #[serde(default, skip_serializing_if = "Option::is_none")] hostname: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] paths: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] tags: Option, }