tugger_common/
glob.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4
5use {
6    anyhow::Result,
7    std::path::{Path, PathBuf},
8};
9
10/// Evaluate a file matching glob relative to the given directory.
11pub fn evaluate_glob<P>(cwd: P, pattern: &str) -> Result<Vec<PathBuf>>
12where
13    P: AsRef<Path>,
14{
15    let pattern_path = PathBuf::from(pattern);
16
17    let search = if pattern.starts_with('/') || pattern_path.is_absolute() {
18        pattern.to_string()
19    } else {
20        format!("{}/{}", cwd.as_ref().display(), pattern)
21    };
22
23    let mut res = Vec::new();
24
25    for path in glob::glob(&search)? {
26        let path = path?;
27
28        if path.is_file() {
29            res.push(path);
30        }
31    }
32
33    Ok(res)
34}