mirror of
https://github.com/rustic-rs/rustic.git
synced 2025-10-26 11:18:51 +00:00
feat(commands): add --json option to forget command (#806)
Co-authored-by: simonsan <14062932+simonsan@users.noreply.github.com>
This commit is contained in:
parent
ae8bb211e2
commit
f528337113
@ -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.
|
||||
@ -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<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,
|
||||
|
||||
@ -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,
|
||||
|
||||
@ -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!();
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user