-
Notifications
You must be signed in to change notification settings - Fork 16
/
main.rs
355 lines (293 loc) · 11 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#[macro_use]
extern crate clap;
extern crate colored;
extern crate liboskar;
use clap::{App, AppSettings};
use colored::*;
use liboskar::prelude::*;
use std::env;
use std::path::PathBuf;
use std::process::Command;
#[allow(unknown_lints)]
#[allow(clippy::cognitive_complexity)]
#[allow(clippy::unreadable_literal)]
fn main() {
// command-line parser
#[cfg(feature = "english")]
let yaml = load_yaml!("cli/options-en.yml");
#[cfg(feature = "francais")]
let yaml = load_yaml!("cli/options-fr.yml");
#[cfg(feature = "deutsch")]
let yaml = load_yaml!("cli/options-de.yml");
let matches = App::from_yaml(yaml)
.version(crate_version!())
.set_term_width(90)
.setting(AppSettings::SubcommandRequired)
.get_matches();
// TODO this should install manpages?
if let Some(x) = matches.subcommand_matches("update") {
let force = x.is_present("force");
println!("current version: {}", crate_version!());
let s = if force {
"curl -LSfs https://japaric.github.io/trust/install.sh | sh -s -- --git vmchale/tin-summer --force"
} else {
"curl -LSfs https://japaric.github.io/trust/install.sh | sh -s -- --git vmchale/tin-summer"
};
let script = Command::new("bash")
.arg("-c")
.arg(s)
.output()
.expect("failed to execute update script.");
let script_string = String::from_utf8(script.stderr).unwrap();
println!("{}", script_string);
} else if let Some(command) = matches.subcommand_matches("clean") {
// check that we aren't in the home dir
let home_dir_str = match env::var("HOME") {
Ok(val) => val,
_ => "/home".to_string(),
};
let home_dir = PathBuf::from(home_dir_str);
let regex = command.value_of("excludes").map(|r| check_regex(r));
// whether to clean up tagfiles generated for vim/emacs
let vimtags = command.is_present("tags");
// set path to dirs
let dirs = get_dirs(command.values_of("dir"));
let force = command.is_present("force");
for dir in dirs {
if (dir != home_dir) && !force {
clean_project_dirs(dir, ®ex, vimtags);
} else {
eprintln!(
"{}: not cleaning directory '{}', as it is your home directory. To clean your home directory, rerun with --force.",
"Warning".yellow(),
dir.display()
)
}
}
}
// test stuff
else if let Some(command) = matches.subcommand_matches("parallel") {
// set flag to print everything
let print_all = command.is_present("all");
// set whether to print files too
let print_files = command.is_present("files");
// get the number of processors to be used
let nproc = get_threads(command.value_of("threads"));
// set threshold
let min_bytes = threshold(command.value_of("threshold"));
// set path to dirs
let dirs = get_dirs(command.values_of("dir"));
let regex = command.value_of("excludes").map(|r| check_regex(r));
for dir in dirs {
let mut w = Walk::new(dir, nproc);
if let Some(b) = min_bytes {
w.set_threshold(b);
}
if !print_all {
let depth = get_depth(command.value_of("depth"));
w.set_depth(depth);
} else if command.is_present("depth") {
eprintln!(
"{}: flag --all is not compatible with --depth",
"Warning".yellow()
);
}
if print_files {
w.with_files();
}
if let Some(e) = regex.clone() {
w.set_regex(e);
}
print_parallel(w);
}
}
// find large files
else if let Some(command) = matches.subcommand_matches("fat") {
// set threshold
let min_bytes = match threshold(command.value_of("threshold")) {
Some(t) => t,
_ => 31457280, // 30 MB
};
// set depth
let depth = if !command.is_present("all") {
Some(get_depth(command.value_of("depth")))
} else if command.is_present("depth") {
eprintln!(
"{}: flag --all is not compatible with --depth",
"Warning".yellow()
);
None
} else {
None
};
// don't print warnings
//let silent = command.is_present("silent");
// set whether to print files too
let print_files = command.is_present("files");
// set regex for exclusions
let regex = command.value_of("excludes");
// set path to dirs
let dirs = get_dirs(command.values_of("dir"));
for dir in dirs {
// get relevant filenames &c.
let v = match regex {
Some(r) => read_all(&dir, 0, depth, Some(&check_regex(r)), &None, false, false),
_ => read_all(&dir, 0, depth, None, &None, false, false),
};
// filter by depth
let mut v_filtered = v.filtered(Some(min_bytes), !print_files, depth);
// display results
v_filtered.display_tree(&dir);
}
}
// find large files
else if let Some(command) = matches.subcommand_matches("directories") {
// set threshold
let min_bytes = threshold(command.value_of("threshold"));
// set depth
let depth = if !command.is_present("all") {
Some(get_depth(command.value_of("depth")))
} else if command.is_present("depth") {
eprintln!(
"{}: flag --all is not compatible with --depth",
"Warning".yellow()
);
None
} else {
None
};
// set regex for exclusions
let regex = command.value_of("excludes");
// set whether to print files too
let print_files = command.is_present("files");
// set path to dirs
let dirs = get_dirs(command.values_of("dir"));
for dir in dirs {
// get relevant filenames &c.
let v = match regex {
Some(r) => read_all(&dir, 0, depth, Some(&check_regex(r)), &None, false, false),
_ => read_all_fast(&dir, 0, depth),
};
// filter by depth
let mut v_filtered = v.filtered(min_bytes, !print_files, depth);
// display results
v_filtered.display_tree(&dir);
}
} else if let Some(command) = matches.subcommand_matches("files") {
// set threshold
let min_bytes = threshold(command.value_of("threshold"));
// set depth
let depth = if !command.is_present("all") {
Some(get_depth(command.value_of("depth")))
} else if command.is_present("depth") {
eprintln!(
"{}: flag --all is not compatible with --depth",
"Warning".yellow()
);
None
} else {
None
};
// set regex for exclusions
let regex = command.value_of("excludes");
// set whether to print files too
let print_files = true;
// set path to dirs
let dirs = get_dirs(command.values_of("dir"));
for dir in dirs {
// get relevant filenames &c.
let v = match regex {
Some(r) => read_all(&dir, 0, depth, Some(&check_regex(r)), &None, false, false),
_ => read_all(&dir, 0, depth, None, &None, false, false),
};
// filter by depth
let mut v_filtered = v.filtered(min_bytes, !print_files, depth);
// display results
v_filtered.display_tree(&dir);
}
} else if let Some(command) = matches.subcommand_matches("artifacts") {
// set threshold
let min_bytes = threshold(command.value_of("threshold"));
// set depth
let depth = if !command.is_present("all") {
Some(get_depth(command.value_of("depth")))
} else if command.is_present("depth") {
eprintln!(
"{}: flag --all is not compatible with --depth",
"Warning".yellow()
);
None
} else {
None
};
// set number to print out
let num_int = if command.is_present("count") {
Some(get_num(command.value_of("count")))
} else {
None
};
// whether to clean up tagfiles generated for vim/emacs
let vimtags = command.is_present("tags");
// decide whether to sort
let should_sort = command.is_present("sort");
// set whether to print files too
let print_files = command.is_present("files");
// set path to dirs
let dirs = get_dirs(command.values_of("dir"));
for dir in dirs {
// get relevant filenames &c.
let excludes = get_excludes(command.value_of("excludes"));
let v = read_all(&dir, 0, depth, Some(&excludes), &None, vimtags, true);
let mut v_processed = if should_sort {
v.sort(num_int, min_bytes, !print_files, depth)
} else {
v.filtered(min_bytes, !print_files, depth)
};
v_processed.display_tree(&dir);
}
}
// sort entities by size
else if let Some(command) = matches.subcommand_matches("sort") {
// set threshold
let min_bytes = threshold(command.value_of("threshold"));
// set depth
let depth = if !command.is_present("all") {
Some(get_depth(command.value_of("depth")))
} else if command.is_present("depth") {
eprintln!(
"{}: flag --all is not compatible with --depth",
"Warning".yellow()
);
None
} else {
None
};
// set number to print out
let num_int = if command.is_present("count") {
Some(get_num(command.value_of("count")))
} else {
None
};
// set whether to print files too
let print_files = command.is_present("files");
// set path to dirs
let dirs = get_dirs(command.values_of("dir"));
for dir in dirs {
// set regex for exclusions
let regex = if let Some(n) = command.value_of("excludes") {
Some(n)
} else {
None
};
// get relevant filenames &c.
let v = match regex {
Some(r) => read_all(&dir, 0, depth, Some(&check_regex(r)), &None, false, false),
_ => read_all(&dir, 0, depth, None, &None, false, false),
};
// sort them
let mut v_sorted = v.sort(num_int, min_bytes, !print_files, depth);
// display sorted filenames
v_sorted.display_tree(&dir);
}
}
}