Skip to content

Commit 019034e

Browse files
committed
Modify fetcher to work with a list of ArtifactTypes
It builts the GlobalSet based on the ArtifactTypes' patterns. Currently main calls fetch_artifacts by providing list of all built-in artifact types (currentyl two). In the future we will support further custimizations
1 parent 2ec37e6 commit 019034e

2 files changed

Lines changed: 64 additions & 29 deletions

File tree

src/crufty/fetcher.rs

Lines changed: 61 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,56 @@
11
use std::path::PathBuf;
22

3-
use globset::{Glob, GlobSetBuilder};
3+
use super::artifact_type::ArtifactType;
4+
use globset::{Glob, GlobSet, GlobSetBuilder};
45
use walkdir::WalkDir;
56

67
use super::types::ArtifactCandidate;
78

8-
pub fn fetch_artifacts(root_path: &PathBuf) -> Vec<ArtifactCandidate> {
9+
fn mk_global_set(
10+
artifact_types: Vec<ArtifactType>,
11+
) -> Result<GlobSet, globset::Error> {
912
let mut builder = GlobSetBuilder::new();
10-
// FIX-ME hardcoded pattern for Rust
11-
builder.add(Glob::new("**/target").unwrap());
12-
13-
let globset = builder.build().unwrap();
14-
15-
WalkDir::new(root_path)
16-
.into_iter()
17-
.filter_map(|entry_result| match entry_result {
18-
Err(_) => None,
19-
Ok(entry) if !entry.path().is_dir() => None,
20-
Ok(entry) => {
21-
let path = entry.into_path();
22-
let rel_path = path.strip_prefix(root_path).ok()?;
23-
match globset.is_match(&rel_path) {
24-
true => Some(ArtifactCandidate::new(path)),
25-
false => None,
13+
for art_type in artifact_types {
14+
builder.add(Glob::new(art_type.pattern())?);
15+
}
16+
builder.build()
17+
}
18+
19+
pub fn fetch_artifacts(
20+
root_path: &PathBuf,
21+
artifact_types: Vec<ArtifactType>,
22+
) -> Vec<ArtifactCandidate> {
23+
match mk_global_set(artifact_types) {
24+
Err(_) => vec![],
25+
Ok(globset) => WalkDir::new(root_path)
26+
.into_iter()
27+
.filter_map(|entry_result| match entry_result {
28+
Err(_) => None,
29+
Ok(entry) if !entry.path().is_dir() => None,
30+
Ok(entry) => {
31+
let path = entry.into_path();
32+
let rel_path = path.strip_prefix(root_path).ok()?;
33+
match globset.is_match(&rel_path) {
34+
true => Some(ArtifactCandidate::new(path)),
35+
false => None,
36+
}
2637
}
27-
}
28-
})
29-
.collect()
38+
})
39+
.collect(),
40+
}
3041
}
3142

3243
#[cfg(test)]
3344
mod tests {
3445

35-
use assert_fs::prelude::*;
3646
use assert_fs::{
3747
fixture::{ChildPath, PathChild},
48+
prelude::*,
3849
TempDir,
3950
};
4051

52+
use crate::crufty::artifact_type::ArtifactType;
53+
4154
use super::{fetch_artifacts, ArtifactCandidate};
4255

4356
fn mk_subpath(base: &TempDir, rel_path: &str) -> ChildPath {
@@ -46,19 +59,27 @@ mod tests {
4659
sub
4760
}
4861

62+
fn only_rust_projects() -> Vec<ArtifactType> {
63+
vec![ArtifactType::Rust]
64+
}
65+
66+
fn custom_project(pattern: &'static str) -> Vec<ArtifactType> {
67+
vec![ArtifactType::Custom { pattern }]
68+
}
69+
4970
fn mk_rust_project<P: PathChild>(base: &P) {
5071
base.child("target").create_dir_all().unwrap();
5172
base.child("Cargo.toml").touch().unwrap();
5273
}
5374

5475
#[test]
5576
fn test_simple_rust_project_being_scanned_folder() {
56-
// given
77+
// given there is a single rust project in the folder
5778
let temp = TempDir::new().unwrap();
5879
mk_rust_project(&temp);
5980

60-
// when
61-
let results = fetch_artifacts(&temp.to_path_buf());
81+
// when we search for Rust artifacts
82+
let results = fetch_artifacts(&temp.to_path_buf(), only_rust_projects());
6283

6384
// then
6485
assert_eq!(results.len(), 1, "Expected exactly one artifact");
@@ -67,6 +88,12 @@ mod tests {
6788
let expected = ArtifactCandidate::new(expected_path);
6889
assert_eq!(&results[0], &expected);
6990

91+
// when we search for projects other than Rust
92+
let results =
93+
fetch_artifacts(&temp.to_path_buf(), custom_project("**/bla"));
94+
// then
95+
assert_eq!(results.len(), 0, "Expected zero artifacts");
96+
7097
temp.close().unwrap();
7198
}
7299

@@ -81,8 +108,9 @@ mod tests {
81108
let project3 = mk_subpath(&temp, "work/project3");
82109
mk_rust_project(&project3);
83110

84-
// when
85-
let mut results = fetch_artifacts(&temp.to_path_buf());
111+
// when we search for Rust artifacts
112+
let mut results =
113+
fetch_artifacts(&temp.to_path_buf(), only_rust_projects());
86114

87115
// then
88116
assert_eq!(results.len(), 3, "Expected exactly three artifacts");
@@ -107,6 +135,12 @@ mod tests {
107135
let expected_3 = ArtifactCandidate::new(expected_path_3);
108136
assert_eq!(&results[2], &expected_3);
109137

138+
// when we search for projects other than Rust
139+
let results =
140+
fetch_artifacts(&temp.to_path_buf(), custom_project("**/bla"));
141+
// then
142+
assert_eq!(results.len(), 0, "Expected zero artifacts");
143+
110144
temp.close().unwrap();
111145
}
112146
}

src/main.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use clap::Parser;
22
use console::{style, Term};
3+
use crufty::artifact_type;
34
use crufty::cleaner::{self, CleanupResult};
45
use crufty::cli::{Cli, Commands};
56
use crufty::estimator::{estimate, total_size};
@@ -34,7 +35,7 @@ fn scan() -> io::Result<()> {
3435
.write_line(&format!("[+] Scanning: {}", style(path.display()).bold()))?;
3536

3637
let spinner = ui::create_spinner("collecting artifacts");
37-
let mut artifacts = fetch_artifacts(&path);
38+
let mut artifacts = fetch_artifacts(&path, artifact_type::builtin().to_vec());
3839
spinner.finish_and_clear();
3940

4041
term.write_line("")?;
@@ -82,7 +83,7 @@ fn clean() -> io::Result<()> {
8283
let path = env::current_dir()?;
8384

8485
let spinner = ui::create_spinner("collecting artifacts");
85-
let mut artifacts = fetch_artifacts(&path);
86+
let mut artifacts = fetch_artifacts(&path, artifact_type::builtin().to_vec());
8687
spinner.finish_and_clear();
8788

8889
if artifacts.is_empty() {

0 commit comments

Comments
 (0)