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

forget: Option --json #806

Merged
merged 3 commits into from Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion changelog/new.txt
Expand Up @@ -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.
- ls: Options --long (-l) and --summary (-s) have been added.
- forget: Option --json has been added.
8 changes: 4 additions & 4 deletions crates/rustic_core/src/commands/forget.rs
Expand Up @@ -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::{
Expand All @@ -12,16 +12,16 @@ use crate::{

type CheckFunction = fn(&SnapshotFile, &SnapshotFile) -> bool;

#[derive(Debug)]
#[derive(Debug, Serialize)]
pub struct ForgetGroups(pub Vec<ForgetGroup>);

#[derive(Debug)]
#[derive(Debug, Serialize)]
pub struct ForgetGroup {
pub group: SnapshotGroup,
pub snapshots: Vec<ForgetSnapshot>,
}

#[derive(Debug)]
#[derive(Debug, Serialize)]
pub struct ForgetSnapshot {
pub snapshot: SnapshotFile,
pub keep: bool,
Expand Down
90 changes: 52 additions & 38 deletions src/commands/forget.rs
Expand Up @@ -28,6 +28,10 @@ pub(super) struct ForgetCmd {
#[clap(value_name = "ID")]
ids: Vec<String>,

/// Show infos in json format
#[clap(long)]
json: bool,

#[clap(flatten)]
config: ForgetOptions,

Expand Down Expand Up @@ -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 {
Expand All @@ -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!();
}
}