Skip to content

Commit 75bfac0

Browse files
committed
feat: implement history command with filtering for backup records
1 parent 8a33d0c commit 75bfac0

File tree

7 files changed

+153
-118
lines changed

7 files changed

+153
-118
lines changed

src/addons.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use crate::{
2020
scan_xss::ScanXSS,
2121
pastebin::Pastebin,
2222
checksum::Checksum,
23+
history_logs::HistoryLogs,
2324
},
2425
};
2526

@@ -113,4 +114,17 @@ impl DumpSyncAddons {
113114
).diagram().await;
114115
}
115116

117+
pub fn history(&self, history_type: &str, filter: Option<String>) {
118+
Env::new();
119+
UI::header();
120+
121+
match history_type {
122+
"backups" => {
123+
UI::section_header("Backup History", "info");
124+
HistoryLogs::new().backups(filter);
125+
},
126+
_ => UI::label("Unknown history type", "error"),
127+
}
128+
}
129+
116130
}

src/args_cli.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,13 @@ pub enum Commands {
7070
},
7171

7272
/// History of backups
73-
History,
73+
History {
74+
/// Type of history
75+
history_type: String,
76+
77+
/// Filter by different fields
78+
filter: Option<String>,
79+
},
7480

7581
/// Login to DumpSync Cloud
7682
Login,

src/dump_sync.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ impl DumpSync {
1919

2020
match Cli::parse().command {
2121
Commands::Init => DumpSyncInit.initialize().await?,
22-
23-
Commands::History => DumpSyncDumper.history(),
24-
22+
2523
Commands::Export(options) => DumpSyncDumper.export(options),
2624
Commands::Import(options) => DumpSyncDumper.import(options),
2725
Commands::Transfer(options) => DumpSyncDumper.transfer(options),
@@ -32,6 +30,7 @@ impl DumpSync {
3230
Commands::Visual(options) => DumpSyncAddons.visual(options).await,
3331
Commands::Share(options) => DumpSyncAddons.share(options).await?,
3432
Commands::Scan(options) => DumpSyncAddons.scan_xss(options).await?,
33+
Commands::History { history_type, filter } => DumpSyncAddons.history(&history_type, filter),
3534

3635
Commands::Pull { file } => DumpSyncService.pull(&file).await,
3736
Commands::Push { file } => DumpSyncService.push(&file).await,

src/dumper.rs

Lines changed: 2 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
11
use std::env;
2-
use std::cmp;
32

43
use crate::{
54
args_cli::*,
5+
ui::ui_base::UI,
6+
helpers::env::Env,
67
init::DumpSyncInit,
78

89
core::{
910
dump::Dump,
1011
truncate::Truncate,
1112
},
12-
13-
helpers::{
14-
env::Env,
15-
history::History,
16-
},
17-
18-
ui::ui_base::UI,
1913
};
2014

2115
pub struct DumpSyncDumper;
@@ -90,108 +84,4 @@ impl DumpSyncDumper {
9084
).transfer();
9185
}
9286

93-
pub fn history(&self) {
94-
Env::new();
95-
UI::header();
96-
97-
UI::section_header("Backup History", "info");
98-
let items = History::new().list_backups_with_filters(None);
99-
100-
match items {
101-
Ok(backups) => {
102-
if backups.is_empty() {
103-
UI::label("No backups found", "warning");
104-
} else {
105-
let mut max_id = 2;
106-
let mut max_slug = 4;
107-
let mut max_db = 8;
108-
let mut max_filename = 8;
109-
let mut max_host = 4;
110-
let mut max_date = 10;
111-
let mut max_size = 4;
112-
let mut max_encrypt = 7;
113-
let mut max_compress = 8;
114-
115-
for (id, slug, db, filename, host, created_at, size, encrypt, compress) in &backups {
116-
max_id = cmp::max(max_id, id.to_string().len());
117-
max_slug = cmp::max(max_slug, slug.len());
118-
max_db = cmp::max(max_db, db.len());
119-
max_filename = cmp::max(max_filename, filename.len());
120-
max_host = cmp::max(max_host, host.len());
121-
122-
let replaced = created_at.replace("T", " ");
123-
let date = replaced.split('.').next().unwrap_or(&created_at);
124-
125-
max_date = cmp::max(max_date, date.len());
126-
max_size = cmp::max(max_size, size.to_string().len());
127-
max_encrypt = cmp::max(max_encrypt, encrypt.to_string().len());
128-
max_compress = cmp::max(max_compress, compress.to_string().len());
129-
}
130-
131-
let print_row = |id: &str, slug: &str, db: &str, filename: &str, host: &str, date: &str, size: &str, encrypt: &str, compress: &str| {
132-
println!(
133-
"| {:<idw$} | {:<slugw$} | {:<dbw$} | {:<fnw$} | {:<hostw$} | {:<datew$} | {:>sizew$} | {:<encw$} | {:<compw$} |",
134-
id,
135-
slug,
136-
db,
137-
filename,
138-
host,
139-
date,
140-
size,
141-
encrypt,
142-
compress,
143-
idw = max_id,
144-
slugw = max_slug,
145-
dbw = max_db,
146-
fnw = max_filename,
147-
hostw = max_host,
148-
datew = max_date,
149-
sizew = max_size,
150-
encw = max_encrypt,
151-
compw = max_compress
152-
);
153-
};
154-
155-
let sep = format!(
156-
"+-{:-<idw$}-+-{:-<slugw$}-+-{:-<dbw$}-+-{:-<fnw$}-+-{:-<hostw$}-+-{:-<datew$}-+-{:-<sizew$}-+-{:-<encw$}-+-{:-<compw$}-+",
157-
"", "", "", "", "", "", "", "", "",
158-
idw = max_id,
159-
slugw = max_slug,
160-
dbw = max_db,
161-
fnw = max_filename,
162-
hostw = max_host,
163-
datew = max_date,
164-
sizew = max_size,
165-
encw = max_encrypt,
166-
compw = max_compress
167-
);
168-
169-
println!("{}", sep);
170-
print_row(
171-
"ID", "Slug", "Database", "Filename", "Host", "Created At", "Size", "Encrypt", "Compress"
172-
);
173-
174-
println!("{}", sep);
175-
for (id, slug, db, filename, host, created_at, size, encrypt, compress) in backups {
176-
let replaced = created_at.replace("T", " ");
177-
let date = replaced.split('.').next().unwrap_or(&created_at);
178-
print_row(
179-
&id.to_string(),
180-
&slug,
181-
&db,
182-
&filename,
183-
&host,
184-
date,
185-
&size.to_string(),
186-
if encrypt { "true" } else { "false" },
187-
if compress { "true" } else { "false" },
188-
);
189-
}
190-
println!("{}", sep);
191-
}
192-
},
193-
Err(e) => UI::label(&format!("Error fetching history: {}", e), "error"),
194-
}
195-
}
196-
19787
}

src/helpers/history.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ impl History {
5353

5454
pub fn list_backups_with_filters(&self, filter: Option<&str>) -> Result<Vec<(i64, String, String, String, String, String, i64, bool, bool)>> {
5555
let conn = Connection::open(&self.db_path)?;
56-
let mut stmt = conn.prepare("SELECT id, slug, db, filename, host, created_at, size, encrypt, compress FROM backups WHERE slug LIKE ?1 OR filename LIKE ?1 OR created_at LIKE ?1 OR db LIKE ?1 OR host LIKE ?1")?;
56+
let mut stmt = conn.prepare("SELECT id, slug, db, filename, host, created_at, size, encrypt, compress FROM backups WHERE id LIKE ?1 OR slug LIKE ?1 OR filename LIKE ?1 OR created_at LIKE ?1 OR db LIKE ?1 OR host LIKE ?1")?;
5757

5858
let backups = stmt
5959
.query_map(params![format!("%{}%", filter.unwrap_or(""))], |row| {

src/plugins/history_logs.rs

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
use std::cmp;
2+
3+
use crate::{
4+
ui::ui_base::UI,
5+
6+
helpers::{
7+
env::Env,
8+
history::History,
9+
},
10+
};
11+
12+
pub struct HistoryLogs;
13+
14+
impl HistoryLogs {
15+
16+
pub fn new() -> Self {
17+
HistoryLogs
18+
}
19+
20+
pub fn backups(&self, filter: Option<String>) {
21+
Env::new();
22+
UI::header();
23+
24+
UI::section_header("Backup History", "info");
25+
let items = History::new().list_backups_with_filters(Some(filter.as_deref().unwrap_or("")));
26+
27+
match items {
28+
Ok(backups) => {
29+
if backups.is_empty() {
30+
UI::label("No backups found", "warning");
31+
} else {
32+
let mut max_id = 2;
33+
let mut max_slug = 4;
34+
let mut max_db = 8;
35+
let mut max_filename = 8;
36+
let mut max_host = 4;
37+
let mut max_date = 10;
38+
let mut max_size = 4;
39+
let mut max_encrypt = 7;
40+
let mut max_compress = 8;
41+
42+
for (id, slug, db, filename, host, created_at, size, encrypt, compress) in &backups {
43+
max_id = cmp::max(max_id, id.to_string().len());
44+
max_slug = cmp::max(max_slug, slug.len());
45+
max_db = cmp::max(max_db, db.len());
46+
max_filename = cmp::max(max_filename, filename.len());
47+
max_host = cmp::max(max_host, host.len());
48+
49+
let replaced = created_at.replace("T", " ");
50+
let date = replaced.split('.').next().unwrap_or(&created_at);
51+
52+
max_date = cmp::max(max_date, date.len());
53+
max_size = cmp::max(max_size, size.to_string().len());
54+
max_encrypt = cmp::max(max_encrypt, encrypt.to_string().len());
55+
max_compress = cmp::max(max_compress, compress.to_string().len());
56+
}
57+
58+
let print_row = |id: &str, slug: &str, db: &str, filename: &str, host: &str, date: &str, size: &str, encrypt: &str, compress: &str| {
59+
println!(
60+
"| {:<idw$} | {:<slugw$} | {:<dbw$} | {:<fnw$} | {:<hostw$} | {:<datew$} | {:>sizew$} | {:<encw$} | {:<compw$} |",
61+
id,
62+
slug,
63+
db,
64+
filename,
65+
host,
66+
date,
67+
size,
68+
encrypt,
69+
compress,
70+
idw = max_id,
71+
slugw = max_slug,
72+
dbw = max_db,
73+
fnw = max_filename,
74+
hostw = max_host,
75+
datew = max_date,
76+
sizew = max_size,
77+
78+
encw = max_encrypt,
79+
compw = max_compress
80+
);
81+
};
82+
83+
let sep = format!(
84+
"+-{:-<idw$}-+-{:-<slugw$}-+-{:-<dbw$}-+-{:-<fnw$}-+-{:-<hostw$}-+-{:-<datew$}-+-{:-<sizew$}-+-{:-<encw$}-+-{:-<compw$}-+",
85+
"", "", "", "", "", "", "", "", "",
86+
idw = max_id,
87+
slugw = max_slug,
88+
dbw = max_db,
89+
fnw = max_filename,
90+
hostw = max_host,
91+
datew = max_date,
92+
sizew = max_size,
93+
encw = max_encrypt,
94+
compw = max_compress
95+
);
96+
97+
println!("{}", sep);
98+
print_row(
99+
"ID", "Slug", "Database", "Filename", "Host", "Created At", "Size", "Encrypt", "Compress"
100+
);
101+
102+
println!("{}", sep);
103+
for (id, slug, db, filename, host, created_at, size, encrypt, compress) in backups {
104+
let replaced = created_at.replace("T", " ");
105+
let date = replaced.split('.').next().unwrap_or(&created_at);
106+
print_row(
107+
&id.to_string(),
108+
&slug,
109+
&db,
110+
&filename,
111+
&host,
112+
date,
113+
&size.to_string(),
114+
if encrypt { "true" } else { "false" },
115+
if compress { "true" } else { "false" },
116+
);
117+
}
118+
println!("{}", sep);
119+
}
120+
},
121+
Err(e) => UI::label(&format!("Error fetching history: {}", e), "error"),
122+
}
123+
}
124+
125+
}

src/plugins/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ pub mod pastebin;
44
pub mod scan_xss;
55
pub mod checksum;
66
pub mod reports_xss;
7-
pub mod reports_pdf;
7+
pub mod reports_pdf;
8+
pub mod history_logs;

0 commit comments

Comments
 (0)