summaryrefslogtreecommitdiffstats
path: root/src/commands/find_pkg.rs
blob: 90cf6c17e9d09ccc2100d1a5d09f0923efa4b6d3 (plain)
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
use anyhow::Error;
use anyhow::Result;
use clap::ArgMatches;
use log::trace;

use crate::config::Configuration;
use crate::package::PackageVersionConstraint;
use crate::repository::Repository;

pub async fn find_pkg(matches: &ArgMatches, config: &Configuration, repo: Repository) -> Result<()> {
    use std::io::Write;

    let package_name_regex = matches
        .value_of("package_name_regex")
        .map(regex::RegexBuilder::new)
        .map(|mut builder| {
            builder.size_limit(1 * 1024 * 1024); // max size for the regex is 1MB. Should be enough for everyone
            builder.build()
                .map_err(Error::from)
        })
        .unwrap()?; // safe by clap

    let package_version_constraint = matches
        .value_of("package_version_constraint")
        .map(String::from)
        .map(PackageVersionConstraint::new)
        .transpose()?;

    let iter = repo.packages()
        .filter(|p| package_name_regex.captures(p.name()).is_some())
        .filter(|p| package_version_constraint.as_ref().map(|v| v.matches(p.version())).unwrap_or(true))
        .inspect(|pkg| trace!("Found package: {:?}", pkg));

    let out = std::io::stdout();
    let mut outlock = out.lock();
    if matches.is_present("terse") {
        for p in iter {
            writeln!(outlock, "{} {}", p.name(), p.version())?;
        }
        Ok(())
    } else {
        let format = config.package_print_format();
        crate::ui::print_packages(&mut outlock,
            format,
            iter,
            true,
            true)
    }
}