Skip to content

Commit 7a7a38b

Browse files
committed
format-code
1 parent 1bade89 commit 7a7a38b

11 files changed

Lines changed: 273 additions & 140 deletions

File tree

src/cleanup.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1+
pub mod dev;
12
pub mod docker;
23
pub mod temp;
3-
pub mod dev;

src/cleanup/dev.rs

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use anyhow::Result;
2-
use std::path::Path;
3-
use std::fs;
4-
use dialoguer::Confirm;
51
use crate::discovery::{DevArtifactFinder, FileItem};
62
use crate::utils::format_size;
3+
use anyhow::Result;
4+
use dialoguer::Confirm;
5+
use std::fs;
6+
use std::path::Path;
77

88
pub async fn cleanup(path: Option<String>, dry_run: bool) -> Result<()> {
99
let target_path = path.unwrap_or_else(|| ".".to_string());
@@ -32,7 +32,10 @@ pub async fn cleanup(path: Option<String>, dry_run: bool) -> Result<()> {
3232
println!(
3333
"{:<60} {:>15} {:>10}",
3434
if artifact.path.to_string_lossy().len() > 57 {
35-
format!("...{}", &artifact.path.to_string_lossy()[artifact.path.to_string_lossy().len()-54..])
35+
format!(
36+
"...{}",
37+
&artifact.path.to_string_lossy()[artifact.path.to_string_lossy().len() - 54..]
38+
)
3639
} else {
3740
artifact.path.to_string_lossy().to_string()
3841
},
@@ -47,14 +50,20 @@ pub async fn cleanup(path: Option<String>, dry_run: bool) -> Result<()> {
4750
println!(" Total items: {}", total_items);
4851

4952
if dry_run {
50-
println!("\n[DRY RUN] Would remove {} development artifacts ({})",
51-
artifacts.len(), format_size(total_size));
53+
println!(
54+
"\n[DRY RUN] Would remove {} development artifacts ({})",
55+
artifacts.len(),
56+
format_size(total_size)
57+
);
5258
return Ok(());
5359
}
5460

5561
if Confirm::new()
56-
.with_prompt(&format!("Remove {} development artifacts ({})?",
57-
artifacts.len(), format_size(total_size)))
62+
.with_prompt(&format!(
63+
"Remove {} development artifacts ({})?",
64+
artifacts.len(),
65+
format_size(total_size)
66+
))
5867
.interact()?
5968
{
6069
remove_artifacts(artifacts).await?;
@@ -66,7 +75,7 @@ pub async fn cleanup(path: Option<String>, dry_run: bool) -> Result<()> {
6675

6776
async fn remove_artifacts(artifacts: Vec<FileItem>) -> Result<()> {
6877
let artifacts_clone = artifacts.clone();
69-
78+
7079
tokio::task::spawn_blocking(move || {
7180
let mut removed_count = 0;
7281
let mut removed_size = 0u64;
@@ -77,7 +86,7 @@ async fn remove_artifacts(artifacts: Vec<FileItem>) -> Result<()> {
7786
removed_count += 1;
7887
removed_size += artifact.size;
7988
println!(" ✅ Removed: {}", artifact.path.display());
80-
},
89+
}
8190
Err(e) => {
8291
eprintln!(" ❌ Failed to remove {}: {}", artifact.path.display(), e);
8392
}
@@ -91,7 +100,8 @@ async fn remove_artifacts(artifacts: Vec<FileItem>) -> Result<()> {
91100
}
92101

93102
Ok(())
94-
}).await?
103+
})
104+
.await?
95105
}
96106

97107
fn remove_dir_all_safe(path: &Path) -> Result<()> {
@@ -101,28 +111,40 @@ fn remove_dir_all_safe(path: &Path) -> Result<()> {
101111
}
102112

103113
if !path.is_dir() {
104-
return Err(anyhow::anyhow!("Path is not a directory: {}", path.display()));
114+
return Err(anyhow::anyhow!(
115+
"Path is not a directory: {}",
116+
path.display()
117+
));
105118
}
106119

107120
// Check if it's actually a development artifact directory
108-
let dir_name = path.file_name()
109-
.and_then(|n| n.to_str())
110-
.unwrap_or("");
121+
let dir_name = path.file_name().and_then(|n| n.to_str()).unwrap_or("");
111122

112123
let safe_dirs = [
113-
"node_modules", ".venv", "venv", "__pycache__",
114-
".tox", "target", "build", "dist"
124+
"node_modules",
125+
".venv",
126+
"venv",
127+
"__pycache__",
128+
".tox",
129+
"target",
130+
"build",
131+
"dist",
115132
];
116133

117134
if !safe_dirs.contains(&dir_name) {
118-
return Err(anyhow::anyhow!("Directory name '{}' is not in the safe removal list", dir_name));
135+
return Err(anyhow::anyhow!(
136+
"Directory name '{}' is not in the safe removal list",
137+
dir_name
138+
));
119139
}
120140

121141
// Additional check: ensure we're not at filesystem root
122142
if path.parent().is_none() {
123-
return Err(anyhow::anyhow!("Refusing to remove directory at filesystem root"));
143+
return Err(anyhow::anyhow!(
144+
"Refusing to remove directory at filesystem root"
145+
));
124146
}
125147

126148
fs::remove_dir_all(path)?;
127149
Ok(())
128-
}
150+
}

src/cleanup/docker.rs

Lines changed: 80 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use anyhow::{anyhow, Result};
2-
use tokio::process::Command as AsyncCommand;
32
use dialoguer::Confirm;
3+
use tokio::process::Command as AsyncCommand;
44

55
pub async fn cleanup(dry_run: bool) -> Result<()> {
66
println!("🐳 Docker Safe Cleanup");
@@ -12,13 +12,13 @@ pub async fn cleanup(dry_run: bool) -> Result<()> {
1212

1313
// Check for unused containers
1414
cleanup_containers(dry_run).await?;
15-
15+
1616
// Check for unused images
1717
cleanup_images(dry_run).await?;
18-
18+
1919
// Check for unused volumes
2020
cleanup_volumes(dry_run).await?;
21-
21+
2222
// Check for unused networks
2323
cleanup_networks(dry_run).await?;
2424

@@ -40,24 +40,34 @@ async fn is_docker_available() -> Result<bool> {
4040

4141
async fn cleanup_containers(dry_run: bool) -> Result<()> {
4242
println!("\n📦 Checking for stopped containers...");
43-
43+
4444
let output = AsyncCommand::new("docker")
45-
.args(["ps", "-a", "--filter", "status=exited", "--format", "table {{.ID}}\\t{{.Image}}\\t{{.Status}}"])
45+
.args([
46+
"ps",
47+
"-a",
48+
"--filter",
49+
"status=exited",
50+
"--format",
51+
"table {{.ID}}\\t{{.Image}}\\t{{.Status}}",
52+
])
4653
.output()
4754
.await?;
4855

4956
let stdout = String::from_utf8_lossy(&output.stdout);
5057
let lines: Vec<&str> = stdout.lines().collect();
51-
58+
5259
if lines.len() <= 1 {
5360
println!(" No stopped containers found.");
5461
return Ok(());
5562
}
5663

5764
println!("{}", stdout);
58-
65+
5966
if dry_run {
60-
println!(" [DRY RUN] Would remove {} stopped containers", lines.len() - 1);
67+
println!(
68+
" [DRY RUN] Would remove {} stopped containers",
69+
lines.len() - 1
70+
);
6171
return Ok(());
6272
}
6373

@@ -73,7 +83,10 @@ async fn cleanup_containers(dry_run: bool) -> Result<()> {
7383
if result.status.success() {
7484
println!(" ✅ Stopped containers removed successfully");
7585
} else {
76-
println!(" ❌ Failed to remove containers: {}", String::from_utf8_lossy(&result.stderr));
86+
println!(
87+
" ❌ Failed to remove containers: {}",
88+
String::from_utf8_lossy(&result.stderr)
89+
);
7790
}
7891
}
7992

@@ -82,24 +95,33 @@ async fn cleanup_containers(dry_run: bool) -> Result<()> {
8295

8396
async fn cleanup_images(dry_run: bool) -> Result<()> {
8497
println!("\n🖼️ Checking for unused images...");
85-
98+
8699
let output = AsyncCommand::new("docker")
87-
.args(["images", "--filter", "dangling=true", "--format", "table {{.ID}}\\t{{.Repository}}\\t{{.Tag}}\\t{{.Size}}"])
100+
.args([
101+
"images",
102+
"--filter",
103+
"dangling=true",
104+
"--format",
105+
"table {{.ID}}\\t{{.Repository}}\\t{{.Tag}}\\t{{.Size}}",
106+
])
88107
.output()
89108
.await?;
90109

91110
let stdout = String::from_utf8_lossy(&output.stdout);
92111
let lines: Vec<&str> = stdout.lines().collect();
93-
112+
94113
if lines.len() <= 1 {
95114
println!(" No dangling images found.");
96115
return Ok(());
97116
}
98117

99118
println!("{}", stdout);
100-
119+
101120
if dry_run {
102-
println!(" [DRY RUN] Would remove {} dangling images", lines.len() - 1);
121+
println!(
122+
" [DRY RUN] Would remove {} dangling images",
123+
lines.len() - 1
124+
);
103125
return Ok(());
104126
}
105127

@@ -115,7 +137,10 @@ async fn cleanup_images(dry_run: bool) -> Result<()> {
115137
if result.status.success() {
116138
println!(" ✅ Dangling images removed successfully");
117139
} else {
118-
println!(" ❌ Failed to remove images: {}", String::from_utf8_lossy(&result.stderr));
140+
println!(
141+
" ❌ Failed to remove images: {}",
142+
String::from_utf8_lossy(&result.stderr)
143+
);
119144
}
120145
}
121146

@@ -124,24 +149,34 @@ async fn cleanup_images(dry_run: bool) -> Result<()> {
124149

125150
async fn cleanup_volumes(dry_run: bool) -> Result<()> {
126151
println!("\n💾 Checking for unused volumes...");
127-
152+
128153
let output = AsyncCommand::new("docker")
129-
.args(["volume", "ls", "--filter", "dangling=true", "--format", "table {{.Name}}\\t{{.Driver}}\\t{{.Scope}}"])
154+
.args([
155+
"volume",
156+
"ls",
157+
"--filter",
158+
"dangling=true",
159+
"--format",
160+
"table {{.Name}}\\t{{.Driver}}\\t{{.Scope}}",
161+
])
130162
.output()
131163
.await?;
132164

133165
let stdout = String::from_utf8_lossy(&output.stdout);
134166
let lines: Vec<&str> = stdout.lines().collect();
135-
167+
136168
if lines.len() <= 1 {
137169
println!(" No unused volumes found.");
138170
return Ok(());
139171
}
140172

141173
println!("{}", stdout);
142-
174+
143175
if dry_run {
144-
println!(" [DRY RUN] Would remove {} unused volumes", lines.len() - 1);
176+
println!(
177+
" [DRY RUN] Would remove {} unused volumes",
178+
lines.len() - 1
179+
);
145180
return Ok(());
146181
}
147182

@@ -157,7 +192,10 @@ async fn cleanup_volumes(dry_run: bool) -> Result<()> {
157192
if result.status.success() {
158193
println!(" ✅ Unused volumes removed successfully");
159194
} else {
160-
println!(" ❌ Failed to remove volumes: {}", String::from_utf8_lossy(&result.stderr));
195+
println!(
196+
" ❌ Failed to remove volumes: {}",
197+
String::from_utf8_lossy(&result.stderr)
198+
);
161199
}
162200
}
163201

@@ -166,24 +204,34 @@ async fn cleanup_volumes(dry_run: bool) -> Result<()> {
166204

167205
async fn cleanup_networks(dry_run: bool) -> Result<()> {
168206
println!("\n🌐 Checking for unused networks...");
169-
207+
170208
let output = AsyncCommand::new("docker")
171-
.args(["network", "ls", "--filter", "dangling=true", "--format", "table {{.ID}}\\t{{.Name}}\\t{{.Driver}}"])
209+
.args([
210+
"network",
211+
"ls",
212+
"--filter",
213+
"dangling=true",
214+
"--format",
215+
"table {{.ID}}\\t{{.Name}}\\t{{.Driver}}",
216+
])
172217
.output()
173218
.await?;
174219

175220
let stdout = String::from_utf8_lossy(&output.stdout);
176221
let lines: Vec<&str> = stdout.lines().collect();
177-
222+
178223
if lines.len() <= 1 {
179224
println!(" No unused networks found.");
180225
return Ok(());
181226
}
182227

183228
println!("{}", stdout);
184-
229+
185230
if dry_run {
186-
println!(" [DRY RUN] Would remove {} unused networks", lines.len() - 1);
231+
println!(
232+
" [DRY RUN] Would remove {} unused networks",
233+
lines.len() - 1
234+
);
187235
return Ok(());
188236
}
189237

@@ -199,9 +247,12 @@ async fn cleanup_networks(dry_run: bool) -> Result<()> {
199247
if result.status.success() {
200248
println!(" ✅ Unused networks removed successfully");
201249
} else {
202-
println!(" ❌ Failed to remove networks: {}", String::from_utf8_lossy(&result.stderr));
250+
println!(
251+
" ❌ Failed to remove networks: {}",
252+
String::from_utf8_lossy(&result.stderr)
253+
);
203254
}
204255
}
205256

206257
Ok(())
207-
}
258+
}

0 commit comments

Comments
 (0)