-
Notifications
You must be signed in to change notification settings - Fork 31
/
main.rs
234 lines (213 loc) · 7.59 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
use anyhow::Context;
use cargo_metadata::{Error, Metadata, MetadataCommand};
use crossterm::tty::IsTty;
use fern::colors::{Color, ColoredLevelConfig};
use log::{debug, error, info};
use std::{
env,
ffi::OsStr,
path::{Path, PathBuf},
time::Duration,
};
use walkdir::WalkDir;
mod cli;
mod fingerprint;
mod stamp;
mod util;
use self::cli::Criterion;
use self::fingerprint::{remove_not_built_with, remove_older_than, remove_older_until_fits};
use self::stamp::Timestamp;
use self::util::format_bytes;
/// Setup logging according to verbose flag.
fn setup_logging(verbose: bool) {
// configure colors for the whole line
let colors_line = ColoredLevelConfig::new()
.error(Color::Red)
.warn(Color::Yellow)
.info(Color::White)
.debug(Color::White)
.trace(Color::BrightBlack);
let level = if verbose {
log::LevelFilter::Debug
} else {
log::LevelFilter::Info
};
let isatty = std::io::stdout().is_tty();
let colors_level = colors_line.info(Color::Green);
fern::Dispatch::new()
.format(move |out, message, record| {
if isatty {
out.finish(format_args!(
"{color_line}[{level}{color_line}] {message}\x1B[0m",
color_line = format_args!(
"\x1B[{}m",
colors_line.get_color(&record.level()).to_fg_str()
),
level = colors_level.color(record.level()),
message = message,
))
} else {
out.finish(format_args!("[{}] {message}", record.level()))
}
})
.level(level)
.level_for("pretty_colored", log::LevelFilter::Trace)
.chain(std::io::stdout())
.apply()
.unwrap();
}
/// Returns whether the given path to a Cargo.toml points to a real target directory.
fn is_cargo_root(path: &Path) -> Option<PathBuf> {
if let Ok(metadata) = metadata(path) {
let out = Path::new(&metadata.target_directory).to_path_buf();
if out.exists() {
return Some(out);
}
}
None
}
/// is a `DirEntry` a unix stile hidden file, ie starts with `.`
fn is_hidden(entry: &walkdir::DirEntry) -> bool {
entry
.file_name()
.to_str()
.map(|s| s.starts_with('.'))
.unwrap_or(false)
}
/// Find all cargo project under the given root path.
fn find_cargo_projects(root: &Path, include_hidden: bool) -> Vec<PathBuf> {
let mut target_paths = std::collections::BTreeSet::new();
let mut iter = WalkDir::new(root).min_depth(1).into_iter();
while let Some(entry) = iter.next() {
if let Ok(entry) = entry {
if entry.file_type().is_dir() {
if !include_hidden && is_hidden(&entry) {
debug!("skip hidden folder {}", entry.path().display());
iter.skip_current_dir();
continue;
}
if entry.path().ancestors().any(|a| target_paths.contains(a)) {
// no reason to look at the contents of something we are already cleaning.
// Yes ancestors is a inefficient way to check. We can use a trie or something if it is slow.
iter.skip_current_dir();
continue;
}
}
if entry.file_name() != "Cargo.toml" {
continue;
}
if let Some(target_directory) = is_cargo_root(entry.path()) {
target_paths.insert(target_directory);
iter.skip_current_dir(); // no reason to look at the src and such
}
}
}
target_paths.into_iter().collect()
}
fn metadata(path: &Path) -> Result<Metadata, Error> {
let manifest_path = if path.file_name().and_then(OsStr::to_str) == Some("Cargo.toml") {
path.to_owned()
} else {
path.join("Cargo.toml")
};
MetadataCommand::new()
.manifest_path(manifest_path)
.no_deps()
.exec()
}
fn main() -> anyhow::Result<()> {
let args = cli::parse();
let criterion = args.criterion();
let dry_run = args.dry_run;
setup_logging(args.verbose);
// Default to current invocation path.
let path = args
.path
.unwrap_or_else(|| env::current_dir().expect("Failed to get current directory"));
if let Criterion::Stamp = criterion {
debug!("Writing timestamp file in: {:?}", path);
return Timestamp::new()
.store(path.as_path())
.context("Failed to write timestamp file");
}
let paths = if args.recursive {
find_cargo_projects(&path, args.hidden)
} else {
let metadata = metadata(&path).context(format!(
"Failed to gather metadata for {:?}",
path.display()
))?;
let out = Path::new(&metadata.target_directory).to_path_buf();
if out.exists() {
vec![out]
} else {
anyhow::bail!("Failed to clean {:?} as it does not exist.", out);
}
};
let toolchains = match &criterion {
Criterion::Installed => Some(vec![]),
Criterion::Toolchains(vec) => Some(vec).cloned(),
_ => None,
};
if let Some(toolchains) = toolchains {
for project_path in &paths {
match remove_not_built_with(project_path, &toolchains, dry_run) {
Ok(cleaned_amount) if dry_run => {
info!(
"Would clean: {} from {project_path:?}",
format_bytes(cleaned_amount)
)
}
Ok(cleaned_amount) => info!(
"Cleaned {} from {project_path:?}",
format_bytes(cleaned_amount)
),
Err(e) => error!(
"{:?}",
e.context(format!("Failed to clean {project_path:?}"))
),
};
}
} else if let Criterion::MaxSize(size) = criterion {
for project_path in &paths {
match remove_older_until_fits(project_path, size, dry_run) {
Ok(cleaned_amount) if dry_run => {
info!(
"Would clean: {} from {project_path:?}",
format_bytes(cleaned_amount)
)
}
Ok(cleaned_amount) => info!(
"Cleaned {} from {project_path:?}",
format_bytes(cleaned_amount)
),
Err(e) => error!("Failed to clean {:?}: {:?}", project_path, e),
};
}
} else {
let keep_duration = if let Criterion::File = criterion {
let ts = Timestamp::load(path.as_path()).expect("Failed to load timestamp file");
Duration::from(ts)
} else if let Criterion::Time(days_to_keep) = criterion {
Duration::from_secs(days_to_keep * 24 * 3600)
} else {
unreachable!();
};
for project_path in &paths {
match remove_older_than(project_path, &keep_duration, dry_run) {
Ok(cleaned_amount) if dry_run => {
info!(
"Would clean: {} from {project_path:?}",
format_bytes(cleaned_amount)
)
}
Ok(cleaned_amount) => info!(
"Cleaned {} from {project_path:?}",
format_bytes(cleaned_amount)
),
Err(e) => error!("Failed to clean {:?}: {:?}", project_path, e),
};
}
}
Ok(())
}