From f528337113da259538dacfa96db4de5b67ff64c4 Mon Sep 17 00:00:00 2001 From: aawsome <37850842+aawsome@users.noreply.github.com> Date: Wed, 16 Aug 2023 18:41:13 +0200 Subject: [PATCH] feat(commands): add `--json` option to `forget` command (#806) Co-authored-by: simonsan <14062932+simonsan@users.noreply.github.com> --- changelog/new.txt | 3 +- crates/rustic_core/src/commands/forget.rs | 8 +- src/commands/forget.rs | 90 +++++++++++++---------- 3 files changed, 58 insertions(+), 43 deletions(-) diff --git a/changelog/new.txt b/changelog/new.txt index ae442a4..144065c 100644 --- a/changelog/new.txt +++ b/changelog/new.txt @@ -19,4 +19,5 @@ New features: - repoinfo: Added new options --json, --only-files, --only-index - Creation of new keys now enforces confirmation of entered key. This helps to prevent mistype of passwords during the initial entry - Check: Add check if time is set for packs-to-delete -- ls: Options --long (-l) and --summary (-s) have been added. \ No newline at end of file +- ls: Options --long (-l) and --summary (-s) have been added. +- forget: Option --json has been added. \ No newline at end of file diff --git a/crates/rustic_core/src/commands/forget.rs b/crates/rustic_core/src/commands/forget.rs index 76eb27a..2eef6e5 100644 --- a/crates/rustic_core/src/commands/forget.rs +++ b/crates/rustic_core/src/commands/forget.rs @@ -2,7 +2,7 @@ use chrono::{DateTime, Datelike, Duration, Local, Timelike}; use derivative::Derivative; -use serde::Deserialize; +use serde::{Deserialize, Serialize}; use serde_with::{serde_as, DisplayFromStr}; use crate::{ @@ -12,16 +12,16 @@ use crate::{ type CheckFunction = fn(&SnapshotFile, &SnapshotFile) -> bool; -#[derive(Debug)] +#[derive(Debug, Serialize)] pub struct ForgetGroups(pub Vec); -#[derive(Debug)] +#[derive(Debug, Serialize)] pub struct ForgetGroup { pub group: SnapshotGroup, pub snapshots: Vec, } -#[derive(Debug)] +#[derive(Debug, Serialize)] pub struct ForgetSnapshot { pub snapshot: SnapshotFile, pub keep: bool, diff --git a/src/commands/forget.rs b/src/commands/forget.rs index 8a993ea..86dd94a 100644 --- a/src/commands/forget.rs +++ b/src/commands/forget.rs @@ -28,6 +28,10 @@ pub(super) struct ForgetCmd { #[clap(value_name = "ID")] ids: Vec, + /// Show infos in json format + #[clap(long)] + json: bool, + #[clap(flatten)] config: ForgetOptions, @@ -112,52 +116,24 @@ impl ForgetCmd { ForgetGroups(vec![item]) }; - for ForgetGroup { group, snapshots } in &groups.0 { - if !group.is_empty() { - println!("snapshots for {group}"); - } - let mut table = table_with_titles([ - "ID", "Time", "Host", "Label", "Tags", "Paths", "Action", "Reason", - ]); - - for ForgetSnapshot { - snapshot: sn, - keep, - reasons, - } in snapshots - { - let time = sn.time.format("%Y-%m-%d %H:%M:%S").to_string(); - let tags = sn.tags.formatln(); - let paths = sn.paths.formatln(); - let action = if *keep { "keep" } else { "remove" }; - let reason = reasons.join("\n"); - _ = table.add_row([ - &sn.id.to_string(), - &time, - &sn.hostname, - &sn.label, - &tags, - &paths, - action, - &reason, - ]); - } - - println!(); - println!("{table}"); - println!(); + if self.json { + let mut stdout = std::io::stdout(); + serde_json::to_writer_pretty(&mut stdout, &groups)?; + } else { + print_groups(&groups); } let forget_snaps = groups.into_forget_ids(); - match (forget_snaps.is_empty(), config.global.dry_run) { - (true, _) => println!("nothing to remove"), - (false, true) => { + match (forget_snaps.is_empty(), config.global.dry_run, self.json) { + (true, _, false) => println!("nothing to remove"), + (false, true, false) => { println!("would have removed the following snapshots:\n {forget_snaps:?}"); } - (false, false) => { + (false, false, _) => { repo.delete_snapshots(&forget_snaps)?; } + (_, _, true) => {} } if self.config.prune { @@ -169,3 +145,41 @@ impl ForgetCmd { Ok(()) } } + +fn print_groups(groups: &ForgetGroups) { + for ForgetGroup { group, snapshots } in &groups.0 { + if !group.is_empty() { + println!("snapshots for {group}"); + } + let mut table = table_with_titles([ + "ID", "Time", "Host", "Label", "Tags", "Paths", "Action", "Reason", + ]); + + for ForgetSnapshot { + snapshot: sn, + keep, + reasons, + } in snapshots + { + let time = sn.time.format("%Y-%m-%d %H:%M:%S").to_string(); + let tags = sn.tags.formatln(); + let paths = sn.paths.formatln(); + let action = if *keep { "keep" } else { "remove" }; + let reason = reasons.join("\n"); + _ = table.add_row([ + &sn.id.to_string(), + &time, + &sn.hostname, + &sn.label, + &tags, + &paths, + action, + &reason, + ]); + } + + println!(); + println!("{table}"); + println!(); + } +}