Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(commands)!: Fine-tune json output for snapshots command #1375

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions src/commands/snapshots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ use crate::{
use abscissa_core::{Command, Runnable, Shutdown};
use anyhow::Result;
use comfy_table::Cell;
use derive_more::From;
use humantime::format_duration;
use itertools::Itertools;
use serde::Serialize;

use rustic_core::{
repofile::{DeleteOption, SnapshotFile},
SnapshotGroupCriterion,
SnapshotGroup, SnapshotGroupCriterion,
};

#[cfg(feature = "tui")]
Expand Down Expand Up @@ -81,15 +83,26 @@ impl SnapshotCmd {
})?;

if self.json {
#[derive(Serialize, From)]
struct SnapshotsGroup {
group_key: SnapshotGroup,
snapshots: Vec<SnapshotFile>,
}
let groups: Vec<SnapshotsGroup> = groups.into_iter().map(|g| g.into()).collect();
let mut stdout = std::io::stdout();
serde_json::to_writer_pretty(&mut stdout, &groups)?;
if groups.len() == 1 && groups[0].group_key.is_empty() {
// we don't use grouping, only output snapshots list
serde_json::to_writer_pretty(&mut stdout, &groups[0].snapshots)?;
} else {
serde_json::to_writer_pretty(&mut stdout, &groups)?;
}
return Ok(());
}

let mut total_count = 0;
for (group, mut snapshots) in groups {
if !group.is_empty() {
println!("\nsnapshots for {group}");
for (group_key, mut snapshots) in groups {
if !group_key.is_empty() {
println!("\nsnapshots for {group_key}");
}
snapshots.sort_unstable();
let count = snapshots.len();
Expand Down
Loading